locked
Hex leading zero; Is there a better way RRS feed

  • Question

  • I am converting an integer to hex using the ToString('X') method.

    The problem is if the number of hex digits is odd there is no leading zero

    ie the output produced is "A2E" and I want "0A2E".

     

    I resorted to this:

    hexString = intVar.ToString('X');
    if (hexString.Length % 2 > 0)
    hexString = '0' + hexString;

    Is there a better way?

    The number of hex digits that are produced is unknown before hand.

     

    James Johnson

     

    Wednesday, March 24, 2010 12:56 AM

Answers

  • hexString = intVar.ToString('X');
    hexString = (hexString.Length % 2 == 0 ? "" : "0") + hexString;

    • Proposed as answer by Sebastian Brand Wednesday, March 24, 2010 5:13 AM
    • Marked as answer by Harry Zhu Thursday, April 1, 2010 3:10 AM
    Wednesday, March 24, 2010 2:54 AM

All replies

  • hexString = String.Format( "{0:X2}", intValue);

    Also see here


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com

    Wednesday, March 24, 2010 1:08 AM
  • Sorry, no cigar.

    with intValue = 1230

    hexString = String.Format( "{0:X2}", intValue);

    produces "4CE".

     

    James Johnson

    Wednesday, March 24, 2010 1:33 AM
  • Bro, the hex for 1230 is 4CE, what is the dealio with having it "even"?

    http://www.xor.co.uk/hex.html


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, March 24, 2010 1:43 AM
  • hexString = intVar.ToString('X');
    hexString = (hexString.Length % 2 == 0 ? "" : "0") + hexString;

    • Proposed as answer by Sebastian Brand Wednesday, March 24, 2010 5:13 AM
    • Marked as answer by Harry Zhu Thursday, April 1, 2010 3:10 AM
    Wednesday, March 24, 2010 2:54 AM
  •  

    hexString = intVar.ToString('X');
    
    hexString = (hexString.Length % 2 == 0 ? "" : "0") + hexString;
    
    

     


    That is not really different then what the user had before. I was just trying to figure out why he needed a zero when both his method and the one I presented were already accurate.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Edited by JohnGrove Tuesday, March 30, 2010 1:01 PM
    Wednesday, March 24, 2010 3:54 AM
  • Hi John,

    It seems the OP wants to format the string, I think BrunoLM7  has provide another way to achieve that. and it looks like a better way :)

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, March 30, 2010 2:39 AM
  • Bro, the hex for 1230 is 4CE, what is the dealio with having it "even"?

    http://www.xor.co.uk/hex.html


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com

    It seems the OP want the legth of the string to be even, so a '0' is left padded to it.

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, March 31, 2010 6:09 AM
  • Just specify ..ToString("X4") 4 becomes the precsion specifier so you will get a leading zero. If 32bit integer it will ignore the limit and leading zeros so better to use X8.
    Friday, May 20, 2011 3:14 PM