Page 1 of 1

Converting to HEX and deHex increase end String value + 1

Posted: Thu Dec 30, 2021 3:02 pm
by tsctrl
Hi all,

I have small issue that i cant figure out why.

uint8_t nonce[16] = "1234567890123456";
String cipherText = String();
for (int i = 0; i < 17; i++) {
char str[3];
sprintf(str, "%02X", nonce);
cipherText = cipherText+String(str);
}
System.println(cipherText);

result is: "31323334353637383930313233343537"
when i use hex converter online the de hex result is "1234567890123457"

hex converter online: https://string-functions.com/hex-string.aspx

The last value will always increase + 1
Anyone can help whats wrong with this codes?

Re: Converting to HEX and deHex increase end String value + 1

Posted: Thu Dec 30, 2021 5:13 pm
by tsctrl
unsigned char nonce[16] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};

ESP_LOGE(TAG, "[LOGGER]: nonce. %x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x", nonce[0], nonce[1], nonce[2], nonce[3], nonce[4], nonce[5], nonce[6], nonce[7], nonce[8], nonce[9], nonce[10], nonce[11], nonce[12], nonce[13], nonce[14], nonce[15]);

[LOGGER]: nonce. 1111111111111112 :D :D :D :D :D :D :D

Re: Converting to HEX and deHex increase end String value + 1

Posted: Thu Dec 30, 2021 6:34 pm
by chegewara
tsctrl wrote:
Thu Dec 30, 2021 3:02 pm
Hi all,

I have small issue that i cant figure out why.

uint8_t nonce[16] = "1234567890123456";
String cipherText = String();
for (int i = 0; i < 17; i++) {
char str[3];
sprintf(str, "%02X", nonce);
cipherText = cipherText+String(str);
}
System.println(cipherText);

result is: "31323334353637383930313233343537"
when i use hex converter online the de hex result is "1234567890123457"

hex converter online: https://string-functions.com/hex-string.aspx

The last value will always increase + 1
Anyone can help whats wrong with this codes?

But you know you are trying to read beyond nonce array? What i mean you are reading 17 elements from array size of 16.

Re: Converting to HEX and deHex increase end String value + 1

Posted: Thu Dec 30, 2021 6:42 pm
by tsctrl
yup, tested with 16 with the same issue.

Re: Converting to HEX and deHex increase end String value + 1

Posted: Thu Dec 30, 2021 6:50 pm
by tsctrl
i have use base64 also with the same issue. and also use other loop approach.

thanks!

Re: Converting to HEX and deHex increase end String value + 1

Posted: Fri Dec 31, 2021 2:20 am
by tsctrl
hi @chegewara, yes you correct. the issue is the iv need to be copy as on your other reply on other post here
viewtopic.php?f=2&t=25269
thanks you so much and happy new year!