byte conversion

  • Thread starter Thread starter Randy B
  • Start date Start date
R

Randy B

Given a decimal value, 0-255,
I need to display the byte equivalent.
(as an 8 character string)

then given: 127
return: 01111111

I do not see that an access function
exists.
Does anyone have a simple solution
in VBA before I re-invent the wheel?

thanx in advance
rlb
 
Given a decimal value, 0-255,
I need to display the byte equivalent.
(as an 8 character string)

Public Function Bin(SomeValue As Integer) As String

If SomeValue > 1 Then
Bin = Bin(SomeValue \ 2) & (SomeValue And 1)
Else
Bin = SomeValue And 1
End If
End Function

If you want to pad it with zeroes, you can use

Debug.Print Right$(String(8,"0") & Bin$(127), 8)

You need to trap negative numbers separately.

HTH


Tim F
 
Given a decimal value, 0-255,
I need to display the byte equivalent.
(as an 8 character string)

then given: 127
return: 01111111

I do not see that an access function
exists.
Does anyone have a simple solution
in VBA before I re-invent the wheel?

Hrm. I don't know of a builtin function either but how about:

Public Function Bits(iIn As Byte) As String
Dim iP As Integer
Bits = ""
For iP = 1 to 8
Bits = CStr(iIn MOD 2) & Bits
iIn = iIn \ 2
Next iP
End Function
 
very well done John and Tim.
A recursive solution as well!!

thanks for this and all your efforts....
rlb
 
Back
Top