reading bytes in reverse order

I need to read three bytes at the following offsets: 13583, 13584, 13585. Except, I need to read them in reverse order (13585, 13584, 13583)

Say the bytes in those offsets are so:

13583 = 0E
13584 = 45
13585 = 1F

Now in normal reading order it would be 0E451F which would be 935199 in decimal. But, in reverse order (the order i need) it would be 1F450E or 2049294 decimal.

I have no clue how I can do this. I spent about 45 minutes looking through the object editor and found nothing that would help me. Any ideas




Answer this question

reading bytes in reverse order

  • plmokn


            Dim hi, mid, lo As Byte
            Dim i As Integer = &HAABBCC
            hi = (i And &HFF)
            mid = (i >> 8) And &HFF
            lo = (i >> 16) And &HFF

            ' Start int: AABBCC, hi: AA, mid: BB, lo: CC

  • wuchisan

    That "computation" didn't work. I had the following values

    13583 = 00
    13584 = 00
    13585 = 01

    So, in reverse order it would be 10000h or 65536 decimal. But, the code you provided resulted in a 1 being displayed. It's almost like it completely ignored the zeros. I also tried it with all three bytes equaling 11h and it resulted in 17 (111111h is 1118481 in decimal)

    this is the code I got:

    Protected charExp As UInt32
    charExp = (b(13585) << 16) Or (b(13584) << 8) Or b(13583)


    Any ideas



  • Arild Fines

    Dim foo as integer

    foo = ((foo Or B(13585 )) << 16) Or ((foo Or B(13584)) << 8) Or B(13583)

    It's simply called ....computing....



  • Rob Volk

     

    Dim foo as integer = (B(13585 ) << 16) or (B(13584) << 8)   or B(13583)

    It's simply called ....computing....



  • rad_siri

    Ok, I use the following code to read the bytes:

    charExp = ((CUInt(b(13585))) << 16) Or ((CUInt(b(13584))) << 8) Or b(13583)

    now what would I use to set the value back to the buffer I have been looking through that link you gave for the past 3 days Johan, but I cannot find what I am looking for. I got the following sub for setting a 16bit value back to the buffer:

    Private Sub ConvertToBytes(ByVal int As Integer, ByVal b As Byte(), ByVal Offset As Integer)
    Dim t As Integer = int
    b(Offset) = t And &HFF
    t = t >> 8
    If t = 0 Then Exit Sub
    b(Offset + 1) = t And &HFF
    End Sub

    That works, but only for 16bit values and only in normal order. The value I am working with is indeed a strange value, using only 3 bytes. Plus, it is in reverse order. I understand bitwise operators a little bit, it's just the reverse order thing that is giving me trouble. Any ideas on how I would write this back to the buffer



  • kosinsky

    Or, as an alternate solution:

    charExp = (CUInt(b(13585)) << 16) Or (CUInt(b(13584)) << 8) Or b(13583)

    For more info on exactly how the shift operators work in Visual Basic.NET, check out:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbls7/html/vblrfvbspec11_11.asp

    Best regards,
    Johan Stenberg



  • DennisV

    It's simply called ....Thanks, worked perfectly....


  • reading bytes in reverse order