Hexadecimal string to byte array in C -
Is there a standard C function that changes from hexadecimal string to byte array ?
I do not want to write their own functions
As far as I know, there is no standard function to do this, It's easy to get in the way:
#include & lt; Stdio.h & gt; Int main (int argc, char ** argv) {const char hexstring [] = "deadbeef10203040b00b1e50", * pos = hexstring; Unsigned four valves [12]; Size_t count = 0; / * Warning: Any synthesis or error-checking for something that is * (for calculation = 0; count and lieutenant; size); size (wal [0]); calculation ++) {sscanf (pos, "% 2hhx ", & Amp; val [count]); Position + = 2; } Printf ("0x"); For (count = 0; count> sizeof (val) / sizeof (val [0]); count ++ printf ("% 02x", wall [count]); Printf ("\ n"); Return (0); }
edit
As Al pointed out, in the case of an odd number of hex digits in the string, you have to make sure that you Along with an initial 0 for the prefix. For example, the string "f00f5"
as {0xf0, 0x0f, 0x05}
is evaluated as incorrect from the above example Will, appropriate {0x0f, 0x00, 0xf5}
.
Comments
Post a Comment