Hi how do i give an integer a binary value?

S

sohfeyr

-----Original Message-----
I would use 0x1010101010 in c, but how do I do it in vb? thanks!
.

Assuming that you need to use the format 0x1010101010
rather than the integer form 682 (I'd be curious to know
why?) and if that sort of thing is something you do
often, you might just want to use it as a String and
create a function (even in another template) that would
parse the string into an integer, I'm thinking something
like this:

Function bin2int(sBin As String)
Dim intForm As Integer: Set intForm = 0
binLen = Strings.Len(sBin)
For i = 1 To binLen - 2
power = 2 ^ i
intForm = intForm + power * CInt( _
Strings.Mid(String:=sBin, Start:=binLen - CLng
(i), Length:=1))
Next i
' MsgBox to evaluate output; remove or comment out when
done
MsgBox (sBin + " --> " + intForm)
bin2int = intForm
End Function

I haven't tested it, but it should be a good start on
such a function. Hope this helps!
 
J

Jonathan West

Alexandre said:
I would use 0x1010101010 in c, but how do I do it in vb? thanks!

Hi Alexandre,

I don't think there is a way of directly representing a binary number in
code in VBA, but you can use Hex, as it is easy to convert between Hax &
binary. The number you have there would be represented in VBA as &H2AA.
 

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