Fix IrRemote_Full JSON leading zeroes (#10680)

This commit is contained in:
Theo Arends 2021-02-01 17:46:28 +01:00
parent bb01fab85a
commit a9cbfbe6f7
2 changed files with 35 additions and 10 deletions

View File

@ -129,6 +129,7 @@ void * __va_cur_ptr4(va_list &va) {
* Genral function to convert u64 to hex
\*********************************************************************************************/
// Simple function to print a 64 bits unsigned int
/*
char * U64toHex(uint64_t value, char *str) {
// str must be at least 17 bytes long
str[16] = 0; // end of string
@ -139,6 +140,29 @@ char * U64toHex(uint64_t value, char *str) {
}
return str;
}
*/
char * U64toHex(uint64_t value, char *str, uint32_t zeroleads) {
// str must be at least 17 bytes long
str[16] = 0; // end of string
for (uint32_t i=0; i<16; i++) { // 16 digits
uint32_t n = value & 0x0F;
str[15 - i] = (n < 10) ? (char)n+'0' : (char)n-10+'A';
value = value >> 4;
}
if (zeroleads < 16) {
uint32_t max_zeroes = 16 - zeroleads;
while (max_zeroes) {
if (str[0] == '0') {
memmove(str, str +1, strlen(str));
} else {
break;
}
max_zeroes--;
}
}
return str;
}
// see https://stackoverflow.com/questions/6357031/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-in-c
// char* ToHex_P(unsigned char * in, size_t insz, char * out, size_t outsz, char inbetween = '\0'); in tasmota_globals.h
@ -308,7 +332,8 @@ int32_t ext_vsnprintf_P(char * buf, size_t buf_len, const char * fmt_P, va_list
// '%_X' outputs a 64 bits unsigned int to uppercase HEX with 16 digits
case 'X': // input is `uint64_t*`, printed as 16 hex digits (no prefix 0x)
{
U64toHex(*(uint64_t*)cur_val, hex);
if ((decimals < 0) || (decimals > 16)) { decimals = 16; }
U64toHex(*(uint64_t*)cur_val, hex, decimals);
new_val_str = copyStr(hex);
allocs[alloc_idx++] = new_val_str;
}

View File

@ -250,7 +250,7 @@ void sendIRJsonState(const struct decode_results &results) {
} else {
if (UNKNOWN != results.decode_type) {
uint64_t reverse = reverseBitsInBytes64(results.value);
ResponseAppend_P(PSTR("\"0x%_X\",\"" D_JSON_IR_DATALSB "\":\"0x%_X\""),
ResponseAppend_P(PSTR("\"0x%0_X\",\"" D_JSON_IR_DATALSB "\":\"0x%0_X\""),
&results.value, &reverse);
} else { // UNKNOWN
ResponseAppend_P(PSTR("\"0x&08X\""), (uint32_t) results.value); // Unknown is always 32 bits