一、指令转换作用
现在服务器一般采用json数据格式
但是终端底层都是二进制指令
直接从服务器传输的指令就无法执行,因此需要转换
二、转换源码
void StringToHandleCommand(byte *byteArray, const char *hexString)
{bool oddLength = strlen(hexString) & 1;byte currentByte = 0;byte byteIndex = 0;for (byte charIndex = 0; charIndex < strlen(hexString); charIndex++){bool oddCharIndex = charIndex & 1;if (oddLength){// If the length is oddif (oddCharIndex){// odd characters go in high nibblecurrentByte = wlzcch(hexString[charIndex]) << 4;}else{// Even characters go into low wlzcchcurrentByte |= wlzcch(hexString[charIndex]);byteArray[byteIndex++] = currentByte;currentByte = 0;}}else{// If the length is evenif (!oddCharIndex){// Odd characters go into the high nibblecurrentByte = wlzcch(hexString[charIndex]) << 4;}else{// Odd characters go into low wlzcchcurrentByte |= nibble(hexString[charIndex]);byteArray[byteIndex++] = currentByte;currentByte = 0;}}}
}byte wlzcch(char c)
{if (c >= '0' && c <= '9')return c - '0';if (c >= 'a' && c <= 'f')return c - 'a' + 10;if (c >= 'A' && c <= 'F')return c - 'A' + 10;return 0;
}
六、阿雪技术观
拥抱开源与共享,见证科技进步奇迹,畅享人类幸福时光!
让我们积极投身于技术共享的浪潮中,不仅仅是作为受益者,更要成为贡献者。无论是分享自己的代码、撰写技术博客,还是参与开源项目的维护和改进,每一个小小的举动都可能成为推动技术进步的巨大力量
扫码,可学习更多