Solved

Long value stored in byte array.

Posted on 1998-12-31
6
147 Views
Last Modified: 2010-05-03
Hi there,

As I understand it a long data item in VB is stored using 32-bits.  This means that in a byte array the value must be sliced so that it occupied 4 8-bit array elements - anyone got code showing how to do this?  I'm sure it involves using bit-masks but my experience in this area is limited - sample code, along with comments, would be greatly appreciated.

Pete
0
Comment
Question by:peterwest
  • 4
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
Mirkwood earned 100 total points
ID: 1453414
Here are the functions:

Int = 256 * HighByte + LowByte
LongInt = 65536 * HighWord + LowWord

LoByte = Int MOD 256
HighByte = Int \ 256
LowWord = LongInt MOD 65536
HighWord = LongInt \ 65536


0
 
LVL 2

Author Comment

by:peterwest
ID: 1453415
Mirkwood,

Thanks for the quick response but i'd appreciate it if you could explain how to apply this code in a little more detail.

Say, for example, I have a long value stored in a variable called lResult and an 4 digit byte array (0 to 3) called MyArray.  Could you post code showing how to translate lResult into MyArray.

Sorry if this sounds really simple but I just don't understand how to use the code you've posted.

Thanks

Pete

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1453416
Sure give me 5 minutes


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1453417
Hmm, I found a better solution. (Read a lot faster)

http://www.softcircuits.com/sw_tools.htm
They are free!
0
 
LVL 2

Author Comment

by:peterwest
ID: 1453418
Thanks mirkwood,

I've looked at the link you've sent me but I probably won't use that method.  I've sat down and looked at your routine again and I understand now how to split the 32 bit value into two words and then split each word into 2 bytes.  I've written a routine using these formulae and they work fine.

Thanks for the quick reply.

Pete

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1453419
Here is some more, note they do not work anymore when the hiword is > 32767
This is a limitation of VB which uses signed long.


Private Function HiByte(ByVal w As Integer) As Byte
    HiByte = w \ 256
End Function

Private Function LoByte(ByVal w As Integer) As Byte
    LoByte = w Or &HFF
End Function

Private Function HiWord(ByVal l As Integer) As Integer
    HiByte = w \ 256
End Function

Private Function LoWord(ByVal l As Long) As Integer
    LoByte = w Or &HFFFF
End Function

Private Function MakeWord(ByVal HiByte As Byte, ByVal LoByte As Byte) As Long
    MakeWord = CLng(HiByte) * 256 + LoByte
End Function

Private Function MakeLong(ByVal HiWord As Long, ByVal LoWord As Long) As Long
    MakeLong = HiWord * 65536 + LoWord
End Function

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using Access 8 58
passing a value with stream reader AFTER a ";" 3 58
Copy a row 12 57
VBA: Select SQL query based on a config Sheet v2 11 38
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now