integers to bytes


Last Updated:

  1. XsCode

    XsCode Member This Topic's Starter

    Joined:
    Jul 14, 2010
    Messages:
    7
    Likes Received:
    0
    Hi all,

    I'm new to Java programming, coming from VB6 programming many years back.

    I'm trying to send a HCI command, but the params are sent as bytes and I can't seem to get the integer I want to send to form the correct bytes.

    my example integer is 974 which when using hcidump is sent as 00 78 82. I understand that 0x78 * 0x82 = 0x3CF ie 974, but I can't work out how to get the bytes from the integer.

    I've tried bit shifting with..

    Code (Text):
    1.  
    2. byte High = (byte) (myInt);
    3. byte Low = (byte) ((myInt >> 8) & 0xFF);
    4.  
    but i get -50 and 03 ???

    Can anyone explain where i'm going wrong?

    TIA
     

    Advertisement
  2. IanGClifton

    IanGClifton Well-Known Member

    Joined:
    Feb 7, 2010
    Messages:
    111
    Likes Received:
    19
    byte[] bytes = ByteBuffer.allocate(capacity).putInt(myInt).array();
     

Share This Page

Loading...