You are on page 1of 3

Convert a c hex value into a char* - Stack Overflow

http://stackoverflow.com/questions/4434821/convert-a-c-hex-value-int...

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Convert a c hex value into a char*

How to convert a hex value in c into an equivalent char* value. For example if the hex value is 1df2 the char* should also contain 1df2 . I am using the VinC compiler and the VinL linker for the VNC2 USB Chip from FTDI . It has these following header files; stdlib, stdio and string. These are however subsets of the main c libraries and don't have the obvious answers such as snprintf or sprintf . The docs say the following types are valid, There are certain definitions for variable and function types which are used throughout the kernel and drivers. They are available to applications in the vos.h header file. Null pointer and logic definitions: #define NULL #define TRUE #define FALSE Variable type definitions: #define #define #define #define #define #define uint8 int8 int16 uint16 uint32 pvoid unsigned char short unsigned unsigned unsigned char 0 1 0

short int char *

Function type definitions: typedef typedef typedef typedef typedef typedef uint8 (*PF)(uint8); void (*PF_OPEN)(void *); void (*PF_CLOSE)(void *); uint8 (*PF_IOCTL)(pvoid); uint8 (*PF_IO)(uint8 *, unsigned short, unsigned short *); void (*PF_INT)(void);

Any suggestions?
c char hex

edited Apr 24 at 1:48 Deepu 3,887 3 5 23

asked Dec 14 '10 at 0:29 RenegadeAndy 614 5 17 35

You cannot have "a char* with '1df2' in it", because there is nothing "in" the char*. Pointers don't contain data, they point to it. There is no real string type in C. Karl Knechtel Dec 14 '10 at 6:50 Just one of my peeves... if you mean convert a hex value into a string, then say it. Don't say "into a char * ". A char * can be used to point to a string, but it's not a string. R.. Dec 14 '10 at 7:18 Ok well I want to convert it to a string then RenegadeAndy Dec 14 '10 at 10:03

3 Answers
Use snprintf() : int to_hex(char *output, size_t len, unsigned n) { return snprintf(output, len, "%.4x", n); }

1 of 3

08-10-2013 19:21

Convert a c hex value into a char* - Stack Overflow

http://stackoverflow.com/questions/4434821/convert-a-c-hex-value-int...

Given the new information that it's a fairly basic embedded system, then if you're only interested in 16 bit numbers a minimal solution like this probably suffices: /* output points to buffer of at least 5 chars */ void to_hex_16(char *output, unsigned n) { static const char hex_digits[] = "0123456789abcdef"; output[0] output[1] output[2] output[3] output[4] } (It should be clear how to extend it to wider numbers).
edited Dec 14 '10 at 1:29 answered Dec 14 '10 at 0:31 caf 100k 8 94 220

= = = = =

hex_digits[(n >> 12) & 0xf]; hex_digits[(n >> 8) & 0xf]; hex_digits[(n >> 4) & 0xf]; hex_digits[n & 0xf]; '\0';

I dont have access to that function because I am developing on an embedded device! Any others ways? RenegadeAndy Dec 14 '10 at 0:34 @RenegadeAndy: See update. caf Dec 14 '10 at 1:32 It doesnt compile with unsigned n:[VinC.exe] : src\log.h line 5: (error) C2807 illegal function pointer declaration RenegadeAndy Dec 14 '10 at 10:07 @RenegadeAndy: If it doesn't accept the unsigned type then it is not actually a C compiler at all. However, you can replace that with unsigned int n , since that is the same type. caf Dec 14 '10 at 12:38

Try sprintf: int to_hex(char *output,unsigned n) { return sprintf(output, "%.4x", n); } it's less safe than caf's answer but should work if you have stdio. You must therefore make sure that the output buffer is big enough to hold the resulting string.
answered Dec 14 '10 at 0:49 Jon Cage 12.4k 9 48 91 It infact has a subset of stdio so doesnt have sprintf either RenegadeAndy Dec 14 '10 at 0:57

Something like this should do it: void to_hex(char *buffer, size_t size, unsigned n) { size_t i; size_t j; char c; unsigned digit; // Print digits for (i = 0; i < { digit = n & buffer[i] = n >>= 4; if (n == 0) { break; } } // Append NUL in the reverse order size - 1; ++i) 0xf; digit < 10 ? digit + '0' : digit - 10 + 'A';

2 of 3

08-10-2013 19:21

Convert a c hex value into a char* - Stack Overflow

http://stackoverflow.com/questions/4434821/convert-a-c-hex-value-int...

buffer[i + 1] = 0; // Reverse the string for (j = 0; j < i / 2; ++j) { c = buffer[j]; buffer[j] = buffer[i - j]; buffer[i - j] = c; } } But you are saying you have stdio available, so there's no need write anything like this yourself. Edit: Could be that the compiler expects K&R style prototype: void to_hex(buffer, size, n) char *buffer; size_t size; unsigned n; { ... Try this on Codepad.
edited Dec 14 '10 at 1:55 answered Dec 14 '10 at 0:55 detunized 6,814 13 33

This looks like the kind of solution im going to need. However it doesnt understand unsigned n, nor does it like unsigned void n - i think we have to give it a proper type.... RenegadeAndy Dec 14 '10 at 1:03 How can it be that it doesn't understand unsigned type? And there's no unsigned void n in this code. Are you sure you don't have any strange macro definitions screwing this up? detunized Dec 14 '10 at 1:05 No idea about macros... compiler states: [VinC.exe] : src\log.h line 5: (error) C2807 illegal function pointer declaration RenegadeAndy Dec 14 '10 at 1:08 I didn't realize that this was tagged as C . I edited to make it C friendly. Does it work now? Still the errors the compiler produces are quite strange. detunized Dec 14 '10 at 1:14 Im sorry I dont see what has changed..it wont compile with unsigned n RenegadeAndy Dec 14 '10 at 1:23 show 5 more comments

Not the answer you're looking for? Browse other questions tagged c char hex or ask your own question.

3 of 3

08-10-2013 19:21

You might also like