Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, February 19, 2010 2:48 PM
Hello, how to convert integer to binary
example:
1 to 0001
2 to 0010
or 11 >00010001
All replies (10)
Friday, February 19, 2010 3:26 PM âś…Answered | 3 votes
Integer, and everything else for that matter, is already in binary. You want to see the string representation of the binary you can do this:
Dim i As Integer = 170
Dim s As String = Convert.ToString(i, 2).PadLeft(32, "0"c) '32 bits
Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
Friday, February 19, 2010 3:15 PM
Hi Lasha,
how about ...
Public Shared Function IntToBin(ByVal intValue As Integer) As String
Dim sb As New System.Text.StringBuilder
While intValue <> 0
sb.Insert(0, (intValue Mod 2).ToString())
intValue \= 2
End While
Return sb.ToString()
End Function
Cheers,
Olaf
http://blogs.intuidev.com
Friday, February 19, 2010 3:32 PM
Hi dbasnett,
pretty cool - didn't know that Convert() offers base-conversion. Cheers,
Olaf
http://blogs.intuidev.com
Friday, February 19, 2010 4:18 PM
2,8,16Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
Friday, February 19, 2010 5:56 PM
Thanks
Friday, February 19, 2010 6:35 PM
Thanks
you mark as answered please...thanksJust Be Humble Malange!
Friday, February 19, 2010 9:50 PM
You can use the methods of the BitConverter class to get the byte values for any base type:
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx
Monday, February 22, 2010 8:55 AM
Hello lasha,
Welcome to MSDN Forum!
I understand your problem, the following is the code,I have debugged. I hope this could help you. When you run this programme, please drag&drop Button1,TextBox1 and TextBox2 onto Form1 first.
Public Class Form1
Public Shared Function IntToBin(ByVal intValue As Integer) As String
Dim sb As New System.Text.StringBuilder
Dim tempstr As String
If intValue = 0 Then
Return "0000"
End If
While intValue <> 0
sb.Insert(0, (intValue Mod 2).ToString())
intValue \ 2
End While
tempstr=sb.ToString()
While Len(tempstr) <> 4
tempstr = CStr("0") + CStr(tempstr)
End While
Return tempstr
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Text1 contains the integer number. Text2 shows the binary result
Dim intValue As Integer
Dim intValue1 As Integer
Dim intValue2 As Integer
Dim temp As String
temp = ""
intValue = TextBox1.Text
If (intValue <= 9) Then
TextBox2.Text = IntToBin(Val(intValue))
Else
While intValue <> 0
intValue1 = intValue Mod 10
intValue2 = intValue \ 10
temp = CStr(IntToBin(Val(intValue1))) + CStr(temp)
intValue = intValue2
End While
TextBox2.Text = temp
End If
End Sub
End Class
If you still have any problems, please feel free to contact me.
Best regards!
Liliane Teng
Wednesday, January 22, 2020 9:14 AM
Hey there bakaroo, theres a little code i made on the side, its basically done even tho you could fine tune it if you paid some effort, hope it works out for ya
Public Class Form1
Function Dec->Bin(ByVal des As Integer)
Dim a As Integer
Dim b As Integer
Dim number As Integer = 0
Dim c As Integer
Dim result As String = """"
a = des
Do
c = a Mod 2
a = a / 2
number += 1
If c = 0 Then
result = "0" + result
Else
result = "1" + result
End If
Loop While a > 0
number = 8 - (number Mod 8)
For i = 1 To number
result = "0" + result
Next
Return result
End Function
Wednesday, January 22, 2020 1:02 PM
Hey there bakaroo, theres a little code i made on the side, its basically done even tho you could fine tune it if you paid some effort, hope it works out for ya
Just in case you hadn't noticed, the thread you replied to is ten (10) years
old. I expect (hope) the OP has moved on to other tasks by now. ;-)
- Wayne