Solved

Long value stored in byte array.

Posted on 1998-12-31
6
148 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

821 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