1

I want to split each of two chars in string and convert it to hex byte array representation, i am just lost how to do this.

in string a= hex a which is 10 in decimal in string b= hex b which is 11 in decimal

String toConvert = "abbbbbbbbbbbbbbbbbbbbbbc";
byte[] output = new byte[12];





                          Input
 ab    bb   bb    bb  bb   bb   bb   bb   bb   bb   bb   bc
                          output
[-85, -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, -68]
5
  • no i want a byte array ass output not string and its string to hex not hex to string. Commented Nov 22, 2014 at 15:09
  • You reserved 12 bytes for a 24 charackter string? Commented Nov 22, 2014 at 15:13
  • stackoverflow.com/questions/5886619/… Commented Nov 22, 2014 at 15:16
  • example (ab) a is msb and b is lsb i want to combine them and make a 16bit no than convert it to hex Commented Nov 22, 2014 at 15:18
  • 1
    in string a= hex a which is 10 in decimal in string b= hex b which is 11 in decimal You mean in String toConvert? What a terrible example! Better String toConvert = "0344A7DF";. And you will not convert to hex but decode from hex representation. Commented Nov 22, 2014 at 15:24

3 Answers 3

1

Takes the first character in a group of two, multiplies its hex value by 16 (it's in the 161 place). That result is added to the second character's hex value.

String toConvert = "abbbbbbbbbbbbbbbbbbbbbbc";
byte[] output = new byte[toConvert.length() / 2];

for (int i = 0; i < output.length; i++) {
    output[i] |= Character.digit(toConvert.charAt(i * 2), 16) * 16;
    output[i] |= Character.digit(toConvert.charAt(i * 2 + 1), 16);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Apache Common Codec's Hex class does exactly what you need:

byte[] bytes = Hex.decodeHex("abbbbbbbbbbbbbbbbbbbbbbc".toCharArray());

If you can't/won't use third parties, you can always "borrow" their implementation (slightly simplified - I omitted correctness checks for simplicity):

public static byte[] decodeHex(final char[] data) {

  final int len = data.length;

  // Handle empty string - omitted for clarity's sake

  final byte[] out = new byte[len >> 1];

  // two characters form the hex value.
  for (int i = 0, j = 0; j < len; i++) {
      int f =  Character.digit(data[j], 16) << 4;
      j++;
      f = f | Character.digit(data[j], 16);
      j++;
      out[i] = (byte) (f & 0xFF);
  }

  return out;
}

Comments

0
for(int i = 0; i < 12; i++) {
    output[i] = (byte) Integer.parseInt(toConvert.substring(i*2, i*2+2), 16);
}

7 Comments

Primitives don't have methods. Maybe you meant Integer.valueOf?
No, I meant what I wrote :) Integer is not a primitive. Just try running it, you'll see.
@Dima I'd run it, if I could (does not compile, because of what has been stated above).
sure it does, c'mon. Try it.
Code only answers are discouraged. It is always better to explain the what, why, and how.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.