letters to binary!

translate one letter to a binary text

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 6960 Views - Last Post: 18 April 2012 - 12:59 AM Rate Topic: -----

#1 Dr.No_0B[SwE]   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 22-January 09

letters to binary!

Post icon  Posted 25 January 2009 - 05:14 AM

Hi i have done a text-to-binary-translator but it only translate one letter at the time, and i want to write a whole text and then translate it to binary :^:

the code now is:

Dim keyin As Integer
		keyin = Val(TextBox1.Text)

		If TextBox1.Text = "a" Then
			TextBox2.Text = 1100001
		End If


etc.etc

it only translate the letter "a" to 1100001

i waqnt to write like "alex" and it would be like
1100001110110011001011111000

any suggestions would be helpfull

MVH Dr.No_0B[SwE]

Is This A Good Question/Topic? 0
  • +

Replies To: letters to binary!

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: letters to binary!

Posted 25 January 2009 - 09:24 AM

You can convert the value in the TextBox to a byte array, then loop through it, something like this

Public Function ToBinary(ByVal str As String) As String
	Dim converted As String = String.Empty
	Dim byteArray As Byte() = System.Text.Encoding.ASCII.GetBytes(str)

	'create a memory stream
	Dim stream As New MemoryStream()

	'create BinaryWriter based on our MemoryStream
	Dim writer As New BinaryWriter(stream)

	Try
		'write out in binary
		writer.Write(byteArray)
	Catch ex As Exception
		Return ex.Message
	End Try
	For i As Integer = 0 To byteArray.Length - 1

		'now we get to start the conversion: Loop through each
		'byte in the byte array
		For j As Integer = 0 To 7
			converted += IIf((byteArray(i) And 128) > 0,"1","0")
			byteArray(i) <<= 1
		Next
	Next

	Return converted
End Function



Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#3 Dr.No_0B[SwE]   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 22-January 09

Re: letters to binary!

Posted 25 January 2009 - 10:06 AM

thx for the tip :^: but it didn't work :v:

i gonna try some more and see where it goes :)

i learned something, thats the point :^:
(and to get it to work :P )
Was This Post Helpful? 0
  • +
  • -

#4 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 121
  • View blog
  • Posts: 666
  • Joined: 01-October 08

Re: letters to binary!

Posted 25 January 2009 - 10:20 AM

		Debug.WriteLine("")
		Dim s As String = "Alex"
		Dim bin As String = ""
		Dim bbin As String = ""
		For Each ch As Char In s
			bbin &= Convert.ToString(Convert.ToByte(ch), 2).PadLeft(8, "0"c)
			bin = Convert.ToString(Convert.ToByte(ch), 2).PadLeft(8, "0"c)
			Debug.Write(ch & " ")
			Debug.WriteLine(bin)
		Next
		Debug.WriteLine(bbin)
		'debug output
		'A 01000001
		'l 01101100
		'e 01100101
		'x 01111000
		'01000001011011000110010101111000
 

Was This Post Helpful? 0
  • +
  • -

#5 Dr.No_0B[SwE]   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 22-January 09

Re: letters to binary!

Posted 25 January 2009 - 10:42 AM

thx mate, :^: but it didn't work :v:

its maybe i that place the code on the wrong place but i tried everywhere...
Was This Post Helpful? 0
  • +
  • -

#6 ZRonZ   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 207
  • Joined: 09-January 09

Re: letters to binary!

Posted 25 January 2009 - 12:55 PM

Quote

' date='25 Jan, 2009 - 09:42 AM' post='522404']
thx mate, :^: but it didn't work :v:

its maybe i that place the code on the wrong place but i tried everywhere...


I'm a bonifide beginner and it worked first try for me - are you sure you're using VB.NET? Do you know how to display a variable?

Sincerely,
0101101001010010011011110110111001011010
Was This Post Helpful? 0
  • +
  • -

#7 Dr.No_0B[SwE]   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 22-January 09

Re: letters to binary!

Posted 25 January 2009 - 01:35 PM

View PostZRonZ, on 25 Jan, 2009 - 11:55 AM, said:

Quote

' date='25 Jan, 2009 - 09:42 AM' post='522404']
thx mate, :^: but it didn't work :v:

its maybe i that place the code on the wrong place but i tried everywhere...


I'm a bonifide beginner and it worked first try for me - are you sure you're using VB.NET? Do you know how to display a variable?

Sincerely,
0101101001010010011011110110111001011010


im not 100% sure on the variables but VB.net im 100% sure :^:

01000100011100100010111001001110011011110101111100110000010000100101101101010011011101110100010101011101
Was This Post Helpful? 0
  • +
  • -

#8 freakz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-January 09

Re: letters to binary!

Posted 25 January 2009 - 04:45 PM

i'm not sure how to do this so some reseach would have to be used, one way of doing this possibly is to get your string, and take each character in turn into a variable char...

you should then be able to take the char as a number(dec)... all you would have to do is take the number and convert it into binary, and add '0' at the beginning to make sure the length is 8 for ascii code...

here is a function i just wrote to convert dec to binary! xD
Private Function dectobinary(ByVal number As Integer)
		Dim division As Integer
		Dim current As Integer = number
		Dim numbers As String
		numbers = ""

		While current > 0
			division = current / 2
			If division + division = current Then
				numbers = "0" & numbers
			Else
				numbers = "1" & numbers
			End If

			'catch lif for an random error that occured - this can never happen
			If division + division > current Then
				division -= 1
			End If

			current = division

		End While
		Return (numbers)

	End Function


01001001011101000010000001110111011000010111001100100000011100100110000101110100011010000110010101110010001000000110100101101110011101000110010101110010011001010111001101110100011010010110111001100111001000000111010001101111001000000110001101101111011001000110010100100000011101000110100001101001011100110010000001110000011100100110111101100111011100100110000101101101001011100010111000101110001000000100010001101111011011100110010100100000011000100111100100100000011001100111001001100101011000010110101101111010

This post has been edited by freakz: 25 January 2009 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

#9 ZRonZ   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 207
  • Joined: 09-January 09

Re: letters to binary!

Posted 25 January 2009 - 07:28 PM

A quick google search revealed that there are a number of solutions to this question available. It's really simple.

The way you did the first letter was one "correct" method but to get the second letter you just need a simple loop to go through all the letters. But, you coded the translation from the letter to the binary. You also would have needed to code all of the conversions to make it work with the loop. Therefore the loop added to your initial code would have only picked up and converted the "A".

The following example that dbasnett gave earlier clearly shows that there is a conversion method within VB.NET to make it really easy. A little research and you can probably come up with a method to reverse the procedure and convert from binary to characters.



	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Debug.WriteLine("")
		Dim s As String = "GoogleMe"
		Dim bin As String = ""
		Dim bbin As String = ""
		For Each ch As Char In s
			bbin &= Convert.ToString(Convert.ToByte(ch), 2).PadLeft(8, "0"c)
			bin = Convert.ToString(Convert.ToByte(ch), 2).PadLeft(8, "0"c)
			Debug.Write(ch & " ")
			Debug.WriteLine(bin)
		Next
		Debug.WriteLine(bbin)
		TextBox1.Text = bbin
			  End Sub



The result from this code will: 0100011101101111011011110110011101101100011001010100110101100101

Z

This post has been edited by ZRonZ: 25 January 2009 - 07:36 PM

Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: letters to binary!

Posted 25 January 2009 - 10:40 PM

Here are two simple functions, one will convert ASCII to binary, and one will convert binary back to ASCII. First for converting ASCII to binary, we will convert the ASCII string to a byte array then loop through each byte. We will use a StringBuilder and use the Append method (faster than using a string and adding to it)

Public Function ConvertToBinary(ByVal str As String) As String
	Dim converted As New StringBuilder
	For Each b As Byte In ASCIIEncoding.ASCII.GetBytes(str)
		converted.Append(Convert.ToString(b, 2).PadLeft(8, "0"))
	Next
	Return converted.ToString()
End Function



Now for converting binary to ASCII, here we will retrieve the original byte array by using Convert.ToByte on each character in the string. We will then use GetString() to convert the byte array back into it's original ASCII value

Public Function ConvertToAscii(ByVal str As String) As String
	Dim chars As String = Regex.Replace(str, "[^01]", "")
	Dim arr((chars.Length / 8) - 1) As Byte
	For i As Integer = 0 To arr.Length - 1
		arr(i) = Convert.ToByte(chars.Substring(i * 8, 8), 2)
	Next
	Return ASCIIEncoding.ASCII.GetString(arr)
End Function



Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#11 freakz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-January 09

Re: letters to binary!

Posted 26 January 2009 - 04:46 AM

PsychoCoder you code is shorter than mine =] lol
Was This Post Helpful? 0
  • +
  • -

#12 Dr.No_0B[SwE]   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 22-January 09

Re: letters to binary!

Posted 26 January 2009 - 06:02 AM

THX to all of you, this really helped me :^:

im just a beginner on vb.net and vbee08 but it's almost the same language so..
:P
Was This Post Helpful? 0
  • +
  • -

#13 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 121
  • View blog
  • Posts: 666
  • Joined: 01-October 08

Re: letters to binary!

Posted 26 January 2009 - 07:39 AM

		'Dim keyin As Integer
		'keyin = Val(TextBox1.Text)

		'If TextBox1.Text = "a" Then
		'	TextBox2.Text = 1100001
		'End If
		TextBox1.Text = "dbasnett" 'test
		Debug.WriteLine("") 'test
		Dim bin As String 'for testing
		'convert characters to  the binary string representation
		Dim s As New System.Text.StringBuilder
		For Each ch As Char In TextBox1.Text
			s.Append(Convert.ToString(Convert.ToByte(ch), 2).PadLeft(8, "0"c))
			bin = Convert.ToString(Convert.ToByte(ch), 2).PadLeft(8, "0"c) 'test
			Debug.Write("'" & ch & " ") 'test
			Debug.WriteLine(bin) 'test
		Next
		TextBox2.Text = s.ToString
		Debug.WriteLine("'" & TextBox2.Text) 'test
		'debug output
		'd 01100100
		'b 01100010
		'a 01100001
		's 01110011
		'n 01101110
		'e 01100101
		't 01110100
		't 01110100
		'0110010001100010011000010111001101101110011001010111010001110100

		'convert binary back to string
		Dim b((TextBox2.TextLength - 1) \ 8) As Byte
		Dim y As Integer = 0, i As Integer = 7
		For x As Integer = 0 To TextBox2.TextLength - 1
			If TextBox2.Text.Substring(x, 1) = "1" Then 'look at one and zero
				b(y) = b(y) Or CByte(2 ^ i) 'if one turn it on in byte
			End If
			i -= 1 'decrement bit counter
			If i < 0 Then 'less than 0
				i = 7 'yes, set bit counter 
				y += 1 'increment byte pointer
			End If
		Next
		Debug.WriteLine(System.Text.Encoding.ASCII.GetChars(b))
 

Was This Post Helpful? 0
  • +
  • -

#14 vzybilly   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 160
  • Joined: 24-September 08

Re: letters to binary!

Posted 26 January 2009 - 11:52 AM

View PostPsychoCoder, on 25 Jan, 2009 - 09:40 PM, said:

Public Function ConvertToBinary(ByVal str As String) As String
	Dim converted As New StringBuilder
	For Each b As Byte In ASCIIEncoding.ASCII.GetBytes(str)
		converted.Append(Convert.ToString(b, 2).PadLeft(8, "0"))
	Next
	Return converted.ToString()
End Function


Public Function ConvertToAscii(ByVal str As String) As String
	Dim chars As String = Regex.Replace(str, "[^01]", "")
	Dim arr((chars.Length / 8) - 1) As Byte
	For i As Integer = 0 To arr.Length - 1
		arr(i) = Convert.ToByte(chars.Substring(i * 8, 8), 2)
	Next
	Return ASCIIEncoding.ASCII.GetString(arr)
End Function



i'm having issues with that code, i currently have...
Public Class Binary_Fun

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		'ASCII > BINARY
		'txtbox1 > txtbox2
		Me.TextBox2.Text = CTA(Me.TextBox1.Text)
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		'BINARY > ASCII
		'txtbox2 > txtbox1
		Me.TextBox1.Text = CTB(Me.TextBox2.Text)
	End Sub

	Public Function CTA(ByVal str As String) As String
		Dim chars As String = Regex.Replace(str, "[^01]", "")
		Dim arr((chars.Length / 8) - 1) As Byte
		For i As Integer = 0 To arr.Length - 1
			arr(i) = Convert.ToByte(chars.Substring(i * 8, 8), 2)
		Next
		Return ASCIIEncoding.ASCII.GetString(arr)
	End Function

	Public Function CTB(ByVal str As String) As String
		Dim converted As New StringBuilder
		For Each b As Byte In ASCIIEncoding.ASCII.GetBytes(str)
			converted.Append(Convert.ToString(b, 2).PadLeft(8, "0"))
		Next
		Return converted.ToString()
	End Function
End Class


my errors are...

VB said:

Error 1 Name 'Regex' is not declared. L:\Programming\thingy\Binary_Fun.vb 16 31 thingy
Error 2 Name 'ASCIIEncoding' is not declared. L:\Programming\thingy\Binary_Fun.vb 21 16 thingy
Error 3 Type 'StringBuilder' is not defined. L:\Programming\thingy\Binary_Fun.vb 25 30 thingy
Error 4 Name 'ASCIIEncoding' is not declared. L:\Programming\thingy\Binary_Fun.vb 26 31 thingy

Was This Post Helpful? 0
  • +
  • -

#15 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: letters to binary!

Posted 26 January 2009 - 11:54 AM

Add a reference to System.Text and System.Text.RegularExpressions, sorry I didn't add that to my post :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2