C code to convert decimal to hexadecimal. Hexadecimal number system: It is base 16 number system which uses the digits from 0 to 9 and A, B, C, D, E, F. You can check Convert to ASCII String from hex String in C and this SO question – Infinite Recursion Dec 5 '13 at 9:47 number isn't a 'hex value', it's a char, i.e. It doesn't matter whether you call hextoascii(buffer, 0xFF), hextoascii(buffer, 255), or hextoascii(buffer, 0377) - they are all the same.
I want to convert two ASCII bytes to one hexadecimal byte.eg.
0x30 0x43 => 0x0C , 0x34 0x46 => 0x4F
...
The ASCII bytes are a number between 0
and 9
or a letter between A
and F
(upper case only), so between 0x30
... 0x39
and 0x41
... 0x46
I know how 'to construct' 0x4F
with the numbers 0x34
and 0x46 : 0x4F = 0x34 * 0x10 + 0x46
So, in fact, i would to convert one ASCII byte in hexadecimal value.
For that, i can build and array to assign the hexadecimal value to the ASCII char :
But, maybe it have a most « proper » solution.
The program will be run on an AVR µC and is compiled with avr-gcc
, so scanf()
/ printf()
solutions aren't suitable.
Have you got an idea ?Thanks
ojblass5 Answers
i can't make sense of your examples, but if you want to convert a string containing hexadecimal ascii characters to its byte value (e.g. so the string '56' becomes the byte 0x56, you can use this (which assumes your system is using ASCII)
You'd use it like e.g.
And res (which must be at least half the length of the in
parameter) now contains the 2 bytes 0x12,0x34
Note also that this code needs the hexadecimal letters A-F to be capital, a-f won't do (and it doesn't do any error checking - so you'll have to pass it valid stuff).
nosnosYou can use strtol()
, which is part of avr-libc, or you can write just your specific case pretty easily:
The task:
Convert a string containing hexadecimal ascii characters to its byte valuesso ascii 'FF'
becomes 0xFF
and ascii '10' (x31x30x00)
becomes 0x10
// the final result should be:
//1. Firt step: convert asciiString so it contains upper cases only:
use:
//2. Convert a string containing hexadecimal ascii characters to its byte values:
//3. print result:
// use:
//4. The result should be:
0xAA 0xAA 0x12 0xFF
//5. This is the test program:
sg7sg7Here's a version that works with both upper and lower-case hex strings:
Loïc G.Ascii To Hex Conversion Program In Embedded C
C Ascii To Hex Conversion Table
Not the answer you're looking for? Browse other questions tagged casciihexavr-gcc or ask your own question.
Say I have a string like:
Where every two characters correspond to the hex representation of their ASCII, value, eg:
So how can I get 'hello'
from '48656c6c6f'
without having to create a lookup ASCII table? atoi()
obviously won't work here.
5 Answers
James CurranJames CurranHex digits are very easy to convert to binary:
So to do the whole string looks something like this:
You might reasonably ask why one would do it this way when there's strtol
, and using it is significantly less code (as in James Curran's answer). Well, that approach is a full decimal order of magnitude slower, because it copies each two-byte chunk (possibly allocating heap memory to do so) and then invokes a general text-to-number conversion routine that cannot be written as efficiently as the specialized code above. Christian's approach (using istringstream) is five times slower than that. Here's a benchmark plot - you can tell the difference even with a tiny block of data to decode, and it becomes blatant as the differences get larger. (Note that both axes are on a log scale.)
Is this premature optimization? Hell no. This is the kind of operation that gets shoved in a library routine, forgotten about, and then called thousands of times a second. It needs to scream. I worked on a project a few years back that made very heavy use of SHA1 checksums internally -- we got 10-20% speedups on common operations by storing them as raw bytes instead of hex, converting only when we had to show them to the user -- and that was with conversion functions that had already been tuned to death. One might honestly prefer brevity to performance here, depending on what the larger task is, but if so, why on earth are you coding in C++?
Also, from a pedagogical perspective, I think it's useful to show hand-coded examples for this kind of problem; it reveals more about what the computer has to do.
zwolzwolI can not comment, but zwol's solution has a bug:
is correctly
as the shift operator has lower precedence than add
strtol should do the job if you add 0x
to each hex digit pair.