Text to binary

G

Gary D.

Can anyone point me in the direction of a resource for converting standard
ASCII text into binary?

I am using Access2003. I already have VBA routines for converting decimal
numbers in binary, now I want to convert text (for very simple encryption
purposes).

Thanks
 
J

John Nurick

You could just use Asc() or AscW() to convert each character of text
into a number, and then use your existing routine on these numbers.
 
G

Gary D.

You could just use Asc() or AscW() to convert each character of text
into a number, and then use your existing routine on these numbers.

Yes, but it doesn't give very clean results for non-alphanumceirc characters,
at least not the way I currently do it.
 
J

John Nurick

Yes, but it doesn't give very clean results for non-alphanumceirc characters,
at least not the way I currently do it.

I have no idea what you mean by "clean results". Asc() returns an
integer between 0 and 255, which fits into 8 binary digits.

AscW() is a bit messier because it seems that for some Unicode
characters it returns a negative integer (there being VBA not having
unsigned integer types). But if I understand it right you can use
something like this

Function AscWAsLong(S As String) As Long
Dim L As Long
L = AscW(S)
If L < 0 Then L = 65536 + L
AscWAsLong = L
End Function

to return a Long containing an integer between 0 and 65535, which fits
into 16 binary digits.
 
G

Gary D.

I have no idea what you mean by "clean results". Asc() returns an
integer between 0 and 255, which fits into 8 binary digits.

AscW() is a bit messier because it seems that for some Unicode
characters it returns a negative integer (there being VBA not having
unsigned integer types). But if I understand it right you can use
something like this

Function AscWAsLong(S As String) As Long
Dim L As Long
L = AscW(S)
If L < 0 Then L = 65536 + L
AscWAsLong = L
End Function

to return a Long containing an integer between 0 and 65535, which fits
into 16 binary digits.

Thanks for the info John. I'll have to get my head around that later.
Ideally, I wanted to translate text into binary in the sense that it would
produce something like a blob, that is, return a non-text type of value.
I don't know if that's even possible with VBA.
 
J

John Nurick

Thanks for the info John. I'll have to get my head around that later.
Ideally, I wanted to translate text into binary in the sense that it would
produce something like a blob, that is, return a non-text type of value.
I don't know if that's even possible with VBA.

I took your mention of "binary" to mean converting (say) 9 to 00001001
and so on.

If you want to obscure your text but don't need to protect it against
any real decryption effort, consider using a (bidirectional) function
like this:

Function ROT128(ByVal S As String) As String
Dim j As Long

For j = 1 To Len(S)
Mid(S, j, 1) = Chr((Asc(Mid(S, j, 1)) + 128) Mod 256)
Next
ROT128 = S
End Function

Otherwise, a web search will probably find some encryption algorithms
implemented in VB(A).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top