none
Pad a hex Number with Leading Zeros RRS feed

  • Question

  • Hi,

    I want to get 2digit hexadecimal of input number as you can see here:

            Dim sp As String = Hex(14)
            MsgBox(String.Format("{0:00}", sp))

    However, instead of printing "0E" it shows me "E" , what is my mistake?

    Wednesday, April 11, 2018 6:44 PM

All replies

  • Hi

    Try this (I used 2,but if you want a different string length then adjust as needed)

        Dim sp As String = Hex(14)
        MessageBox.Show(sp.PadLeft(2, "0"c))
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by IronRazerz Wednesday, April 11, 2018 7:26 PM
    Wednesday, April 11, 2018 7:22 PM
  •         Dim sp As Integer = 14
            Dim s As String = sp.ToString("X").PadLeft(2, "0"c)
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    • Proposed as answer by IronRazerz Wednesday, April 11, 2018 7:26 PM
    Wednesday, April 11, 2018 7:23 PM
  • You can also format the original number:

       Dim number As Integer = 14

       MsgBox(String.Format("{0:X2}", number))

     

    or

       MsgBox(number.ToString("X2"))

    • Edited by Viorel_MVP Wednesday, April 11, 2018 9:09 PM
    • Proposed as answer by Mr. Monkeyboy Wednesday, April 11, 2018 9:18 PM
    Wednesday, April 11, 2018 9:08 PM
  • I'll guess all three answers are correct.

    La vida loca

    Wednesday, April 11, 2018 9:18 PM