# Hexidecimal value to ASCII representation

#### emoney123

Joined Jun 4, 2012
32
Hello,
I am wondering what is the best way to convert a hex number into its ASCII representation. For example, the hex number I would like to convert is E9 (233 decimal) and would like write it out as ascii 233 (hex: 0x32 0x33 0x33). Any help is appreciated.
Thank you.

#### spinnaker

Joined Oct 29, 2009
7,837
There is no such thing as "hex" to a computer. Hexadecimal is simply a number representation for humans,

You need to define your requirements a lot better than you did. First what programming language are you using? And do you have a "hex" number in a string or simply a "hex" number?

#### WBahn

Joined Mar 31, 2012
24,974
Is this a homework problem (I ask because this is a very common programming exercise).

If your "hex value" is stored as an integer, then it is merely an integer and is represented internally in binary. If you have an ASCII string containing the characters "E9" then the first step is to convert this to an integer by walking across the string, beginning with the most significant digit, and multiplying the converted result by the number base (16) and then adding the next digit. You can get the value for each digit by subtracting the base digit, either '0' or 'A', as appropriate.

Once you have an integer you can build up the ASCII string that represents in, in whatever base you choose (within reason) by repeatedly performing modulo division followed by integer division, each by the number base you are converting to (so ten, in this case). This will build up the representation starting with the least significant digit. There are other, more brute force ways to do it as well.

Joined Jul 18, 2013
19,436
Check out the way it is done in a μp, there are several example in the Piclist site, convert to BCD and add 30.
Max.

#### wayneh

Joined Sep 9, 2010
16,128
Best? What are your criteria for "best"? I just use online free tools.

#### MrChips

Joined Oct 2, 2009
19,755
As pointed out above, hex is a representation that only humans use because it is easier to write down hex than a whole bunch of zeros and ones.

Let's say you have a bucket full of 234 marbles.
You need to find a way of knowing how many groups of 100, groups of 10 and remainders you have.
How would you do that?

#### MrAl

Joined Jun 17, 2014
6,808
Hi,

The best way is probably the fastest way.

That said, an easy way is to just subtract 100 until you get a negative number, then add 100. The number of times you subtract 100 minus 1 is the first digit.
Then subtract 10 until you get a negative number, then add 10. The number of times you subtract 10 minus 1 is the second digit.
What remains is the units digit.

Subtract 100 once, count 1.
Subtract 100 again, count 2, but that leaves us with a negative number so add 100. We are left with 23.
So the first digit is 2-1=1.
Now subtract 10, count 1.
Subtract 10 again, count 2.
Subtract 10 again, count 3, but now we have a negative number so add 10. The second digit is 3-1=2 and we are left with 3.
So the complete number is:
1,2,3

and now we convert to ascii by adding 0x30 to each digit in most cases (with the standard code page that is). Alternately, we can use sprintf() with the "%d" format: If the digits are stored in a,b,c then:

char s[5]; //some room to work in
sprintf(s,"%d%d%d\0",a,b,c);

and now the string in 's' is "123" with a trailing zero.
That's the ANSI version.

Of course if all you want to do is print it somewhere you could just do:
char s[5];
sprintf(s,"%d\0",123);

and that can be printed to console or to a file or posted to a listbox, etc.

You probably dont need the "\0" added in those examples:
sprintf(s,"%d",123);

Last edited:

#### WBahn

Joined Mar 31, 2012
24,974
That works for the example E9, because you happen to know the answer only has three digits. But it was only an example and he gave no intrinsic bounds. So how would your suggestion work if the hex value were F67B?

As has already been point out, a key question that has not been answered is how is this hex representation available? Is it stored in an variable of integer data type? If so, then just print it out. Is it in a string? If so, then how do you subtract 100 from it? That's like telling someone to subtract an "orange" from an "apple".

Assuming it is available in an integer variable (or can be converted to that) and that you want to build up the ASCII string representation (instead of just using sprint(), for instance), then first you need to determine how many digits there are in the base-10 representation. You can't make any assumptions about this unless you want a bunch of leading zeroes that don't need to be there. You also have to allow for the possibility that the value IS zero, in which case you DO want exactly one leading zero.

#### Picbuster

Joined Dec 2, 2013
983
Hello,
I am wondering what is the best way to convert a hex number into its ASCII representation. For example, the hex number I would like to convert is E9 (233 decimal) and would like write it out as ascii 233 (hex: 0x32 0x33 0x33). Any help is appreciated.
Thank you.
I assume that you are using C and not in old fashion Assembler as being use in the previous century.
Use printf("%c", value);
or sprint(buf,"%c",value); character is now in buf[]

Picbuster

#### WBahn

Joined Mar 31, 2012
24,974
I assume that you are using C and not in old fashion Assembler as being use in the previous century.
Use printf("%c", value);
or sprint(buf,"%c",value); character is now in buf[]

Picbuster
So if, as in the example, the value 0xE9, what is printf("%c", value) going to print?

If you use

sprintf(buf, "%i", value);

THEN the string of ASCII codes for '2', '3', 3' (following by a NUL terminator) will be in buf[].

Joined Jul 18, 2013
19,436
I assume that you are using C and not in old fashion Assembler as being use in the previous century.
I consider that a rather Elitist remark, an apparent put down to the many who still have the ability to accomplish it.
Max.

#### WBahn

Joined Mar 31, 2012
24,974
If it's a putdown, it would seem more along the lines of a putdown to the new generation that don't have the ability to accomplish it. But then that might just be my personal prejudices poking through.