头文件
void logbuffer(unsigned char *buff, int len, int iclm = 16);
cpp文件
void logbuffer(unsigned char *buff,int len,int iclm ) { static const int clm = iclm; //每行被打印成16进制的字符数 只修改这一行就可以改变每行打印的字符数量 static const int linelen = clm * 3 + clm + 1;//每行的字符总数 char *Xbuffer = new char[(len/clm+1)*linelen]; memset(Xbuffer,0, (len / clm + 1)*linelen); sprintf(Xbuffer, "\r\n"); int linenum = 0; for (size_t i = 0; i < len; i++) { if (0 == (i+1)%clm ) { sprintf(Xbuffer + 3 * i+ 1 + linenum * (clm+1) , "%02X ", *(buff + i)); for (size_t j = 0; j < clm; j++) { if ( isgraph(*(buff + i - clm+1 + j)) /*|| ispunct(*(buff + i - 15 + i))*/ ) { *(Xbuffer + 3 * i + 1 + linenum * (clm + 1) + j+3) = *(buff + i - clm+1 + j); } else { *(Xbuffer + 3 * i + 1 + linenum * (clm + 1) + j+3) = ‘.‘; } } linenum++; *(Xbuffer + 3 * i + 1 + (clm + 1)*linenum+3-1) = ‘\n‘; } else { sprintf(Xbuffer + 3 * i+1+linenum* (clm + 1), "%02X ", *(buff + i)); } } //最后一行处理 if (0 != len % clm ) { for (size_t i = len % clm; i < clm; i++) { sprintf(Xbuffer + 1 + linenum * linelen+3*(i), " "); } for (size_t i = 0; i < len % clm; i++) { if ( isgraph(*(buff + clm * linenum + i))/*|| ispunct(*(buff + 16 * linenum + i))*/ ) { *(Xbuffer + 1 + linenum * linelen + clm * 3 + i) = *(buff + clm * linenum + i); } else { *(Xbuffer + 1 + linenum * linelen + clm * 3 + i) = ‘.‘; } } } //LOG(DEBUG) <<Xbuffer; delete[]Xbuffer; } //原创。转载请标明引用:https://www.cnblogs.com/liuguoyao514257665/p/9863837.html
原创。转载请标明引用:https://www.cnblogs.com/liuguoyao514257665/p/9863837.html
原文地址:https://www.cnblogs.com/liuguoyao514257665/p/9863837.html
时间: 2024-10-10 11:06:22