Convert string or base10 to base 2 in VB

I am looking for a way to get a value from a text box and convert it to base number 2 with 8 digits.
So if they print text box 2, it will answer 00000010. or if they printed 255 11111111. etc ...
is there any way to do this.

Dim prVal As Integer

prVal = PrefixTxt.Text
+3
source share
2 answers

Use the method Convert.ToStringand specify the base as 2. This converts the value Integerto Stringin the specified base

Dim result = Convert.ToString(Integer.Parse(prVal), 2)

As @Dan noted, if you want to make it be 8 wide, use the method PadLeft

result = result.PadLeft(8, "0"c)
+6
source
Convert.ToString(Integer.Parse(prVal), 2).PadLeft(8, '0')
+2
source

All Articles