Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in data conversion... 1

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
0
0
GB
Can anyone give me any help in converting a Long into its constituent four Bytes?

Cheers

elziko
 
Doh! I meant to say a Single not a long.

At the moment I'm working on using an API function to get the address of an array which contains a bunch of Singles and then I wanna try to read each succesive byte into a byte array.

Any thoughts?

elziko
 
Elziko,
There are several ways to do this :-
One is to use LSET to assign the memory contents of one user
defined variable into another eg ....

Private Type Typ_4Byte
Byte1 As Byte
Byte2 As Byte
Byte3 As Byte
Byte4 As Byte
End Type

Private Type Typ_Single
Byte_All As Single
End Type

Dim Four_Bytes As Typ_4Byte
Dim Floater1 As Typ_Single

'put code here to assign your floating point number to
'variable Floater1.Byte_All

lset Four_Bytes = Floater1

'Floater1.Byte1 to Byte4 will contain the individual bytes
'that comprised the single.
 
Yeah, thanks very much.

I need a little more advice, from anyone!

Using the advice above I have now stored an array of Singles in a Byte array.

The only problem is the array of Singles is two dimensional. If it was only a single dimension I would know that I can create a Single on every forth byte in the Byte array and that would enable me to rebuild the Single array.

With a two dimensional array I need to know when a new 'column' in the second dimension starts so that I can recreate it.

I've tried setting one of the entries in the byte array to NULL enabling me to iterate through the Byte array and start a new column in the rebuilt Single array every time I encounter a NULL.

However, I have found that its not possible to assign a value of NULL to a byte.

How else could I do this?

cheers,

elziko
 
I've sorted i now by storing the number of columns in the first byte of the byte array.

But if anyone has any comments or better ways of doing this.......
 

You can use the CopyMemory api...

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(ByRef lpvDest As Any, ByRef lpvSource As Any, _
ByVal cbCopy As Long)

example...

Dim bytLongs(0 to 3) '4 bytes only for 1 long, increase as needed
Dim lngPointer as Long
Dim lngNumber as Long

lngPointer = 0
lngNumber = 4563

CopyMemory(bytLongs(lngPointer), lngNumber, 4)


'increment lngPointer by 4 before copying next long value to byte array
 

Dim bytLongs(0 to 3) as byte

syntax for CopyMemory....

CopyMemory bytLongs(lngPointer), lngNumber, 4


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top