Shop OBEX P1 Docs P2 Docs Learn Events
Integer to hex to byte — Parallax Forums

Integer to hex to byte

trytryagaintrytryagain Posts: 26
edited 2009-11-08 09:23 in General Discussion
confused.gif·Javelin Stamp
·
If anyone can help, I would apprceiate it:

I am trying to convert an integer to hex
always with a prefix of "0x"
then converted to a·sendable byte

If my integer is 31, I need to convert it to a·hex 0x1F.
Then·hex 0x1F has to be converted to type byte so I
can send it like this:

fSend.sendByte(byte);

Any sample code for converting any integer < 255
to this byte would be appreciated

Thanks

Jeff (trytryagain)

·

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-09-24 18:15
    If you convert 31 to "0x1F", then you cannot convert 1F to be of type byte.
    Either send '1', then send 'F', or send 31 directly as in send(31)

    regards peter
  • trytryagaintrytryagain Posts: 26
    edited 2009-09-24 19:22
    Pete,

    My problem is that the receiving processor
    is expecting a series of hex commands in order.

    It has hex check sums and some other
    complicated issues.

    The only way the program acknowledges the
    transmission is when I send it like this:

    fSend.sendByte(0x40); //Start Packet
    fSend.sendByte(0x02); //Save System Parameter Command
    fSend.sendByte(0x12); <= RIGHT HERE IS WHERE I NEED TO SEND MY CONVERTED DATA
    fSend.sendByte(0x00); //Some other stuff
    fSend.sendByte(0x00);········· |
    fSend.sendByte(0x00);········· |
    fSend.sendByte(0x00);········· |
    fSend.sendByte(0x00);········· |
    fSend.sendByte(0x00);········· |
    fSend.sendByte(0x00);········· |
    fSend.sendByte(0x00);··········|
    fSend.sendByte(0x57); //Check Sum
    fSend.sendByte(0x0A); //End Packet

    I know how to convert the int to hex but
    I can't figure out how to get that
    into a variable of type byte with the 0x concatinated
    as a prefix.

    Any other ideas Peter? ·I'm really stuck

    Thanks,

    Jeff

    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-09-24 20:05
    It appears you just need·to send the binary value.
    fSend.sendByte(0x12);
    is the same as
    fSend.sendByte(18);
    and so
    fSend.sendByte(0x1F);
    is the same as
    fSend.sendByte(31);

    So if your data is in int value,
    just use
    fSend.sendByte(value);
    or
    fSend.sendByte((byte)value); //if parameter must be type byte

    regards peter
    ·
  • trytryagaintrytryagain Posts: 26
    edited 2009-09-24 21:12
    smhair.gif·Peter,

    Neither of those 2 methods work.

    I would really like to know what the
    data stream actually looks like as
    it is sent out the port.

    When this gets sent
    fSend.sendByte(0x12);
    it is not·the same as:
    fSend.sendByte(18);
    or the same as:
    fSend.sendByte((byte)18);

    The program only accepts the 0x12

    If I make a byte array containing
    each possible value up to 255 is
    the only way I can think of.

    static byte[noparse]/noparse myByte = new byte[noparse]/noparse;
    myByte[noparse][[/noparse]1] = 0x01;·
    myByte[noparse][[/noparse]2] = 0x02;·
    myByte[noparse][[/noparse]3] = 0x03;·
    myByte[noparse][[/noparse]4] = 0x04;·
    myByte[noparse][[/noparse]5] = 0x05;·
    myByte[noparse][[/noparse]6] = 0x06;·
    myByte[noparse][[/noparse]7] = 0x07;·
    myByte[noparse][[/noparse]8] = 0x08;·
    myByte[noparse][[/noparse]9] = 0x09;·
    myByte[noparse][[/noparse]10] = 0x0A;·
    myByte[noparse][[/noparse]11] = 0x0B;·
    ...

    cool.gif·Is there any way to string together the right
    side so it doesn't blow up because of mismatched
    types?! This stuff is really strongly typed!
    I'm used to VB & JavaScript!

    Thanks for trying Pete

    Jeff






    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-09-24 22:00
    fSend.sendByte(18) IS identical to fSend.sendByte(0x12)

    If it doesn't get accepted I suspect the mentioned checksum
    is calculated wrong.

    Attach your entire program and a description of the protocol
    then I can give proper advise.

    regards peter
  • trytryagaintrytryagain Posts: 26
    edited 2009-09-24 22:35
    yeah.gif·Peter,

    You are correct sir. My check sum
    was using 12 instead of 18. Another
    mystery solved.

    You just made my program alot smaller!

    Thank you very much Peter.

    Jeff
  • mamun02mamun02 Posts: 1
    edited 2009-11-08 07:26
    I have a byte array and I need to convert the first two bytes to an integer as though they're one byte, but they have to be read backwards.

    ie. The first byte is A1 and the second byte is B2. I need to convert them as B2A1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    sejour de luxe - Centre formation a distance
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-11-08 09:23
    int value = (array[noparse][[/noparse]0] & 255) + (array[noparse][[/noparse]1] << 8)

    regards peter
Sign In or Register to comment.