Solved

Long value stored in byte array.

Posted on 1998-12-31
6
146 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 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

18 Experts available now in Live!

Get 1:1 Help Now