views:

158

answers:

1

I have a program that's supposed to take values and print them back out. But when the user enters something like 12, (C in HEX) the program prints out some weird letter, that I think is the representation in ASCII. Is there a way to make it save those numbers as raw numbers? I'm doing the input and output through an external library, so I don't know if that has anything to do with it.

+1  A: 

There are multiple ways to store a number inside of a computer. The main ones are:

  • As a native, binary number. The number 123 will be stored as the octets: 0x7b, with zero-padding if using a larger than one byte integer. Zero-padding could be on either the left (big-endian machine) or the right (little-endian machine).
  • As a string. 123 will be stored as 0x31 32 33, assuming ASCII/Latin1/UTF-8. There may be either a length field first (stored as a native binary number) or a zero-byte (0x00) afterwards to indicate where the string ends.
  • BCD. 123 will be stored as 0x01 23. The bytes may also be stored in little-endian order, as 0x23 01.

You'll need to figure (hopefully the documentation says) out what format your input library wants and what format your output library provides, and convert between them in your program. The general name for this conversion is "binary-decimal conversion"

derobert