Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Long value stored in byte array.

Posted on 1998-12-31
6
Medium Priority
?
155 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 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Suggested Courses

824 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