3

I am looking for a way to take a value from a text box and convert it into a base 2 number with 8 digits.
So if they type in a text box 2 it would respond 00000010. or if they typed 255 11111111. etc...
is there any way to do this.

Dim prVal As Integer

prVal = PrefixTxt.Text
Jérôme Teisseire
  • 1,518
  • 1
  • 16
  • 26
user2569803
  • 637
  • 6
  • 11
  • 19

2 Answers2

6

Use the Convert.ToString method and specify the base as 2. This will convert an Integer value into a String in the specified base

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

As @Dan pointed out if you want to force this to be width 8 use the PadLeft method

result = result.PadLeft(8, "0"c)
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2
Convert.ToString(Integer.Parse(prVal), 2).PadLeft(8, '0')
Kaz-LA
  • 226
  • 1
  • 5