|
-
Jun 6th, 2006, 03:13 AM
#1
Thread Starter
KING BODWAD XXI
Merging 2 bytes to make an integer (Resolved)
Hello,
If I load all file details into a binary array how would I convert two of those binary entries into an integer or long?
e.g
VB Code:
Dim FileBinary() as byte
Dim intFile as integer
Dim intTest as integer
'Read in the whole file to a binary array
intFile = FreeFile
ReDim FileBinary(Len(Filename))
Open Filename For Binary Access Read As intFile Len = 1
Get intFile, 1, FileBinary
Close intFile
'How do I now read the first two elements as though they were an integer
intTest = FileBinary(0) & Filebinary(1) 'doesnt work
I hope this makes sense. The application I am writting reads in a third part file and needs to be read in as bytes so that I can process the file correctly. i cant therefore load in the value straight to an integer as I dont actually know its an integer until I have the file information.
Last edited by BodwadUK; Jun 6th, 2006 at 04:08 AM.
-
Jun 6th, 2006, 03:50 AM
#2
Re: Merging 2 bytes to make an integer
VB Code:
Declare Sub RtlMoveMemory Lib "kernel32" ( _
ByRef lpvDest As Any, _
ByRef lpvSrc As Any, _
ByVal cbLen As Long _
)
' --
RtlMoveMemory ByVal VarPtr(intTest), FileBinary(0), 1
RtlMoveMemory ByVal VarPtr(intTest) + 1, FileBinary(1), 1
-
Jun 6th, 2006, 04:08 AM
#3
Thread Starter
KING BODWAD XXI
-
Jun 6th, 2006, 04:57 AM
#4
Thread Starter
KING BODWAD XXI
Re: Merging 2 bytes to make an integer (Resolved)
Do I have to do something different for long values?
VB Code:
'Calculate The Long
RtlMoveMemory ByVal VarPtr(ReturnLong), Binary(Startpos), 1
RtlMoveMemory ByVal VarPtr(ReturnLong) + 1, Binary(Startpos + 1), 1
RtlMoveMemory ByVal VarPtr(ReturnLong) + 2, Binary(Startpos + 2), 1
RtlMoveMemory ByVal VarPtr(ReturnLong) + 3, Binary(Startpos + 3), 1
Doesnt seem to work for this?
-
Jun 6th, 2006, 04:59 AM
#5
Re: Merging 2 bytes to make an integer (Resolved)
Hmm. It should, unless my memory is playing tricks with me.
What do you get? Is it hideously big or negative in comparison to what you should be getting?
-
Jun 6th, 2006, 05:13 AM
#6
Thread Starter
KING BODWAD XXI
Re: Merging 2 bytes to make an integer (Resolved)
Its a huge negative number =(
-
Jun 6th, 2006, 05:16 AM
#7
Re: Merging 2 bytes to make an integer (Resolved)
Might be an endian issue. Try it in reverse.
VB Code:
RtlMoveMemory ByVal VarPtr(ReturnLong), Binary(Startpos + 3), 1
RtlMoveMemory ByVal VarPtr(ReturnLong) + 1, Binary(Startpos + 2), 1
RtlMoveMemory ByVal VarPtr(ReturnLong) + 2, Binary(Startpos + 1), 1
RtlMoveMemory ByVal VarPtr(ReturnLong) + 3, Binary(Startpos), 1
Or failing that:
VB Code:
RtlMoveMemory ByVal VarPtr(ReturnLong), Binary(Startpos + 2), 1
RtlMoveMemory ByVal VarPtr(ReturnLong) + 1, Binary(Startpos + 3), 1
RtlMoveMemory ByVal VarPtr(ReturnLong) + 2, Binary(Startpos), 1
RtlMoveMemory ByVal VarPtr(ReturnLong) + 3, Binary(Startpos + 1), 1
See if either of those work.
-
Jun 6th, 2006, 05:39 AM
#8
Thread Starter
KING BODWAD XXI
Re: Merging 2 bytes to make an integer (Resolved)
No they didnt work either. I must be doing something really stupid in my program because logically it should work
-
Jun 6th, 2006, 05:41 AM
#9
Re: Merging 2 bytes to make an integer (Resolved)
:S
What exactly do you get, and what exactly should you get? I'll see if I can spot what's wrong.
-
Jun 6th, 2006, 05:55 AM
#10
Re: Merging 2 bytes to make an integer (Resolved)
intTest = (FileBinary(0) * (2^8)) OR Filebinary(1)
-
Jun 6th, 2006, 06:31 AM
#11
Thread Starter
KING BODWAD XXI
Re: Merging 2 bytes to make an integer (Resolved)
sod it ill parse an ascii version of the file 
I kept getting negative numbers for file sizes on my longs
-
Jun 6th, 2006, 06:35 AM
#12
PowerPoster
Re: Merging 2 bytes to make an integer (Resolved)
Filesizes? Are you getting the filesizes for files on your local drive? There's simple enough methods for doing that using only a few lines of code...basically open the file for reading, do a siz=LOF(1) and close it (assuming you opened it using #1...change that if not)...LOF is "Length Of File" and would return the exact byte size to the variable siz :-)
-
Jun 6th, 2006, 06:46 AM
#13
Thread Starter
KING BODWAD XXI
Re: Merging 2 bytes to make an integer (Resolved)
No sorry the file contains zip files. I am trying to decode the .cob format of truespace which contains a zip file embedded within it.
-
Jun 6th, 2006, 06:49 AM
#14
PowerPoster
Re: Merging 2 bytes to make an integer (Resolved)
Ah, I see (walks away more confused than before) :-P
-
Jun 6th, 2006, 07:05 AM
#15
Re: Merging 2 bytes to make an integer (Resolved)
If the result is negative then the highest order bit was set to 1... will removing this bit give you the correct size?
intTest = intTest AND 65535
lngTest = lngTest AND 4294967295
-
Jun 6th, 2006, 07:09 AM
#16
Re: Merging 2 bytes to make an integer (Resolved)
What I always do, however MS doesn't recommend, is making 2 UDT:
VB Code:
Public Type udtLongByte
Byte1 as Byte
Byte2 as Byte
Byte3 as Byte
Byte4 as Byte
End Type
and:
VB Code:
Public Type udtLong
MyLong as Long
End Type
Fill the bytes and:
VB Code:
Dim LongByte as udtLongByte
Dim LongValue as udtLong
LongByte.Byte1=..
LongByte.Byte2=..
LongByte.Byte3=..
LongByte.Byte4=..
LSet LongValue=LongByte
...=LongValue.MyLong
-
Jun 6th, 2006, 10:01 AM
#17
Thread Starter
KING BODWAD XXI
Re: Merging 2 bytes to make an integer (Resolved)
I have tried both but they didnt work and had the same result.
Does anyone know which "bit" is the negative trigger? I always thought it was the first one.
I am trying to do a bit operation on it to see if that does it
EDIT:::: How random if I go back and grab the data by opening the file and getting the long at that exact location, then the value is different (and correct). How wierd
-
Jun 6th, 2006, 10:06 AM
#18
Thread Starter
KING BODWAD XXI
Re: Merging 2 bytes to make an integer (Resolved)
oh ffs its got to be a badly written file. It isnt different if I grab it from the file
-
Jun 6th, 2006, 10:08 AM
#19
Re: Merging 2 bytes to make an integer (Resolved)
Could the problem be that VB has no unsigned variables? Are you sure that the expected values are in range?
-
Jun 6th, 2006, 10:16 AM
#20
Re: Merging 2 bytes to make an integer (Resolved)
Long is limited to 2147483647 (&H7FFFFFFF) in positive numbers. So, about the only thing we can do is to use something else to handle the numbers properly. Thus this leads us to one solution: use Currency. Unfortunatenaly we can't use bitwise operators for Currency, which forces us to use even more basic math. So to finally get what you want:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim Bytes(3) As Byte, curTest As Currency
Bytes(0) = &HFF
Bytes(1) = &HFF
Bytes(2) = &HFF
Bytes(3) = &HFF
curTest = Bytes(0) * CCur(&H1000000) + Bytes(1) * CCur(&H10000) + _
Bytes(2) * CCur(&H100&) + Bytes(3)
MsgBox curTest
End Sub
-
Jun 6th, 2006, 10:17 AM
#21
Thread Starter
KING BODWAD XXI
Re: Merging 2 bytes to make an integer (Resolved)
The specification document doesnt say its unsigned and the zip certainly isnt very big. Ho hum
Lol ill just have to learn c++
-
Jun 6th, 2006, 10:24 AM
#22
Re: Merging 2 bytes to make an integer (Resolved)
Also, if you want to read Long directly from file and convert it to Currency without hassling with bytes and API like that:
VB Code:
Private Sub Form_Load()
Dim lngTest As Long, curTest As Currency
lngTest = CLng(-1)
If lngTest < 0 Then
curTest = CCur(lngTest And &H7FFFFFFF) + 2147483648#
Else
curTest = lngTest
End If
MsgBox curTest
End Sub
Believe me, this is also faster than doing four calls of RtlMoveMemory
-
Jun 6th, 2006, 12:09 PM
#23
Re: Merging 2 bytes to make an integer (Resolved)
Signed values are according 2’s complement.
If you have a 3 bits variable then:
Signed Unsigned
000 0 0
001 1 1
010 2 2
011 3 3
111 7 -1
110 6 -2
101 5 -3
100 4 -4
So flipping the sign bit will not solve your problem. In this case you can do (pseudo code):
If var <0 then vardef = 8 - var
If var =>0 then vardef = var
Last edited by namrekka; Jun 6th, 2006 at 12:13 PM.
-
Jun 7th, 2006, 02:01 AM
#24
Thread Starter
KING BODWAD XXI
Re: Merging 2 bytes to make an integer (Resolved)
thanks everyone but I have given up for now. I will just have to text parse an ascii file when I can be bothered
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|