We help IT Professionals succeed at work.

# Convert Byte-Arrays to visual basic datatypes

holli asked
on
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?
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Commented:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

dim i as integer
dim b(1) as byte

b(0)=123:b(1)=128
CopyMemory i,b(0),2

Cheers

Commented:
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.
IT Operations
CERTIFIED EXPERT

Commented:
learning...

Commented:
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

Commented:
Ark,

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

holli
CERTIFIED EXPERT

Commented:
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

Commented:
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

Commented:
Per recommendation, force-accepted.

Netminder
CS Moderator