Results 1 to 18 of 18

Thread: [2005] How to convert a array of bytes to a long value?

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    [2005] How to convert a array of bytes to a long value?

    Hi!!

    Probably a easy and simple question. But i have been searching and cant find the answer.I have a array of bytes with 4 elements. How can i convert it to a Long value?
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] How to convert a array of bytes to a long value?

    I'd be interested to see a good answer to this one, as well. My solution was to do it directly:

    vb Code:
    1. 'I assume that your really mean an 8 byte long, but I'm going to cheat a bit.
    2. Dim lng as Long
    3. Dim bArr(7) as Byte
    4.  
    5.  
    6. lng = (Clng((Cint(bArr(0)) >> 24) + (CInt(bArr(1) >> 16) + (CInt(bArr(2) >>8)_
    7. + bArr(3))>>32) + ((CInt(bArr(4)) >> 24) + (CInt(bArr(5)) >> 16) + _
    8. (CInt(bArr(6)) >> 8) + bArr(7)

    Just typed that freehand, which royally sucks. I may have some of the parenthesis wrong, but the idea is pretty simple:

    I took the first byte, converted it to an integer and shifted it to the top byte. I took the second byte, converted it to an integer, shifted it to the third byte, and added it to the first. I did roughly the same with the next two bytes, shifting the third byte eight places, an not shifting the last byte at all. I used an integer because I felt it might be slightly faster than a long. Therefore, I took bytes 4-7 and converted them into an integer as well, converted the first integer to a Long, shifted it 32 places, and added in the second integer.

    I may have the order of the bytes all screwed up, but the technique works. I've never tried it for a Long, but I am working on a project where I convert arrays of bytes into integers routinely, so at least those steps work fine (though I didn't actually copy in the code, so, again, the parenthesis could be all wrong).
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] How to convert a array of bytes to a long value?

    Well. Although i get the idea of what u done it and "why", that code really confuses me (well if i look with more time to it...)
    I said wrong Long i wanted to say Integer, because i'm reading a .pdf of the TGA File Format and they are working with bits so a Long to "them" is a 32 bits value. Then i came here to make the question still thinking on what i read on the .pdf and said long lool :P.
    Anyway this is really a simple thing because an integer is 4 bytes value. So to convert four bytes to an integer should be very simple like Integer.FromBytes(My4ByteArray) or like Bytes.ToInteger. But no there is no simple way of doing this and we have to waste time to do it. I'm still going to keep looking because i'm still convinced that there is an easy way.

    thks a lot for ur code. By the way i ready the post u asked, well the same since i thougth i wanted and long but i really need an integer.

    Let the ask u another thing:
    How can u convert this 00CF890F (hexadecimal number) into this 13601039 (decimal number)? (I know that windows calculator does it so its got to be easy)
    Last edited by Lasering; Nov 20th, 2007 at 02:17 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] How to convert a array of bytes to a long value?

    well I found a "clue".
    If u do this: CInt("&H00CF895F") u will get 13601119
    If u do this: CInt("&H00CF890F") u will get 13601039
    Both are correct
    If u do this:
    vb Code:
    1. Dim b() As Byte = {&H0, &HCF, &H89, &H5F}
    2. CInt("&H" & Hex(b(0)) & Hex(b(1)) & Hex(b(2)) & Hex(b(3)))
    u will get 13601119 which is correct. Now comes the problem, if u do this:
    vb Code:
    1. Dim b() As Byte = {&H0, &HCF, &H89, &HF}
    2. CInt("&H" & Hex(b(0)) & Hex(b(1)) & Hex(b(2)) & Hex(b(3)))
    u will get 850079. Why? Because the string that gets inside CInt is "&H00CF89F" and NOT "&H00CF890F" which would be the correct value.

    Now we just need to find a way of converting a byte to its "hexadecimal string" correctly.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] How to convert a array of bytes to a long value?

    As you saw, I was also looking for a simple answer and didn't find one. I have the actual code to do this on a different computer, but not here. To convert to an integer is this piece of the code I posted above:

    vb Code:
    1. Dim i as int
    2. Dim bArr(3) as Byte
    3.  
    4. i = ((CInt(bArr(0)) >> 24) + (CInt(bArr(1)) >> 16) + (CInt(bArr(2)) >> 8) + bArr(3)

    as for converting hex to decimal, I don't have that handy, but you can search on hex to decimal and find lots. I've seen the question plenty.
    My usual boring signature: Nothing

  6. #6
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: [2005] How to convert a array of bytes to a long value?

    Not sure if I'm on the same wavelength, as (Yes you've heard it before ) I'm new to net. But this is what I use to convert a (binary) byte array (retrieved from the registry) to an integer:
    Code:
    intDwordDat = System.Text.Encoding.ASCII.GetString( theByteArray() )

  7. #7

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] How to convert a array of bytes to a long value?

    that way u get the ASCII chacacters, we are trying to get the integer
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  8. #8
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: [2005] How to convert a array of bytes to a long value?

    Ah, do you mean convert the array of say:

    15 CD 5B 07

    to

    123456789

    ?

  9. #9

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] How to convert a array of bytes to a long value?

    yes but in that case 15 CD 5B 07 = 365779719
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  10. #10
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: [2005] How to convert a array of bytes to a long value?

    I'm going off the way the bytes are stored in the registry . If you reverse the byte order and do "MsgBox CInt("&H075BCD15")" you get 123456789.

    As for formatting the byte array:

    Code:
    strRet = Hex(ByteArray(intIndex)).PadLeft(2, "0")

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] How to convert a array of bytes to a long value?

    Actually, for a byte array, that would work. You can convert the bytes to a string using GetString, then convert back to an integer with CInt(). However, I would be really surprised if that way is faster than the solution I posted for an array of bytes. If you have a string hex number to begin with, rather than an array of bytes, then this would be faster than just the bit shifting.

    Bit shifts and additions should be among the fastest operations you can perform, so my bit shifting technique should be faster than the conversion to string then integer, but only for raw bytes, not character interpretations of numbers.
    My usual boring signature: Nothing

  12. #12
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: [2005] How to convert a array of bytes to a long value?

    I very much doubt my way would be fast. The main reason I originally came up with this was for registry manipulation - if anything, it's better off being slow.

    I use something similar to concatenate hex strings (from REG_BINARY or REG_NONE types) before converting to a number. It works (*shrug).

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] How to convert a array of bytes to a long value?

    For hex strings, it is the best way. In fact, for ANY string it is the best way. My technique will ONLY work if the byte that holds 1 holds only 1, not the character 1, which will be the case with hex strings.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] How to convert a array of bytes to a long value?

    Shaggy how can I convert two bytes to a short? Using bitshiffting
    Last edited by Lasering; Nov 22nd, 2007 at 09:58 AM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] How to convert a array of bytes to a long value?

    Here's a sub from my robot code where I take an array of bytes and convert it into two different integers. I am using 32 bit integers for convenience sake, but I'm only filling 16 bits of each of them, so a short would work just as well. Unless you absolutely need to use a short, using an int is slightly faster, though you just waste the top two bytes.

    vb Code:
    1. Public Overrides Function DecomposeString(ByRef buf() As Byte) As Boolean
    2.         mIndSpeed(0) = (CInt(buf(0) << 8)) + buf(1)
    3.         mIndSpeed(1) = (CInt(buf(2) << 8)) + buf(3)
    4. End Function

    HA!! Just noticed that this is a function that is supposed to return a boolean. It is actually declared MustOverride in a base class, so it needs the same signature, but I never do return anything. Oops. I need to change those to subs. That's development for you. My original intention for the function has changed by now, and it shouldn't return anything.

    As far as converting the information goes, this assumes that the first byte holds the high part of the number and the second byte holds the low part. Not all data comes through this way.

    What the code is doing is converting the one byte into an integer (four bytes, with only the low byte holding anything), then shifts the integer eight places, which moves the information in the low byte up to the second byte. Then the other byte is added in. Since the shift will mean that there will be nothing in the low byte, adding in the other byte will put it into the low byte. An alternative would be to OR in the other byte, but there should be no performance difference between these two, so I chose addition.
    My usual boring signature: Nothing

  16. #16

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] How to convert a array of bytes to a long value?

    Thks a lot. I'm starting to understand how to shift.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] How to convert a array of bytes to a long value?

    Bit shifting is a real interesting operation. On it's face, it just moves bits:

    10110011 >> 1 = 01011001

    However, this has another strange property, because it is the same as multiplying by 2. Shifting the other direction is the same as an integer divide by two. However, bit shifts are among the set of the fastest instructions that can be performed by a processor, so shifting by 1 is FAR faster than multiplying by 2. An interesting property, but not often a very useful one.
    My usual boring signature: Nothing

  18. #18

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] How to convert a array of bytes to a long value?

    Well I need to do this again and I found the jackpot!!

    Well is not as fast as shiffting of course but here it goes:
    vb Code:
    1. Dim b() As Byte = {&H0, &HCF, &H89, &HF}
    2. MsgBox(CInt("&H" & b(0).ToString("x2") & b(1).ToString("x2") & b(2).ToString("x2") & b(3).ToString("x2")))
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width