-3

I have a Base64 encoded String: iQA8AJ4ANwCjADYAqQA1ALAANAC7ADMAyAAyANUAMQACATEAEwEyACMBNABDATkAUwE8AGIBQABwAUQAfgFIAIsBTQCYAVEApQFVALIBWQC+AVwAygFgANcBYgDjAWQA8AFmAPwBZwAIAmcAFQJmACECZAAsAmMAOAJgAEMCXQBNAlkAVgJWAF8CUgBnAk4AbgJKAHUCRgB7AkIAgAI+AIQCOwCIAjgAjAI1AI8CMwCSAjIA

My system is required to Base64 decode this value. The decoded data is a series of 2 byte x 2 byte y coordinates (low order byte 1st). i.e. (-1,-1) or (0xFFFF, 0xFFFF) denotes a pen up.

How can I convert Based64 encoded to Point Array in Java?

1 Answers1

0
byte[] bytes = Base64.getDecoder().decode("8AG...");
ShortBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
int npoints = bytes.length / 4;
for (int i = 0; i < npoints; ++i) {
    short x = buf.get();
    short y = buf.get();
}

To wit: wrap the bytes in a ByteBuffer. Set the byte order on little-endian, and fetch shorts.

Older answers exist for Base64. This usest the latest Base64 class to replace the older ones.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I am sorry. I missed big thing :) .Thank you for your help quickly. However, I got exception at line: byte[] bytes = Base64.getDecoder().decode("8AG...");. The exception is java.lang.IllegalArgumentException: Illegal base64 character 2e. How do I check encoded string is correct or not? – Tom Nguyen Sep 26 '17 at 12:59
  • It works well. Thank you very much. – Tom Nguyen Sep 26 '17 at 13:32