0

I have a 40 chars text String which is written in log files as follows:

0xee, 0x36, 0xe3, 0x57, 0x2b, 0x06, 0x1e, 0x3e, 0x5a, 0x3e, 0xc0, 0x07, 0xab, 0x81, 0xa7, 0x76, 0xa4, 0x27, 0x26, 0x37

I'm able to convert it statically with some functions:

static byte[] rawbytes = toBytes(0xee, 0x36, 0xe3, 0x57, 0x2b, 0x06, 0x1e, 0x3e, 0x5a, 0x3e, 0xc0, 0x07, 0xab, 0x81, 0xa7, 0x76, 0xa4, 0x27, 0x26, 0x37); 
public static byte[] toBytes(int... ints) { // helper function
    byte[] result = new byte[ints.length];
    for (int i = 0; i < ints.length; i++) {
        result[i] = (byte) ints[i];
    }
    return result;
}

However the problem is that I cannot find a way to create the byte[] at runtime, starting from the String displayed in the logs. Any help? Thanks

Carla
  • 3,064
  • 8
  • 36
  • 65
  • 1
    Use [this function](http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java), just adapt it to skip the useless characters in your string. – Denys Séguret Mar 31 '16 at 14:11
  • Thanks! (I had to skip the "0x" part to get it working). – Carla Mar 31 '16 at 14:36

0 Answers0