问题描述
- 帮忙看一下这个DataDeal函数有啥好办法优化吗?
-
void DataDeal(char* data, int size) { int32_t index = 0; int32_t start = 0; int32_t end = 0; for(;index <= size;) { if (!start) { if (data[index++] == 0x00 && data[index++] == 0x00 && data[index++] == 0x00 && data[index++] == 0x01) { start = index; } } if (!end) { if (data[index++] == 0x00 && data[index++] == 0x00 && data[index++] == 0x00 && data[index++] == 0x01) { end = index -4 ; } } if (start && end) { int32_t size = end-start; char* pp = (char*)&size; memcpy(data+start-1, pp, 1); memcpy(data+start-2, pp+1, 1); memcpy(data+start-3, pp+2, 1); memcpy(data+start-4, pp+3, 1); start = end+4; end = 0; } } end = size; int32_t size_s = end-start; if (size_s) { char* pp = (char*)&size_s; memcpy(data+start-1, pp, 1); memcpy(data+start-2, pp+1, 1); memcpy(data+start-3, pp+2, 1); memcpy(data+start-4, pp+3, 1); } return; } void main() { char data[100] = {0x00, 0x00, 0x00, 0x01, 0x63, 0x00, 0x21, 0x00, 0x00, 0x00, 0x01, 0x63, 0x21, 0x63, 0x21, 0x00, 0x00, 0x00, 0x01, 0x63, 0x21,0x63, 0x21,0x63, 0x21,0x63, 0x21,0x00, 0x00, 0x00, 0x01,0x63, 0x21,0x63, 0x21,0x63, 0x21,0x63, 0x21,0x63, 0x21,0x63, 0x21,0x00, 0x00, 0x00, 0x01, 0x63, 0x21,0x63, 0x21,0x63, 0x21,0x63, 0x21, 0x63, 0x21,0x63, 0x21,0x63, 0x21, 0x00, 0x00, 0x00, 0x01}; DataDeal(data, 65); }
DataDeal函数是查找替换掉0x00, 0x00, 0x00, 0x01, 并将其数值修改为两个0x00, 0x00, 0x00, 0x01之间数据长度,并且按照打段顺序排列;
就目前来看 这个函数怎么样优化好些呢?
时间: 2024-10-27 17:21:26