printing hexadecimal value in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pratapd
    New Member
    • Sep 2010
    • 2

    printing hexadecimal value in c++

    Hello,

    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.tx t","w");
    fprintf (pFile, "%X ",c);
    The above code didn't work!!

    Thanks a lot for your inputs,
    Pratap
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Didn't work how?

    Comment

    • Pratapd
      New Member
      • Sep 2010
      • 2

      #3
      suppose c contains string "test" , actual hexcode 74657374 is but its printing 40A150

      Regards,
      Pratap

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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.

        Comment

        Working...