none
how to convert a char or string to hex? RRS feed

  • Question

  • Hi,

    I want to convert a string to hexadecimal in vc++. Is there any built in function in vc++ to do this. I am working on visual studio 2008. Just like VB has some in built some in built function. otherwise if there are no built in functions, any code which anyone can share is appreciated.

    Thanks & Regards,
    P.Karthik
    Wednesday, September 14, 2011 8:45 AM

Answers

  • Hi Karthik,

    Try this:

    string input = "Hello World!";
    char[] values = input.ToCharArray();
    foreach (char letter in values)
    {
        // Get the integral value of the character.
        int value = Convert.ToInt32(letter);
        // Convert the decimal value to a hexadecimal value in string form.
        string hexOutput = String.Format("{0:X}", value);
        Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
    }
    

     


    Regards, http://shwetamannjain.blogspot.com
    Wednesday, September 14, 2011 9:03 AM
  • Why you appended 2 with X.

    Try it without 2 as

    string ^ hexOutput = System::String:Format("{0:X}", value);


    Regards, http://shwetamannjain.blogspot.com
    • Marked as answer by Karthik Agarwal Wednesday, September 14, 2011 9:43 AM
    Wednesday, September 14, 2011 9:29 AM

All replies

  • strtol is used to convert string to long integer right?

    check out the following link as reference:

    http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

    Wednesday, September 14, 2011 8:59 AM
  • Hi Karthik,

    Try this:

    string input = "Hello World!";
    char[] values = input.ToCharArray();
    foreach (char letter in values)
    {
        // Get the integral value of the character.
        int value = Convert.ToInt32(letter);
        // Convert the decimal value to a hexadecimal value in string form.
        string hexOutput = String.Format("{0:X}", value);
        Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
    }
    

     


    Regards, http://shwetamannjain.blogspot.com
    Wednesday, September 14, 2011 9:03 AM
  • Hi swetha jain,

     

    What you told looks like C# code. I am working on vc++ not C#. Will this work in vc++!!!

    Wednesday, September 14, 2011 9:06 AM
  • Hi,

    I am not sure about VC++. But check out this link:

    http://www.codeguru.com/forum/showthread.php?t=231054

     


    Regards, http://shwetamannjain.blogspot.com
    Wednesday, September 14, 2011 9:08 AM
  • or you can try converter to convert C# code to VC++

    http://tangiblesoftwaresolutions.com/

    Wednesday, September 14, 2011 9:11 AM
  • Conversion to int32 is done perfectly but when i try to

    string ^ hexOutput = System::String:Format("{0:2X}", value);

    print double precision hexadecimal value it is not returning proper value. It is just returning 2X as output
    Wednesday, September 14, 2011 9:26 AM
  • Why you appended 2 with X.

    Try it without 2 as

    string ^ hexOutput = System::String:Format("{0:X}", value);


    Regards, http://shwetamannjain.blogspot.com
    • Marked as answer by Karthik Agarwal Wednesday, September 14, 2011 9:43 AM
    Wednesday, September 14, 2011 9:29 AM
  • Hey sorry i am trying to print hex value to output file as 0x46 something like that so. If the value it is returning is lessthan 10 then i need to append another 0 na. Its like getting 2 values after a decimal point for float values. thanks for the help. Got it finally. its {0:X2} rather {0:2X}. The former gives hexadecimal where as later gives 2X as output all the time. I appreciate your help Shweta  Jain. Thanks a lot.
    Wednesday, September 14, 2011 9:39 AM
  • Try this code.

    #include<iostream.h>
    
    int main()
    {
    unsigned long temp=0, counter=0, OutPut=0;
    char Letters[10]= "abcd";                        // letter characters
    
    cout<<Letters;         
    for (counter=1; counter<=strlen(Letters); counter++)  
    {
    temp = Letters[counter-1];          // get character of char
    
    OutPut=OutPut +temp;                 // add result (temp) to OutPut
    }
    
    OutPut=OutPut ^ 1234;  //means1234 h  
    
    cout<<OutPut<<"\n";   // decimal output by default
    cout<<hex<<OutPut;    // Hex output
    
    return 0;
    }
    Wednesday, September 14, 2011 9:40 AM
  • Thanks Zain_Ali Got it.

    for each(wchar_t ^ each_char in string)

    {

    int dec_value = System::Convert::ToInt32(each_char);
    String ^ hex_value = System::String::Format("{0:X2}",dec_value);

    }

    hex_value is the converted hex of each character.

    Wednesday, September 14, 2011 9:45 AM
  • What you told looks like C# code.

    This is a C# forum.
    Wednesday, September 14, 2011 10:01 AM
  • Thank you all for your time and effort!

    Hi Karthik,
    Welcome to the MSDN forum!

    You are more likely to get in touch with more VC++ experts in the Visual C++ forum.
    Thank you for your understanding!

    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    • Edited by Dummy yoyo Sunday, September 18, 2011 1:02 PM
    Sunday, September 18, 2011 1:00 PM