Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you convert a string to binary numbers?

Status
Not open for further replies.

vbkris

Programmer
Jan 20, 2003
5,994
0
0
IN
is there any function to convert a string say "This is a string" to binary code?
 
I don't know of any intrinsic function that will do that directly.

There have been several posts showing how to display an Integer or Byte value to a string representation of binary, so just loop through your string, use the Asc() function to convert each character to a byte value, then do the binary string conversion.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
i want to avoid the Asc() function. i want the string to be inserted into a text file in computer languae so that no one can understand it.
 
You say <i want to avoid the Asc() function>

Is there some reason to avoid it?

This is the VB intrinsic function to convert a string to its ASCII (numeric) value. The ASCII value is how the string is stored internally, which I think is what you asked. The conversion from decimal to binary (as I said earlier) needs to be done separately.

Normally the only reason that you want to avoid the use of a standard intrinsic function would be for a homework assignment, or some sort of trick question.

If I have misunderstood the problem, please clarify
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 

So what you are trying to say is you want to convert a string &quot;This is a test.&quot; to its equalivant in binary string format so that all you see in the text file is ones (1) and zeros (0)?

Confused!

 
I think what you are really looking for is a way to encrypt a string. here is some code that will do that. Just encrypt the string with a decent length key and then decrypt it to get the original value back

Private Sub Command1_Click()
Dim t2 As String
Dim t3 As String
Dim Key As String

Key = &quot;My Encryption Key&quot;
'to encrypt it
t2 = crypt(&quot;Some text you want to encrypt.&quot;, Key)

'to decrypt it
t3 = crypt(t2, Key)

't2 is the encrypted data
't3 is the decripted data
'key is the encryption key

End Sub

Function crypt(a As String, b As String) As String
'a gets encrypted with b and the value is returned
Dim Val1 As Integer
Dim Val2 As Integer
Dim i As Integer
Dim j As Integer
Dim OutVal As Integer
Dim OutString As String

For i = 1 To Len(a)
Val1 = Asc(Mid$(a, i, 1))
If i > Len(b) Then
j = i Mod Len(b) + 1
Val2 = Asc(Mid$(b, j, 1))
Else
Val2 = Asc(Mid$(b, i, 1))
End If

OutVal = Val1 Xor Val2
OutString = OutString & Chr$(OutVal)

Next i
crypt = OutString

End Function
 
yep vb5prgrmr
i want to convert a string &quot;This is a test.&quot; to its equalivant in binary string format so that all you see in the text file is ones (1) and zeros (0).
 
unless I am totally missing something strings are stored as their ascii values in binary. Unless you encrypt the string its always easy to view the text. If you want to hide the value of a string from users encrypt it. Otherwise it will always be easy to pull the text out of the file. If you're wanting to store a string as a string of binary values that's just more work than you need to go through.. and pretty easy to convert back to the string anyway.

I recommend just encrypting the string and saving the encrypted string. Then to crack your system/decode your text they would have to find the encryption key in your compiled code/database. Still possible but a lot harder.
 

Ok, I really cannot take credit for this code (nor do I really want to) but I pieced it together from several sources. Namely this site and PSC. This is also probally not the fastest code either but it will do exactly what you want.
[tt]
Option Explicit

Private Sub Form_Load()
Dim S As String, B() As Byte, OutString As String
Dim C(1 To 8) As String, UpperLimit As Long, L As Long
Dim I As Integer
S = &quot;This is a test.&quot;
B = StrConv(S, vbFromUnicode)
UpperLimit = UBound(B)
For L = 0 To UpperLimit

C(1) = IIf(B(L) And &H1, &quot;1&quot;, &quot;0&quot;)
C(2) = IIf(B(L) And &H2, &quot;1&quot;, &quot;0&quot;)
C(3) = IIf(B(L) And &H4, &quot;1&quot;, &quot;0&quot;)
C(4) = IIf(B(L) And &H8, &quot;1&quot;, &quot;0&quot;)
C(5) = IIf(B(L) And &H10, &quot;1&quot;, &quot;0&quot;)
C(6) = IIf(B(L) And &H20, &quot;1&quot;, &quot;0&quot;)
C(7) = IIf(B(L) And &H40, &quot;1&quot;, &quot;0&quot;)
C(8) = IIf(B(L) And &H80, &quot;1&quot;, &quot;0&quot;)

For I = 8 To 1 Step -1
OutString = OutString & C(I)
Next I

Next L

MsgBox OutString

End Sub
[/tt]

Good Luck

 
You will have to convert the text string to it's ascii value than to it's binary value.

&quot; this is a test &quot; = 8 bits for 1 byte per character

In binary:
this _01110100 01101000 01101001 01110011
is _01101001 01110011
a _01100001
test _01110100 01100101 01110011 01110100

If your thinking of creating an encryption for text to binary..it will be enormous but unique.
 
O.K so i have converted it, how do u reconvert it back vb5prgrmr?
 
It looks pretty straightforward.
Code:
Dim strInput As String
Dim strOutput As String
Dim strGroup As String
Dim lngChar As Long
Dim tmpVal As Long
Dim lngCount As Long

'Split strinput back into byte-sized chunks
For lngChar = 1 To Len(strInput) Step 8
 strGroup = Mid(strInput, lngChar, 8)
 MsgBox strGroup
 'strGroup now contains 01110001 or whatever
 'then loop through each group
 tmpVal = 0
  For lngCount = 1 To 8
   tmpVal = tmpVal + Val(Mid(strGroup, lngCount, 1)) * 2 ^ (8 - lngCount)
Next lngCount
 strOutput = strOutput & Chr(tmpVal)
Next lngChar
MsgBox strOutput


It 's not error checked, nor is it intended as production code but it will get you started
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top