Yes that is what i want and visa versa?
Main Topics
Browse All TopicsI am using ansi c and need to convert a char* array with 16 chars to 16 hex values and visa versa.
I have not found a nice solution on experts exchange in ansi c. Are there any routine available.
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
printf, sprintf to make the hexadecimal presentation. There is an example in MSDN:
http://msdn.microsoft
I
visa versa - is scanf, sscanf: http://msdn.microsoft.com/
but, probably not in your case.
I have tried several options you have suggested.
Let it be me's solution is what i want: printf("%x",(int)c);
I then get a hex value 504A4F54544552 for the word "PJOTTER".
The other way around is not clear to me. I want to convert the char[14] = "504A4F54544552" back to "PJOTTER" i cannot get this working yet with any of the suggested solutions.
Thanks for your very quick responses.
Huh, I don't know what's going on with me today :(... This one works, I have tested all the outputs...
void IntToHex(long number, char* hexstr)
{
size_t hexindex;
size_t position = 0;
long mask = number;
const char hex[] = "0123456789ABCDEF";
do
{
mask = mask >> 1;
position++;
} while (mask != 0);
position = (position / 4) + ((position % 4) && 1);
hexstr[position] = '\0';
position--;
do
{
hexindex = number % 16;
number = number / 16;
hexstr[position] = hex[hexindex];
position --;
} while (number != 0);
}
Hey PJotter,
I didn't understood your problem at first... I thought you wanted to convert numbers to hex and hex to numbers... or even worse I didn't read your problem as I was supposed to.
Attached you'll see the code that does what you want it to do...
>>>> I want to convert the char[14] = "504A4F54544552" back to "PJOTTER"
The format specifiers of printf and scanf can be misused for hijacking a program by simply modifying the contents in the executable files or in memory. I wouldn't use them in newer programs.
For converting hex to int you may use the strtol instead of sscanf what isn't reinventing the wheel either but is the appropriate function for converting hex digits to char.
Note, you always should make error checks for buffer sizes and results of a conversion.
>> The format specifiers of printf and scanf can be misused for hijacking a program by simply modifying the contents in the executable files or in memory.
If you can modify that in your executable, you can modify anything else, with equally devastating consequences ... If you want to protect against all that, you might as well not write code at all. Or better yet : get a more secure OS and properly set permissions ;)
Regarding strtol vs. sscanf - the downside is that you have to copy the entire hex string two characters at a time first. Those are unnecessary copy operations that can be avoided by using sscanf which works directly on the original hex string.
>>>> you can modify anything else,
format specifiers are strings which easily can be spotted in an executable. The format specifiers were main target for malware and have been reasons for many security leaks and buffer overflow attacks. It is two things to protect new code from any harm or to simply avoid known dangers, especially if there is less benefit involved like in the case above.
printf and scanf more over are not type safe and actually are an overkill for converting numbers. The downside of copying two bytes to an already zero-terminated 3-bytes char array cannot reasonably compared with the overhead a scanf operation will cause.
Big words. But you need to actually support them if you want to discourage the use of sscanf/sprintf in this case.
>> The format specifiers were main target for malware
What advantage would malware have in modifying these format specifiers here ? Do you have a reference for this assertion ?
>> reasons for many security leaks
Show me one in the code I posted.
>> and buffer overflow attacks.
Show me one in the code I posted. (related to the use of sscanf and sprintf - I know that if the output buffer isn't big enough to hold all data, there is a problem, but checks for that are easily added)
>> printf and scanf more over are not type safe
The type un-safety you're referring to is only a worry for the programmer, but no worry at runtime, when the code has been properly written. There's no issue in the code I posted. Unless you can point one out ?
>> The downside of copying two bytes to an already zero-terminated 3-bytes char array cannot reasonably compared with the overhead a scanf operation will cause.
That's arguable - especially with larger strings. The overhead you're referring to, is the "overhead" of reading and interpreting the format string. This might or might not be slower than the overhead of copying those two characters. Performance tests can show that if you notice a performance problem.
So, sure, strtol is an alternative (to be confirmed by tests if needed), but it doesn't invalidate the point I was trying to make earlier to not re-invent the wheel (by implementing your own hex-to-int conversion code for example).
>> I wonder why everybody is trying to make this seem so complicated, while it is very straightforward. Let the standard library do the work for you...
Infinity, I think that what I wrote is quite similar on what you wrote? You're using a pointer approach while I'm using an array approach... but indeed I'm using the standard library
You're not making full use of it though. By using %x instead of %2x, you are forced to copy the hex data two characters at a time before processing it.
Furthermore, there are a few problems with your latest code :
(a) Your StringToHex function does not support characters under 0x10
(b) Your temp buffer in your HexToString function is not null terminated
(c) Your HexToString function fails if the hex string length is not a multiple of 2
>> You're not making full use of it though. By using %x instead of %2x, you are forced to copy the hex data two characters at a time before processing it.
I must admit it, my standard library usage has become somewhat rusty. I agree with you, it also should be %02X for the sprintf function.
Thanks for the fixes :D...
>> (a) Your StringToHex function does not support characters under 0x10
Agree, due the %02X that was incorrect.
>> (b) Your temp buffer in your HexToString function is not null terminated
Agree, removed the temp and avoided the use of strncpy
Attached the fixed code:
>> Attached the fixed code:
Forgive me for being devil's advocate but doesn't your code now do exactly what I8's does about 9 posts ago, (only less efficiently due to unnecessary use of indexers and temporary variables and repeated calls to strlen)? I'm not quite sure I see what this adds to an already reasonable solution?
Erhmm... well, I was not trying to post a more efficient solution than Infinity's, and I know that the use of pointers is faster by far, I just wanted to post a new code without bugs because I posted the code about 10 posts ago before Infinity's pointer solution... and fix it just because Infinity noticed the errors, I also did it that way because I think that the use of pointers for C beginners is sometimes difficult than using array indexers.
Any way I'm not trying to win anything with this, I agree that Infinity's solution is better than mine.
Business Accounts
Answer for Membership
by: Let_Me_BePosted on 2009-11-04 at 13:58:39ID: 25744434
What do you mean convert 16 characters to hex values? You want to print them as hex values of the ASCII values? Then simply use printf("%x",(int)c); Otherwise please clarify.