Binary

  • Thread starter Thread starter javiernews via AccessMonster.com
  • Start date Start date
J

javiernews via AccessMonster.com

Hi,

Is it any buitl in function to convert any caracter to his equivalent in bits
(ceros & ones): ??

Example:

A = 01000001

Thnak you !
 
Not built in.

There is a built-in Hex function that will convert decimal values to Hex
values. You can then write your own function to convert the Hex values 0 to
F to the equivalent binary 0000 to 1111.
 
Thank you for your prompt replay !
But

Ok for hexadecimal numbers,.........
but I need something for binary numbers.

And I would need something similar to a binary number like:

Msgbox ???? (01000001) ' Returns A

or

MsgBox ???? ("A") ' Returns 01000001


Does this kind of function exits in Access ??

Thank You !
 
No, as I already said, there isn't such function built into Access.

However, it's fairly easy to write your own function.

You can use Asc to convert the symbol to decimal.

You can use Hex to convert the decimal representation to hex.

You can write a function to convert the hex to binary.

Something along the lines of:

Function CharBinary(Character As String) As String
Dim intLoop As Integer
Dim strInBinary As String
Dim strInHex As String

strInBinary = vbNullString
strInHex = Hex(Asc(Character))
For intLoop = 1 To Len(strInHex)
Select Case Mid(strInHex, intLoop, 1)
Case "0"
strInBinary = strInBinary & "0000"
Case "1"
strInBinary = strInBinary & "0001"
Case "2"
strInBinary = strInBinary & "0010"
Case "3"
strInBinary = strInBinary & "0011"
Case "4"
strInBinary = strInBinary & "0100"
Case "5"
strInBinary = strInBinary & "0101"
Case "6"
strInBinary = strInBinary & "0110"
Case "7"
strInBinary = strInBinary & "0111"
Case "8"
strInBinary = strInBinary & "1000"
Case "9"
strInBinary = strInBinary & "1001"
Case "A"
strInBinary = strInBinary & "1010"
Case "B"
strInBinary = strInBinary & "1011"
Case "C"
strInBinary = strInBinary & "1100"
Case "D"
strInBinary = strInBinary & "1101"
Case "E"
strInBinary = strInBinary & "1110"
Case "F"
strInBinary = strInBinary & "1111"
Case Else
End Select
Next intLoop

CharBinary = strInBinary

End Function

Now, CharBinary("A") will return "01000001"

I'll leave writing the inverse to you.
 
Is it any buitl in function to convert any caracter to his equivalent
in bits (ceros & ones): ??

debug.print LongToBinary(Asc("A"))
01000001


Public Function LongToBinary(SomeValue As Long) As String

If SomeValue = 0 Then
LongToBinary = "0"

ElseIf (SomeValue And 1) Then
LongToBinary = LongToBinary(SomeValue \ 2) & "1"

Else
LongToBinary = LongToBinary(SomeValue \ 2) & "0"

End If

End Function


Hope that helps


Tim F
 
Hi Tim,

Great !! Very Good and very short code !!

THANK YOU !!!
Javier
 
Doug,

I am trying to convert a binary number to hex. I used your code below,
switching the references between strInBinary and strInHex and adjusting the
Select Case statement values. How do I change the following line to account
for a binary number since there is no built-in binary conversion function in
Access?

strInBinary = Hex(Asc(Character)) '(fifth line in function)

Thanks,
Melanie

Douglas J. Steele said:
No, as I already said, there isn't such function built into Access.

However, it's fairly easy to write your own function.

You can use Asc to convert the symbol to decimal.

You can use Hex to convert the decimal representation to hex.

You can write a function to convert the hex to binary.

Something along the lines of:

Function CharBinary(Character As String) As String
Dim intLoop As Integer
Dim strInBinary As String
Dim strInHex As String

strInBinary = vbNullString
strInHex = Hex(Asc(Character))
For intLoop = 1 To Len(strInHex)
Select Case Mid(strInHex, intLoop, 1)
Case "0"
strInBinary = strInBinary & "0000"
Case "1"
strInBinary = strInBinary & "0001"
Case "2"
strInBinary = strInBinary & "0010"
Case "3"
strInBinary = strInBinary & "0011"
Case "4"
strInBinary = strInBinary & "0100"
Case "5"
strInBinary = strInBinary & "0101"
Case "6"
strInBinary = strInBinary & "0110"
Case "7"
strInBinary = strInBinary & "0111"
Case "8"
strInBinary = strInBinary & "1000"
Case "9"
strInBinary = strInBinary & "1001"
Case "A"
strInBinary = strInBinary & "1010"
Case "B"
strInBinary = strInBinary & "1011"
Case "C"
strInBinary = strInBinary & "1100"
Case "D"
strInBinary = strInBinary & "1101"
Case "E"
strInBinary = strInBinary & "1110"
Case "F"
strInBinary = strInBinary & "1111"
Case Else
End Select
Next intLoop

CharBinary = strInBinary

End Function

Now, CharBinary("A") will return "01000001"

I'll leave writing the inverse to you.
 
I am trying to convert a binary number to hex.

Public Function BinaryToLong(SomeBinary As String) As Long

If Len(SomeBinary) = 0 Then
BinaryToLong = 0

Else
BinaryToLong = _
BinaryToLong(Left(SomeBinary, Len(SomeBinary) - 1)) * 2 + _
Asc(Mid(SomeBinary, Len(SomeBinary), 1)) - Asc("0")

End If

End Function


Hope that helps


Tim F
 
Tim,

Thanks for responding. I tried out your function, and the result wasn't
quite what I expected. One of the results was '3825' when I was expecting
something closer to '9AE7FECA1C3AA8419036EE8A89113363'. Do I need to do
something else with the BinaryToLong result?

Thanks,
Melanie
 
Tim's code converts binary to long integer (i.e.: base 10). You'll then need
to convert that to Hex.

What was the value you passed to it? There's no way on earth something that
would result in that long a Hex representation could be stored in a Long
Integer!

To convert binary to hex, make sure that the number of bits is a multiple of
4 (prepend zeroes until it is) then look at each nibble (group of 4 bits).
In other words, if you've got 1011100110, you'd add two 0s in front to get
001011100110, split that into 0010 1110 0110 then convert each nibble to
hex. 0010 = 2, 1110 = E, 0110 = 6, so you'd end up with 1011100110 equals
2E6.
 
Doug,

Thanks for the clarification. The problem is that I cannot see what value
is being passed to the function. In SQL Server Enterprise Manager, the value
shows for every record as <binary>. When I link the tables into Access, the
values show up as something like ਀皨ц䦿æ¦ä²´â—ªâ¶­. So I was looking for a function
to convert the supposedly binary numbers to hex. Using the built-in Access
function Hex() only results in a #Error result. Perhaps I need to figure
this out in SQL. Thanks for the help.

Melanie
 
Back
Top