Java how to concatenate 0x with variable containing hex?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeffbroodwar
    New Member
    • Oct 2006
    • 118

    Java how to concatenate 0x with variable containing hex?

    Hi,

    I want to concatenate 0x with a variable (dont know what type) to pass it in a byte array. the code below shows the original code :

    ORIGINAL CODE :

    ioData = new byte[256];

    ioData[0] = (byte)0xAA;


    the code below illustrates what i want to do....

    DESIRED CODE :

    ioData = new byte[256];
    String strHEX = "FF";


    ioData[0] = (byte)0x + strHEX;

    This way i can pass any hex value to 0x. The problem is, i can't eliminate the error..... is there a way to achieve this? i'm sure there is right? I'll just need experts like you guys to figure it out and help me with this one... ^^ thanx in advance


    Regards,
    Jeff
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    There's no need to prepend "0x" to a String value that represents a hexadecimal
    value. Have a look at the Integer.parseIn t(String rep, int radix) method. It
    does exactly what you want. Cast the result of this method back to a (byte) and
    store it in you byte array.

    kind regards,

    Jos

    Comment

    • jeffbroodwar
      New Member
      • Oct 2006
      • 118

      #3
      Thanks Jos, i didn't see that. ehehe i love this place. In this place ppl really help one another... unlike in other forums, people just ask questions and never care about helping others.... ^^ thanks again.

      Best Regards,
      Jeff

      Comment

      Working...