Reputation: 2472
why I am not geting the same result after encoding a string in base64 with JAVA and arduino library
Arduino code:
char data[] = "0123456789012345";
int inputLen = sizeof(data);
char encoded[100];
base64_encode(encoded, data, inputLen);
Serial.print("encoded base 64:");
Serial.println(encoded);
Arduino code result
encoded base 64:MDEyMzQ1Njc4OTAxMjM0NQA=
Java code:
static String message= "0123456789012345";
/////
String encoded = DatatypeConverter.printBase64Binary(message.getBytes());
System.out.println("encoded value is \t" + encoded);
Java code result:
encoded value is MDEyMzQ1Njc4OTAxMjM0NQ==
Why is the arduino library adding extra data at the end?
Thanks!
Upvotes: 0
Views: 510
Reputation: 2880
Because strings are null-terminated. Consequently, when you write
char data[] = "0123456789012345";
you allocate a 17-bytes string, with an hex content of
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x30 0x31 0x32 0x33 0x34 0x35 0x00
The function is adding also the terminal byte in the base64 encoding. If you want to discard it, change your line to
int inputLen = sizeof(data) - 1;
Upvotes: 1