Link to home
Start Free TrialLog in
Avatar of holli
holli

asked on

Convert Byte-Arrays to visual basic datatypes

How can i convert byte arrays into vb-datatypes?

i need to know ho to do this for all numeric datatypes and fixed-lenght strings (dim foo as string * 10).

eg.
dim i as integer
dim b(1) as byte

b(0)=123:b(1)=128
i=????? 'bytearray2int?
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello holli !

'dim b(1) as byte' is an vb array and byte is a vb datatype.
So there is nothing to do.

If you want to store b(0) and b(1) in i as low and high byte:

Public Function GetInteger(LowByte As Byte, HighByte As Byte) As Integer
    Dim tmp As Long
    tmp = CLng(CLng(HighByte) * 256) + CLng(LowByte)
    If (HighByte And 128) Then tmp = Not ((tmp And &H7F) - 1)
    GetInteger = CInt(tmp)
End Function


V.K.
learning...
Avatar of holli
holli

ASKER

thanks so far.

So i need a set of functions like

function byte2long (array(3) as byte) as long
end function

for all mentioned datatypes

tx, holli
Avatar of holli

ASKER

Ark,

would you provide examples for the other data-types?
then i would like to reward you the points.

holli
Using Ark's example don't forget to pre-create your destination data to be big enough to receive the data.

e.g.

Dim NoOfBytes as Long
ReDim ByteArray(99) ' source data
Dim A$              ' destination

NoOfBytes = 100   ' no of bytes to copy

' MAKE SURE DESTINATION IS BIG ENOUGH OR ELSE BIG SCREW-UP
A$ = String$(100,0) ' declare the destination
 
CopyMemory A$, ByteArray(0), NoOfBytes

This question appears to have been abandoned. A Moderator will be asked to
close this question after seven days, with the following recommended
disposition:

Points to Ark

If you have any comment or objection to the recommendation, please leave it here.

guidway
EE Cleanup Volunteer
Per recommendation, force-accepted.

Netminder
CS Moderator