Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a String or base10 to a base 2 in VB [duplicate]

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
like image 323
user2569803 Avatar asked Dec 05 '25 16:12

user2569803


1 Answers

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)
like image 129
JaredPar Avatar answered Dec 08 '25 13:12

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!