Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Long value stored in byte array.

Posted on 1998-12-31
6
Medium Priority
?
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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

721 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