Announcement

Collapse
No announcement yet.

Breaking a 32-Bit Long into 4 8-Bit Bytes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bern Ertl
    replied
    Edwin, your question is not clear, but if you mean that you used:

    Code:
    UNION BYTE_LONG
        Bytes(4) AS BYTE
        l AS LONG
    END UNION
    
    
    DIM var AS BYTE_LONG
    
    
    var.Bytes(1) = 1998
    Then I believe that PB does a type conversion and truncates the excess
    bytes before assignment to the BYTE variable. So, var.Bytes(2-4) should
    not be affected.



    ------------------
    Bernard Ertl

    Leave a comment:


  • Edwin Knoppert
    replied
    I have some weird thinghy ..

    I used the union above..

    nByte(1) = 1998
    This is not ok of course but happily for me it stored 201 (unique value)
    What happens with the other bytes (2-4)?



    ------------------

    Leave a comment:


  • Andrew Peskin
    replied
    I wanted to say thank you to all who have responded, your comments and suggestions were Incredibly helpful.

    Many thanks.

    Andrew

    ------------------

    Leave a comment:


  • Semen Matusovski
    replied
    Code:
       #Compile Exe
       #Dim All
       #Register None
    
       Function PbMain
          Dim L1 As Long, B1 As Byte, B2 As Byte, B3 As Byte, B4 As Byte
          L1 = &H01020304
          ! MOV AX, L1
          ! MOV B4, AL
          ! MOV B3, AH
          ! MOV AX, L1[2]
          ! MOV B2, AL
          ! MOV B1, AH
          MsgBox Hex$(B1, 2) + " " + Hex$(B2, 2) + " " + Hex$(B3, 2) + " " + Hex$(B4, 2)
       End Function
    ------------------
    E-MAIL: [email protected]

    Leave a comment:


  • Aldo Cavini
    replied
    You can use the following:
    Code:
    MSB: HIBYT( HIWRD( ... ) )
         LOBYT( HIWRD( ... ) )
         HIBYT( LOWRD( ... ) )
    LSB: LOBYT( LOWRD( ... ) )
    The four function used on the value -1 yill give you a 255.

    ------------------

    Leave a comment:


  • Florent Heyworth
    replied
    Or

    Code:
    UNION BYTE_LONG
        Bytes(4) AS BYTE
        l AS LONG
    END UNION
    
    FUNCTION PBMAIN AS LONG
        LOCAL pByte AS BYTE PTR
        LOCAL l AS LONG
        LOCAL u AS BYTE_LONG
        
        l = -12983434
        u.l = l
        
        'one way
        pByte = VARPTR(l)
        PRINT @pByte[0]
        PRINT @pByte[1]
        PRINT @pByte[2]
        PRINT @pByte[3]
    
        PRINT "-----"
    
        'another
        PRINT u.Bytes(0)
        PRINT u.Bytes(1)
        PRINT u.Bytes(2)
        PRINT u.Bytes(3)
        
        WAITKEY$
    
    END FUNCTION
    ------------------

    Leave a comment:


  • Paul Dwyer
    replied
    Why not use a union?

    Code:
    Type FourBytes
        One as Byte
        Two as Byte
        Three as Byte
        Four as Byte
    End type
    
    Union LongBytes
        Bytes as FourBytes
        lLong as long
    end union
    you get the idea...

    (check the byte order though)



    ------------------
    Paul Dwyer
    Network Engineer
    Aussie in Tokyo

    [This message has been edited by Paul Dwyer (edited April 04, 2001).]

    Leave a comment:


  • Andrew Peskin
    started a topic Breaking a 32-Bit Long into 4 8-Bit Bytes

    Breaking a 32-Bit Long into 4 8-Bit Bytes

    I have a small routine which I pass a 32-Bit Long Variable, and 4 8-Bit Byte Variables, which will break up the value of the long into its 4 Byte Values.

    Code:
    	Long = WWWWWWWW XXXXXXXX YYYYYYYY ZZZZZZZZ
    	Byte 1 = ZZZZZZZZ
    	Byte 2 = YYYYYYYY
    	Byte 3 = XXXXXXXX
    	Byte 4 = WWWWWWWW
    The following routine I wrote worked fine for long value which are positive.

    Code:
    Sub Break_Long_To_Bytes(L1 As Long, B1 As Byte, B2 As Byte, B3 As Byte, B4 As Byte)
    	Dim LNG_1      As Long  '// Holding variable for B1
    	Dim LNG_2      As Long  '// Holding variable for B2
    	Dim LNG_3      As Long  '// Holding variable for B3
    	Dim LNG_4      As Long  '// Holding variable for B4
    	Dim TMPlng     As Long  '// Temporary Long variable used for shifting
    
    	TMPlng = L1
    
    	LNG_1 = TMPlng Mod 256
    	TMPlng = TMPlng \ 256     '// Shift 1 Byte
    	LNG_2 = TMPlng Mod 256
    	TMPlng = TMPlng \ 256     '// Shift 1 Byte again
    	LNG_3 = TMPlng Mod 256
    	TMPlng = TMPlng \ 256     '// Shift 1 Byte again
    	LNG_4 = TMPlng Mod 256
    
    	B1 = LNG_1    '// Set the Most Right Byte -- ZZZZZZZZ
    	B2 = LNG_2    '// Set the Second Right Byte -- YYYYYYYY
    	B3 = LNG_3    '// Set the Second Left Byte -- XXXXXXXX
    	B4 = LNG_4    '// Set the Most Left Byte -- WWWWWWWW
    
    End Sub
    The problem I am encountering now is that when you try and pass the Long value as a negative number the sub, an overflow occurs, because Bytes only go from 0-255. Is there anyway to handle negative numbers properly? Any changes or modifications to the above routine?

    Any and all help would be greatly appreciated.

    Best regards,

    Andrew Peskin
    [email protected]

    ------------------
Working...
X