locked
BYTE type casting RRS feed

  • Question

  • Hello everyone,

    I'm new to VB and I'm trying to convert some C codes into VB but I can't seem to get it working...My guess is that I did something in the type casting in VB..


    C codes:
    ULONG    AccessBytes;
    char iChar[2];
    int iBytes = ( BYTE )( AccessBytes >>  0 );
    wsprintf( iChar, "%d", iBytes );  //iChar would return 1


    I did the following in VB:

    Dim iBytes,  As Byte
    Dim AccessBytes As Long
    iBytes = System.Text.Encoding.ASCII.GetByteCount(AccessBytes >> 0)  //iBytes would return 6 here.

    but two iBytes don't match..

    thanks a lot
    Wednesday, January 14, 2009 7:58 AM

Answers

  • I guess AccessBytes is a 32 bit value then. The bits are laid out like this:

    00000000 WWWWWWWW RRRRRRRR IIIIIIII

    Where 0 are bits that aren't used, W are the bits encoding the write count, R are the bits encoding the read count and I are the bits encoding the index count.

    263169 is:
    00000000 00000100 00000100 00000001

    If you take the bits out then:

    I = 1 binary = 1 base 10
    R = 100 binary = 4 base 10
    W = 4 base 10 too.

    The C cast to byte discards everything in the first 3 bytes:

    DDDDDDDD DDDDDDDD DDDDDDDD OOOOOOOO

    D = bits discarded by the (Byte) cast.
    O = bits kept after the (Byte) cast.

    So, the code shifts the bits to the right, and then casts, leaving only the lowest 8 bits...
    (00000000 00000100 00000100 00000001) >> 0 = 00000000 00000100 00000100 00000001
    (Byte) 00000000 00000100 00000100 00000001 = 00000001

    (00000000 00000100 00000100 00000001) >> 8 = 00000000 00000100 00000100
    (Byte) 00000000 00000100 00000100 = 00000100

    (00000000 00000100 00000100 00000001) >> 16 = 00000000 00000100
    (byte) 00000000 00000100 = 00000100


    Ok?

    In vb.net just do:

    Dim indexBytes As Integer = AccessBytes And &HFF
    Dim readBytes As Integer = (AccessBytes >> 8) And &HFF
    Dim writeBytes As Integer = (AccessBytes >> 16) And &HFF

    Here we do the shifts just like in C, but instead of the cast to Byte we And with &HFF which sets all the higher bits to 0.

    00000000 00000100 00000100 00000001
    00000000 00000000 00000000 11111111 &HFF
    ------------------------------------------------------ AND
    00000000 00000000 00000000 00000001



    • Marked as answer by emNull Friday, January 16, 2009 3:11 AM
    Friday, January 16, 2009 3:01 AM

All replies

  • The C code doesn't make sense to me, where's the rest of it?

    Look at the subject "Windows data types" in the msdn library.

    C ULONG = unsigned 32 bit integer.
    VB.Net Long = signed 64 bit integer.  -- use UInteger

    C Char = 8 bit ansi character.
    Vb.Net Char = 16 bit unicode character.  -- use Byte

    C array declaration - uses the size of the array
    Vb.Net array declaration - uses the upper bound of the array. -- use Dim iChar(1) As Byte

    >> 0 has no effect in VB.Net. I've no idea what it does in C, probably something clever and complicated.


    For the vb.net - switch on Option Strict at the top of the code file...

    At the moment this is how it is evaluated - the compiler is adding all sorts of conversions behind the scenes to get it to work:

    iBytes = ASCII.GetByteCount(AccessBytes >> 0) ' AccessBytes has the default value 0
    iBytes = ASCII.GetByteCount(0 >> 0)  ' Bit shift of 0 has no effect
    iBytes = ASCII.GetByteCount(0) ' There is no overload that accepts an Integer (0 is an Int32), So it is converted to string, as there is an overload for that.
    iBytes = ASCII.GetByteCount("0") ' There is 1 Byte in the ASCII string "0".
    iBytes = 1 ' The return value is cast to Byte

    I've no idea why you say it should return 6.

    What should the code do?

    Wednesday, January 14, 2009 11:49 AM
  • Hi jo0ls,

    thanks for your reply, heres the code snippet:

    the code is to get the # of bytes for accesses accordingly (index, read, and write)

    I ran through the debug mode and I couldn't get the values of iBytes, rBytes, wBytes (saying the symbol not found)

    what does i.e. ( BYTE )( AccessBytes >>  8 ) do anyway? I know its just bitshift operation without (BYTE) type casting..
     

    the AccessBytes has the value of 263169.

    // Display # of bytes for accesses.
        hIbytes = GetDlgItem( hDlg, IDC_I2C_EDIT_I );
        hRbytes = GetDlgItem( hDlg, IDC_I2C_EDIT_R );
        hWbytes = GetDlgItem( hDlg, IDC_I2C_EDIT_W );
        
        iBytes  = ( BYTE )( AccessBytes >>  0 );
        rBytes  = ( BYTE )( AccessBytes >>  8 );
        wBytes  = ( BYTE )( AccessBytes >> 16 );

        wsprintf( iChar, "%d", iBytes );     // iChar = 1
        wsprintf( rChar, "%d", rBytes );    //rChar = 4
        wsprintf( wChar, "%d", wBytes ); //wChar = 4
        SetWindowText( hIbytes, iChar );
        SetWindowText( hRbytes, rChar );
        SetWindowText( hWbytes, wChar );


    thanks again
    Thursday, January 15, 2009 2:14 AM
  • I guess AccessBytes is a 32 bit value then. The bits are laid out like this:

    00000000 WWWWWWWW RRRRRRRR IIIIIIII

    Where 0 are bits that aren't used, W are the bits encoding the write count, R are the bits encoding the read count and I are the bits encoding the index count.

    263169 is:
    00000000 00000100 00000100 00000001

    If you take the bits out then:

    I = 1 binary = 1 base 10
    R = 100 binary = 4 base 10
    W = 4 base 10 too.

    The C cast to byte discards everything in the first 3 bytes:

    DDDDDDDD DDDDDDDD DDDDDDDD OOOOOOOO

    D = bits discarded by the (Byte) cast.
    O = bits kept after the (Byte) cast.

    So, the code shifts the bits to the right, and then casts, leaving only the lowest 8 bits...
    (00000000 00000100 00000100 00000001) >> 0 = 00000000 00000100 00000100 00000001
    (Byte) 00000000 00000100 00000100 00000001 = 00000001

    (00000000 00000100 00000100 00000001) >> 8 = 00000000 00000100 00000100
    (Byte) 00000000 00000100 00000100 = 00000100

    (00000000 00000100 00000100 00000001) >> 16 = 00000000 00000100
    (byte) 00000000 00000100 = 00000100


    Ok?

    In vb.net just do:

    Dim indexBytes As Integer = AccessBytes And &HFF
    Dim readBytes As Integer = (AccessBytes >> 8) And &HFF
    Dim writeBytes As Integer = (AccessBytes >> 16) And &HFF

    Here we do the shifts just like in C, but instead of the cast to Byte we And with &HFF which sets all the higher bits to 0.

    00000000 00000100 00000100 00000001
    00000000 00000000 00000000 11111111 &HFF
    ------------------------------------------------------ AND
    00000000 00000000 00000000 00000001



    • Marked as answer by emNull Friday, January 16, 2009 3:11 AM
    Friday, January 16, 2009 3:01 AM
  •  Hello jo0ls,

    Thank you so much for such a detailed explaiantion

    It works just fine now.

    I really appreciate it.

    allen

     

    Friday, January 16, 2009 3:11 AM