Shop OBEX P1 Docs P2 Docs Learn Events
Converting Binary to Hex — Parallax Forums

Converting Binary to Hex

trytryagaintrytryagain Posts: 26
edited 2009-09-22 22:13 in General Discussion
Peter,

First of all thanks for all the great information
I've read from your posts.

I am new to javelin stamp and I'm trying to convert some binary inputs to hex.

The Format.printf works great printing out to the debugger
but I need these values in my code.

Is there a method to store the hex info as a string and build it as it comes in?

while (fingerReceive.byteAvailable() == true)
{
b = (char)fingerReceive.receiveByte();
Format.printf("%x",b);
}

Thanks for taking a look at this

Jeff
·

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-09-22 18:41
    You·can use Format.sprintf() and Format.bprintf() to print
    to a char[noparse]/noparse rather than System.out

    regards peter
    ·
  • trytryagaintrytryagain Posts: 26
    edited 2009-09-22 21:50
    Thanks very much Pete

    I am getting wacky results that differ from the original

    with this code:

    cIndex = 0;

    while (fingerReceive.byteAvailable() == true)
    {


    b = (char)fingerReceive.receiveByte();

    cIndex = Format.bprintf(cBuffer,cIndex,"%x",b);
    String s = Integer.toString(cBuffer[noparse][[/noparse]cIndex]);
    cIndex = cIndex + 1;
    buffer.append(s);


    }

    System.out.println(buffer.toString());

    Data seems to be getting lost with my method above.

    Is there an easier loop to concat and show the entire string?

    Thanks

    Jeff


    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-09-22 22:13
    cIndex = 0;
    while (fingerReceive.byteAvailable() == true) {
      int b = fingerReceive.receiveByte();
      cIndex = Format.bprintf(cBuffer,cIndex,"%02x",b&0xFF);
    }
    cBuffer[noparse][[/noparse]cIndex] = 0; //add closing null
    Format.printf("%s\n".cBuffer); //printf checks for closing null
    
    

    regards peter

    Post Edited (Peter Verkaik) : 9/22/2009 10:29:47 PM GMT
Sign In or Register to comment.