但对于字符串ASCIIASCII,某些输出字节错误:
C1E930990C4E87498024
结果应该是:
C1E930990C4E87C924
OP的代码没有考虑到输出的长度可能比输入的长度短。
如果输入为10个ASCII字符,则为70位。输出需要为上限(70/8)或9字节。另请参见
@Steve Summit
。
缺少
start_indx
.因为输入是
一串
(“转换ASCII字符串”),不需要输入长度。
bool ascii_to_gsmA(const char* in, uint8_t* out) {
unsigned bit_count = 0;
unsigned bit_queue = 0;
while (*in) {
bit_queue |= (*in & 0x7Fu) << bit_count;
bit_count += 7;
if (bit_count >= 8) {
*out++ = (uint8_t) bit_queue;
bit_count -= 8;
bit_queue >>= 8;
}
in++;
}
if (bit_count > 0) {
*out++ = (uint8_t) bit_queue;
}
return true;
}