[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Converting ByteArray to Long - Two different functions - What exactly do they mean?

Posted on 2003-11-22
5
Medium Priority
?
379 Views
Last Modified: 2010-04-17
Hello!

While working on a project of mine, I have come across the following functions:

Public Function Convert1(ByRef abytByteArray() As Byte) As Long

  Dim lngCounter As Long
  Dim lngResult As Long

    For lngCounter = 0 To 3
        lngResult = lngResult + (abytByteArray(lngCounter) * (2 ^ ((3 - lngCounter) * 7)))
    Next lngCounter

    Convert1 = lngResult

Exit Function

Public Function Convert2(abytByteArray() As Byte) As Long

  Dim lngCounter As Long
  Dim lngResult As Long

    On Error GoTo ErrorHandler

    For lngCounter = 0 To 3
        lngResult = ShiftLeft(lngResult, 8) + abytByteArray(lngCounter)
    Next lngCounter

    Convert2= lngResult

Exit Function

In Convert2, "ShiftLeft" is a function for shifting a value n times to the left.

Let us say you have the following array:

a(0) = 3
a(1) = 35
a(2) = 127
a(3) = 251

Convert1 will return 6881403 and Convert2 will return 52658171.
What exactly do those functions do?


Regards,
Sebastian Mares
0
Comment
Question by:Sebastian_Mares
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
cookre earned 750 total points
ID: 9803001
In neither is lngResult initialized, so we'll presume the intent wast to start with 0.

The first one sums the entries in the array multiplied by 2 raised to decreasing multiples of seven.
The powers of two are 3*7, 2*7, 1*7, and 0*7, or 21, 14, 7, and 0.
This also has the effect of shifting the entries in the array left by 21, 14, 7, and 0 bits each.
So the final result is array(0)<<21  OR  array(1)<<14  OR  array(2)<<7  OR  array(3).
Or, putting the entries in array into seven bit clumps.  Given that your example has arr(3) requiring 8 bits, I'd be much happier if the powers of two were multiples of 8 instead of 7.


In any case, the second one has the effect of making a 32-bit item with the array entries stored left to right, 0 to 3, the result being a long whose value is:

65536*(256*arr(3) + arr(2))   + (256*arr(1)+arr(0) (recall that on Intel CPUs, bytes in integers are stored least significant first).

Note that can also be considered reversing the entries of the array and puting the result into 32 bits.

Given what the second one does, if the multiplier in the first one had been 8, it's result would have been putting the entries in the array into a 32-bit item in the same order as the second.
0
 

Author Comment

by:Sebastian_Mares
ID: 9803062
OK, I understand that the second function will convert a Big Endian value to Long. Assuming we have the following values: 0xFF , 0x51 , 0x01 , 0x35, the function will convert the array to the value 0xFF510135.

The main problem is with the first function. I have no idea why the powers of two are multiples of 7.
0
 

Author Comment

by:Sebastian_Mares
ID: 9803075
OK, I think I got it... The first function is used for calculating the size of an ID3v2 tag. The size descriptor is stored inside the ID3v2 tag header. Here is what ID3.org says about the format of the size descriptor:

"The ID3v2 tag size is encoded with four bytes where the most
significant bit (bit 7) is set to zero in every byte, making a total
of 28 bits. The zeroed bits are ignored, so a 257 bytes long tag is
represented as $00 00 02 01."

This should explain why the powers of 2 are a multiple of 7. :-)
0
 

Author Comment

by:Sebastian_Mares
ID: 9803093
Last thing... The first function could also be:

Public Function Convert1(abytByteArray() As Byte) As Long

  Dim lngCounter As Long
  Dim lngResult As Long

    On Error GoTo ErrorHandler

    For lngCounter = 0 To 3
        lngResult = ShiftLeft(lngResult, 7) + abytByteArray(lngCounter)
    Next lngCounter

    Convert1= lngResult

Exit Function
0
 
LVL 22

Expert Comment

by:cookre
ID: 9803117
If it's intended to smoosh four 7-bit fields into the rightmost 28 bits, then that's what it does.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Loops Section Overview

872 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