Convert int to Hex?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Convert int to Hex?

  1. #1
    Join Date
    Apr 2003
    Location
    Ireland
    Posts
    46

    Unhappy Convert int to Hex?

    Hi
    I simply want to convert an int into a hex value and assign it to a string

    int i = 64;
    string hexValue;

    In the example above I want to convert i into a Hex and assign it to hexValue?

    Should be simple but Im having some difficulty doing it. Any help appreciated


    Many thanks

  2. #2
    Join Date
    Nov 2003
    Posts
    8
    how about this?

    string str = System.Convert.ToString(integer, 16);

    JJ

  3. #3
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    263
    C# / vb.net has a great in-built namespace called System.URI , here's a quick example ...
    Code:
                string str = System.Uri.HexEscape((char)64);
                MessageBox.Show(str); // this will show as %40  (  which is the way hex would show in web urls  ) 
                // to show without the %  ,  the following line would apply  .  .  . 
                MessageBox.Show(str.Substring(1));
    string Signature = Censored;

  4. #4
    Join Date
    Apr 2003
    Location
    Ireland
    Posts
    46
    Thanks for the comments

    string str = System.Convert.ToString(integer, 16); - I couldnt get this to work as it gave me the wrong Hex value for the int I wanted to change for some reason

    I got

    string str = System.Uri.HexEscape((char)64);

    to work fine which is great, Im surprised its not that obvious to change an int to a hex

    Thanks again for the comments

    Keith

  5. #5
    Join Date
    Apr 2003
    Location
    Ireland
    Posts
    46

    Unhappy

    Actually this code only works for char up to 255 if I entered anything above that it throws an exception.

    string str = System.Uri.HexEscape((char)12625); //This does not work for example

    Is there anything else that I could do to just change an int to a Hex value

  6. #6
    Join Date
    Apr 2003
    Location
    Ireland
    Posts
    46
    Figured it out

    String xyz = 12625.ToString("X");

    Thanks again


    Keith

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center