[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Convert bit string to a value?

Martin

4/10/2012 8:18:00 PM

How can I convert a string of 1's and 0's to an integer value?

For example, I want the numeric equivalent of "11100111".

Is there a function in VB6 that can do this? I tried INT and VAL but
neither did the job.

Thanks
43 Answers

Karl E. Peterson

4/10/2012 8:29:00 PM

0

After serious thinking Martin wrote :
> How can I convert a string of 1's and 0's to an integer value?
>
> For example, I want the numeric equivalent of "11100111".
>
> Is there a function in VB6 that can do this? I tried INT and VAL but
> neither did the job.

Nothing pre-built. You need to roll yer own, by looping through
something like this air-code:

For i = Len(bin) To 1 Step - 1
If Mid$(bin, i, 1) = "1" Then
n = n + 2^(Len(bin)-i)
End IF
Next i

You'll have to take special precautions with the sign-bit, depending
how you want to use the value.

--
..NET: It's About Trust!
http://vfre...


Jim Mack

4/10/2012 8:35:00 PM

0

> How can I convert a string of 1's and 0's to an integer value?
>
> For example, I want the numeric equivalent of "11100111".
>
> Is there a function in VB6 that can do this? I tried INT and VAL but
> neither did the job.

No native function, but it's just powers of 2. If you do it a lot I'd
recommend a precomuted array of the powers, but to illustrate (air
code):

Dim Byts() As Byte

BinStr$ = "1010111101"

Result = 0
Byts = StrConv(StrRev(BinStr), vbFromUnicode)
For Indx = 0 to UBound(Byts)
Result = Result + (Byts(Indx) And 1) * (2 ^ Indx)
Next

Limited to strings of 31 characters or less.

--
Jim


Mayayana

4/10/2012 8:46:00 PM

0

I've got something that I thought I originally
took from your Twiddle Bits code. Maybe not,
bu I know I didn't write it:

Public Function Bin2Dec(BinaryString As String) As Long
Dim X As Long
For X = 0 To Len(BinaryString) - 1
Bin2Dec = Bin2Dec + Val(Mid(BinaryString, _
Len(BinaryString) - X, 1)) * 2 ^ X
Next
End Function

--
--
"Karl E. Peterson" <karl@exmvps.org> wrote in message
news:jm256k$hin$1@dont-email.me...
| After serious thinking Martin wrote :
| > How can I convert a string of 1's and 0's to an integer value?
| >
| > For example, I want the numeric equivalent of "11100111".
| >
| > Is there a function in VB6 that can do this? I tried INT and VAL but
| > neither did the job.
|
| Nothing pre-built. You need to roll yer own, by looping through
| something like this air-code:
|
| For i = Len(bin) To 1 Step - 1
| If Mid$(bin, i, 1) = "1" Then
| n = n + 2^(Len(bin)-i)
| End IF
| Next i
|
| You'll have to take special precautions with the sign-bit, depending
| how you want to use the value.
|
| --
| .NET: It's About Trust!
| http://vfre...
|
|


Karl E. Peterson

4/10/2012 8:47:00 PM

0

Mayayana brought next idea :
> I've got something that I thought I originally
> took from your Twiddle Bits code. Maybe not,
> bu I know I didn't write it:
>
> Public Function Bin2Dec(BinaryString As String) As Long
> Dim X As Long
> For X = 0 To Len(BinaryString) - 1
> Bin2Dec = Bin2Dec + Val(Mid(BinaryString, _
> Len(BinaryString) - X, 1)) * 2 ^ X
> Next
> End Function

I thought I'd written one like that myself, but that one's either been
redone or wasn't done by me. (Just noting stylistic differences - X
rather than i, no variable after Next, lack of $ after Mid)

I really did like Jim's use of StrRev and slinging it into a Byte
array, though! Those things weren't there back when I did that
original set of routines. Far better air code than what I offered!
:-)

--
..NET: It's About Trust!
http://vfre...


Mike Williams

4/10/2012 9:04:00 PM

0

"Karl E. Peterson" <karl@exmvps.org> wrote in message
news:jm267f$ooh$1@dont-email.me...
>
> I really did like Jim's use of StrRev and slinging it
> into a Byte array, though! Those things weren't
> there back when I did that original set of routines.
> Far better air code than what I offered! :-)

.. . . except of course there is no StrRev function in VB6, so you would need
to write that function as well, which would slow it down a tad ;-)

Mike


Jim Mack

4/10/2012 9:42:00 PM

0

>> I really did like Jim's use of StrRev and slinging it
>> into a Byte array, though! Those things weren't
>> there back when I did that original set of routines.
>> Far better air code than what I offered! :-)
>
> . . . except of course there is no StrRev function in VB6, so you would
> need to write that function as well, which would slow it down a tad ;-)

They call it 'air code' for a reason...

To the OP -- substitute 'StrReverse' for 'StrRev'

--
Jim


Karl E. Peterson

4/10/2012 9:57:00 PM

0

Mike Williams explained on 4/10/2012 :
> "Karl E. Peterson" <karl@exmvps.org> wrote in message
> news:jm267f$ooh$1@dont-email.me...
>>
>> I really did like Jim's use of StrRev and slinging it
>> into a Byte array, though! Those things weren't
>> there back when I did that original set of routines.
>> Far better air code than what I offered! :-)
>
> . . . except of course there is no StrRev function in VB6, so you would need
> to write that function as well, which would slow it down a tad ;-)

The beauty of air code! WTH, ...

Public Function StrRev(Expression As String) As String
StrRev = StrReverse(Expression) ' <g>
End Function

--
..NET: It's About Trust!
http://vfre...


Larry Serflaten

4/10/2012 10:00:00 PM

0

Martin wrote:
> How can I convert a string of 1's and 0's to an integer value?


To accurately handle strings out to about 96 bits;

Function Bin2Dec(BinStr As String) As Variant
Dim i As Long
Dim v As Variant
If BinStr Like "*[!01]*" Then
' Raise error, bad input
Else
For i = 1 To Len(BinStr)
v = CDec(v + v) + (1 And Asc(Mid(BinStr, i, 1)))
Next
Bin2Dec = v
End If
End Function


LFS

Martin

4/10/2012 10:58:00 PM

0

Wow - thank you all. Got it working.

ralph

4/11/2012 12:22:00 AM

0

On Tue, 10 Apr 2012 13:46:36 -0700, Karl E. Peterson <karl@exmvps.org>
wrote:

>Mayayana brought next idea :
>> I've got something that I thought I originally
>> took from your Twiddle Bits code. Maybe not,
>> bu I know I didn't write it:
>>
>> Public Function Bin2Dec(BinaryString As String) As Long
>> Dim X As Long
>> For X = 0 To Len(BinaryString) - 1
>> Bin2Dec = Bin2Dec + Val(Mid(BinaryString, _
>> Len(BinaryString) - X, 1)) * 2 ^ X
>> Next
>> End Function
>
>I thought I'd written one like that myself, but that one's either been
>redone or wasn't done by me. (Just noting stylistic differences - X
>rather than i, no variable after Next, lack of $ after Mid)
>
>I really did like Jim's use of StrRev and slinging it into a Byte
>array, though! Those things weren't there back when I did that
>original set of routines. Far better air code than what I offered!
>:-)

Someone once posted a tongue-in-cheek method where the binary string
was passed by automation to <something>, to fetch a hex string, then
prepended and appended "&"s to the hex string and then passed that to
a conversion function.

Anyone remember that? Or the <something>. It was a hoot at the time.
Can remember the giggles, but not the parties involved.

-ralph