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?
Printable View
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?
I'd be interested to see a good answer to this one, as well. My solution was to do it directly:
vb Code:
'I assume that your really mean an 8 byte long, but I'm going to cheat a bit. Dim lng as Long Dim bArr(7) as Byte lng = (Clng((Cint(bArr(0)) >> 24) + (CInt(bArr(1) >> 16) + (CInt(bArr(2) >>8)_ + bArr(3))>>32) + ((CInt(bArr(4)) >> 24) + (CInt(bArr(5)) >> 16) + _ (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).
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)
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:
u will get 13601119 which is correct. Now comes the problem, if u do this:vb Code:
Dim b() As Byte = {&H0, &HCF, &H89, &H5F} 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.vb Code:
Dim b() As Byte = {&H0, &HCF, &H89, &HF} CInt("&H" & Hex(b(0)) & Hex(b(1)) & Hex(b(2)) & Hex(b(3)))
Now we just need to find a way of converting a byte to its "hexadecimal string" correctly.
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:
Dim i as int Dim bArr(3) as Byte 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.
Not sure if I'm on the same wavelength, as (Yes you've heard it before :rolleyes: ) 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() )
that way u get the ASCII chacacters, we are trying to get the integer
Ah, do you mean convert the array of say:
15 CD 5B 07
to
123456789
?
yes but in that case 15 CD 5B 07 = 365779719
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")
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.
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).
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.
Shaggy how can I convert two bytes to a short? Using bitshiffting
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:
Public Overrides Function DecomposeString(ByRef buf() As Byte) As Boolean mIndSpeed(0) = (CInt(buf(0) << 8)) + buf(1) mIndSpeed(1) = (CInt(buf(2) << 8)) + buf(3) 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.
Thks a lot. I'm starting to understand how to shift.
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.
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:
Dim b() As Byte = {&H0, &HCF, &H89, &HF} MsgBox(CInt("&H" & b(0).ToString("x2") & b(1).ToString("x2") & b(2).ToString("x2") & b(3).ToString("x2")))