[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.vb.general.discussion

Is there an equivalent to ASC() to get the numeric value of multiple bytes?

NadCixelsyd

6/7/2012 12:03:00 PM

I have a four byte string (e.g. 0x03020100) in little-endian format.
Is there a function that will convert this to a long (e.g. 66051).
I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
+ ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
ASC(STRING$)
15 Answers

DaveO

6/7/2012 1:14:00 PM

0


"NadCixelsyd" <nadcixelsyd@aol.com> wrote in message
news:6a32f7b5-f307-4770-a058-c22da36ea94a@e3g2000yqm.googlegroups.com...
>I have a four byte string (e.g. 0x03020100) in little-endian format.
> Is there a function that will convert this to a long (e.g. 66051).
> I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
> + ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
> ASC(STRING$)

Not directly for littleendian like that if it was bigendian then no problem
just prefix a "&H" and away you go :---

&H00010203 = 66051

You could try something like this: (untested air code)

"&H" & Right$(String,2) & Mid$(String,5,2) & Mid$(String,3,2) &
Left$(String,2)

Alternatively you may get better speed thus:

HexStr = "&H00000000"
Mid$(HexStr,3,2) = Right$(String,2)
Mid$(HexStr,5,2) = Mid$(String,5,2)
Mid$(HexStr,7,2) = Mid$(String,3,2)
Mid$(HextStr,9,2) = Keft$(String,2)

There may be a better way to transpose the bytes, perhap some API method I'm
blissfully unaware of.

DaveO.


DaveO

6/7/2012 1:28:00 PM

0

Doh!

Mid$(HextStr,9,2) = Keft$(String,2)
obviously should be
Mid$(HexStr,9,2) = Left$(String,2)

DaveO


Dee Earley

6/7/2012 1:47:00 PM

0

On 07/06/2012 13:02, NadCixelsyd wrote:
> I have a four byte string (e.g. 0x03020100) in little-endian format.
> Is there a function that will convert this to a long (e.g. 66051).
> I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
> + ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
> ASC(STRING$)

You could try CopyMemory into a Long.
You may need to convert from a VB/UTF-16 string into a character/byte
array first though using StrConv().

--
Deanna Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

Jason Keats

6/7/2012 3:17:00 PM

0

NadCixelsyd wrote:
> I have a four byte string (e.g. 0x03020100) in little-endian format.
> Is there a function that will convert this to a long (e.g. 66051).
> I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
> + ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
> ASC(STRING$)

See the VBSpeed site...

http://www.xbeat.net/vbspeed/c_Swap...

Debug.Print SwapEndian08(&H03020100) '-> 66051

HTH

Dee Earley

6/7/2012 3:46:00 PM

0

On 07/06/2012 13:02, NadCixelsyd wrote:
> I have a four byte string (e.g. 0x03020100) in little-endian format.
> Is there a function that will convert this to a long (e.g. 66051).
> I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
> + ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
> ASC(STRING$)

Just to clarify, is it a string containing "0x03020100" or a 4 byte
string holding characters with the ASCII values &H03, &H02, &H01, &H00?
Everyone else seems to be thinking the former...

--
Deanna Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

Dee Earley

6/7/2012 3:48:00 PM

0

On 07/06/2012 14:47, Deanna Earley wrote:
> On 07/06/2012 13:02, NadCixelsyd wrote:
>> I have a four byte string (e.g. 0x03020100) in little-endian format.
>> Is there a function that will convert this to a long (e.g. 66051).
>> I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
>> + ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
>> ASC(STRING$)
>
> You could try CopyMemory into a Long.
> You may need to convert from a VB/UTF-16 string into a character/byte
> array first though using StrConv().

Dim X As String
Dim X2() As Byte
Dim X3 As Long

X = Chr(3) & Chr(2) & Chr(1) & Chr(0)
X2 = StrConv(X, vbFromUnicode)
CopyMemory X3, X2(0), 4
?x3
66051

Note that a VB6 string is NOT suitable for holding arbitrary byte data
and it will be subject to codepage/character set conversion from unicode
(as used by VB6) to anything else.

--
Deanna Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

DaveO

6/7/2012 3:54:00 PM

0


"Deanna Earley" <dee.earley@icode.co.uk> wrote in message
news:jqqiao$f7b$1@speranza.aioe.org...
> On 07/06/2012 13:02, NadCixelsyd wrote:
>> I have a four byte string (e.g. 0x03020100) in little-endian format.
>> Is there a function that will convert this to a long (e.g. 66051).
>> I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
>> + ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
>> ASC(STRING$)
>
> Just to clarify, is it a string containing "0x03020100" or a 4 byte string
> holding characters with the ASCII values &H03, &H02, &H01, &H00?
> Everyone else seems to be thinking the former...

Well given that the ASC(MID... stuff will only work with the former and that
code was from the OP then I think we can be pretty certain.

DaveO.


Dee Earley

6/7/2012 4:04:00 PM

0

On 07/06/2012 16:54, DaveO wrote:
> "Deanna Earley"<dee.earley@icode.co.uk> wrote in message
> news:jqqiao$f7b$1@speranza.aioe.org...
>> On 07/06/2012 13:02, NadCixelsyd wrote:
>>> I have a four byte string (e.g. 0x03020100) in little-endian format.
>>> Is there a function that will convert this to a long (e.g. 66051).
>>> I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
>>> + ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
>>> ASC(STRING$)
>>
>> Just to clarify, is it a string containing "0x03020100" or a 4 byte string
>> holding characters with the ASCII values&H03,&H02,&H01,&H00?
>> Everyone else seems to be thinking the former...
>
> Well given that the ASC(MID... stuff will only work with the former and that
> code was from the OP then I think we can be pretty certain.

No, it would only work with the latter (4 byte characters). I only
queried as your original answer was based on it being a hex string and
Jason thought it was a single Long value.

--
Deanna Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

NadCixelsyd

6/7/2012 5:56:00 PM

0

On Jun 7, 12:03 pm, Deanna Earley <dee.ear...@icode.co.uk> wrote:
> On 07/06/2012 16:54, DaveO wrote:
>
> > "Deanna Earley"<dee.ear...@icode.co.uk>  wrote in message
> >news:jqqiao$f7b$1@speranza.aioe.org...
> >> On 07/06/2012 13:02, NadCixelsyd wrote:
> >>> I have a four byte string (e.g. 0x03020100) in little-endian format.
> >>> Is there a function that will convert this to a long (e.g. 66051).
> >>> I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
> >>> + ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
> >>> ASC(STRING$)
>
> >> Just to clarify, is it a string containing "0x03020100" or a 4 byte string
> >> holding characters with the ASCII values&H03,&H02,&H01,&H00?
> >> Everyone else seems to be thinking the former...
>
> > Well given that the ASC(MID... stuff will only work with the former and that
> > code was from the OP then I think we can be pretty certain.
>
> No, it would only work with the latter (4 byte characters). I only
> queried as your original answer was based on it being a hex string and
> Jason thought it was a single Long value.
>
> --
> Deanna Earley (dee.ear...@icode.co.uk)
> i-Catcher Development Teamhttp://www.icode.co.uk...
>
> iCode Systems
>
> (Replies direct to my email address will be ignored.
> Please reply to the group.)

Sorry for any confusion. There are so many ways to represent a hex
string. In MVS, we would say, X'03020100'. This is four byte field.
Some where I work say, 0x03020100 where "0x" signifies that a hex
string follows (the zero is necessary to distinguish it from a
variable/label). Or the four byte string, S$ = chr(&h03) &
chr(&h02) & chr(&h01) & chr(&h00). If I did it correctly, all of
these would be equivalent to decimal 66051 if the four characters were
in little-endian format.

unknown

6/7/2012 9:37:00 PM

0

"NadCixelsyd" <nadcixelsyd@aol.com> wrote in message
news:6a32f7b5-f307-4770-a058-c22da36ea94a@e3g2000yqm.googlegroups.com...
>I have a four byte string (e.g. 0x03020100) in little-endian format.
> Is there a function that will convert this to a long (e.g. 66051).
> I'm looking for something faster than ASC(MID$(STRING$,4)) * 16777216
> + ASC(MID$(STRING$,3)) * 65536 + ASC(MID$(STRING$,2)) * 256 +
> ASC(STRING$)

AscW is another option, but won't give you advantage if the second byte of
the character is always 0. It's still not clear how the string is stored.
Try posting the result of this code, or post code that puts some value in a
string variable:

Dim i As Long

For i = 1 To 4
Debug.Print AscW(Mid(s, i, 1))
Next