Results 1 to 24 of 24

Thread: Merging 2 bytes to make an integer (Resolved)

  1. #1

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Resolved 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:
    1. Dim FileBinary() as byte
    2.     Dim intFile as integer
    3.     Dim intTest as integer
    4.  
    5.     'Read in the whole file to a binary array
    6.     intFile = FreeFile
    7.     ReDim FileBinary(Len(Filename))
    8.     Open Filename For Binary Access Read As intFile Len = 1
    9.         Get intFile, 1, FileBinary
    10.     Close intFile
    11.  
    12.     'How do I now read the first two elements as though they were an integer
    13.     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.
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Merging 2 bytes to make an integer

    VB Code:
    1. Declare Sub RtlMoveMemory Lib "kernel32" ( _
    2.   ByRef lpvDest As Any, _
    3.   ByRef lpvSrc As Any, _
    4.   ByVal cbLen As Long _
    5. )
    6.  
    7. ' --
    8.  
    9. RtlMoveMemory ByVal VarPtr(intTest), FileBinary(0), 1
    10. RtlMoveMemory ByVal VarPtr(intTest) + 1, FileBinary(1), 1

  3. #3

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Re: Merging 2 bytes to make an integer

    Brilliant that even helps with converting to other vars too. Have a rep point
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  4. #4

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Re: Merging 2 bytes to make an integer (Resolved)

    Do I have to do something different for long values?

    VB Code:
    1. 'Calculate The Long
    2.     RtlMoveMemory ByVal VarPtr(ReturnLong), Binary(Startpos), 1
    3.     RtlMoveMemory ByVal VarPtr(ReturnLong) + 1, Binary(Startpos + 1), 1
    4.     RtlMoveMemory ByVal VarPtr(ReturnLong) + 2, Binary(Startpos + 2), 1
    5.     RtlMoveMemory ByVal VarPtr(ReturnLong) + 3, Binary(Startpos + 3), 1

    Doesnt seem to work for this?
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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?

  6. #6

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Re: Merging 2 bytes to make an integer (Resolved)

    Its a huge negative number =(
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Merging 2 bytes to make an integer (Resolved)

    Might be an endian issue. Try it in reverse.

    VB Code:
    1. RtlMoveMemory ByVal VarPtr(ReturnLong), Binary(Startpos + 3), 1
    2.     RtlMoveMemory ByVal VarPtr(ReturnLong) + 1, Binary(Startpos + 2), 1
    3.     RtlMoveMemory ByVal VarPtr(ReturnLong) + 2, Binary(Startpos + 1), 1
    4.     RtlMoveMemory ByVal VarPtr(ReturnLong) + 3, Binary(Startpos), 1

    Or failing that:
    VB Code:
    1. RtlMoveMemory ByVal VarPtr(ReturnLong), Binary(Startpos + 2), 1
    2.     RtlMoveMemory ByVal VarPtr(ReturnLong) + 1, Binary(Startpos + 3), 1
    3.     RtlMoveMemory ByVal VarPtr(ReturnLong) + 2, Binary(Startpos), 1
    4.     RtlMoveMemory ByVal VarPtr(ReturnLong) + 3, Binary(Startpos + 1), 1

    See if either of those work.

  8. #8

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    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
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Merging 2 bytes to make an integer (Resolved)

    intTest = (FileBinary(0) * (2^8)) OR Filebinary(1)

  11. #11

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    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
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  12. #12
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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 :-)

  13. #13

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    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.
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  14. #14
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Merging 2 bytes to make an integer (Resolved)

    Ah, I see (walks away more confused than before) :-P

  15. #15
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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

  16. #16
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Merging 2 bytes to make an integer (Resolved)

    What I always do, however MS doesn't recommend, is making 2 UDT:

    VB Code:
    1. Public Type udtLongByte
    2.  Byte1 as Byte
    3.  Byte2 as Byte
    4.  Byte3 as Byte
    5.  Byte4 as Byte
    6. End Type

    and:

    VB Code:
    1. Public Type udtLong
    2.  MyLong as Long
    3. End Type

    Fill the bytes and:

    VB Code:
    1. Dim LongByte as udtLongByte
    2.  Dim LongValue as udtLong
    3.  
    4.  LongByte.Byte1=..
    5.  LongByte.Byte2=..
    6.  LongByte.Byte3=..
    7.  LongByte.Byte4=..
    8.  
    9.  LSet LongValue=LongByte
    10.  
    11.  ...=LongValue.MyLong

  17. #17

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    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
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  18. #18

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    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
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  19. #19
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    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?

  20. #20
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim Bytes(3) As Byte, curTest As Currency
    5.     Bytes(0) = &HFF
    6.     Bytes(1) = &HFF
    7.     Bytes(2) = &HFF
    8.     Bytes(3) = &HFF
    9.     curTest = Bytes(0) * CCur(&H1000000) + Bytes(1) * CCur(&H10000) + _
    10.         Bytes(2) * CCur(&H100&) + Bytes(3)
    11.     MsgBox curTest
    12. End Sub


  21. #21

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    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++
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  22. #22
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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:
    1. Private Sub Form_Load()
    2.     Dim lngTest As Long, curTest As Currency
    3.     lngTest = CLng(-1)
    4.     If lngTest < 0 Then
    5.         curTest = CCur(lngTest And &H7FFFFFFF) + 2147483648#
    6.     Else
    7.         curTest = lngTest
    8.     End If
    9.     MsgBox curTest
    10. End Sub

    Believe me, this is also faster than doing four calls of RtlMoveMemory

  23. #23
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    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.

  24. #24

    Thread Starter
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    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
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

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