Search code examples
vb.nethexleading-zero

Preserve leading zeros hex vb


Doing some color operations, which include saving hex color values to variables. Having a problem preserving the leading zero for 1-16. Relevant code section:

        _coltable.Add("A", (Hex$(_color.A)))
        _coltable.Add("R", (Hex$(_color.R)))
        _coltable.Add("G", (Hex$(_color.G)))
        _coltable.Add("B", (Hex$(_color.B)))

This trims the LH spaces on numbers below 16. _coltable is dictionary(string,string). _color, a color object. Text below shows console out:

>> FUNCTION convert bg color to text and return
Recived vars: col: Color [A=255, R=0, G=0, B=160] | coltype: ARGB | mask: {A}{R}{G}{B} | format: HEX
FF{R}{G}{B}
FF0{G}{B}
FF00{B}
FF00A0
End of process. Output:FF00A0
> END BG color to text

Lines 3-6 show the iterative process - adds R and G as a single zero.

Tried various things with &s, Right and String.format. Can't get it running. Anyone got a magic wand?


Solution

  • Try

     _coltable.Add("A", _color.A.ToString("X2"))
    

    Where "X" denotes the hex format and "X2" specifies it to be two digits wide.