By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

printing hexadecimal value in c++

P: 2

I'm trying to print the hexadecimal value for a string.
Actually, it was already encoded by my application into utf-8. And I would like to check the converted hex-code.
Could some one please let me know how to print it to a file ?

pFile = fopen ("C:\\myfile.txt","w");
fprintf (pFile, "%X ",c);
The above code didn't work!!

Thanks a lot for your inputs,
Sep 13 '10 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 8,916
Didn't work how?
Sep 13 '10 #2

P: 2
suppose c contains string "test" , actual hexcode 74657374 is but its printing 40A150

Sep 13 '10 #3

Expert Mod 5K+
P: 8,916
You haven't said what type c is but I am assuming an array of char

char c[100];

You are printing out the hexidecimal representation of the address of the memory that stores the array.

To print the value of the character you need to pass the character to fprintf not the pointer to the array

fprintf (pFile, "%X ",c[0]);

Obviously you will need to repeat for all used entries in the array.
Sep 13 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.