📄 util.c
字号:
/****************************************************************************** SCT, Serial Communication Tracer ** CopyRight (C) Bruce (ZhaoFei), zhaofei@zflogic.com* http://www.zflogic.com** All sources in the package are copyrighted under the the terms of GNU GPL****************************************************************************** * This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA** If you intend to use this pragram commercially on a regular basis you* must contact the author for an appropriate donation.* ***************************************************************************/#include "stdhdr.h"#define UTIL_L#include "util.h"void hex2str(char *str, uchar *hex,int len){ int i; uchar l4,h4; for(i=0; i<len; i++) { h4=(hex[i] & 0xf0)>>4; l4=hex[i] & 0x0f; if (h4<=9) str[2*i] = h4 + ('0' -0); else str[2*i] = h4 + ('A'-10); if (l4<=9) str[2*i+1] = l4 + ('0' -0); else str[2*i+1] = l4 + ('A'-10); } str[2*i]=0;}uchar asc2hex(char asccode){ uchar ret; if('0'<=asccode && asccode<='9') ret=asccode-'0'; else if('a'<=asccode && asccode<='f') ret=asccode-'a'+10; else if('A'<=asccode && asccode<='F') ret=asccode-'A'+10; else ret=0; return ret;}void ascs2hex(uchar *hex,uchar *ascs,int srclen){ uchar l4,h4; int i,lenstr; lenstr = srclen; if(lenstr==0){ return; } if(lenstr%2) return; for(i=0; i<lenstr; i+=2){ h4=asc2hex(ascs[i]); l4=asc2hex(ascs[i+1]); hex[i/2]=(h4<<4)+l4; }}void str2hex(uchar *hex, int *len, char *str){ uchar l4,h4; int i,lenstr; lenstr = strlen(str); if(lenstr==0){ *len = 0; return; } for(i=0; i<lenstr-(lenstr%2); i+=2){ h4=asc2hex(str[i]); l4=asc2hex(str[i+1]); hex[i/2]=(h4<<4)+l4; } if(lenstr%2) hex[(lenstr+1)/2-1]=asc2hex(str[lenstr-1]) << 4; *len=(lenstr+1)/2;}void noz_str(char *str){ str[strlen(str)]=' ';}#define fillfmt(x,y,z) do{ sprintf(x,y,z); noz_str(x); } while(0)void disp_hexasc(uchar *data, int size){ int i; bool inpara; //16 bytes for a para,and displayed at same line char outln[100]; memset(outln,' ',sizeof(outln)); for (i=0; i<size; i++) { if(!(i%16)) fillfmt(&outln[0],"%04X",i); if((i%16)<8) { fillfmt(&outln[(i%16)*3+9],"%02X ",data[i]); } else { fillfmt(&outln[8*3+9],"%s","-"); fillfmt(&outln[(i%16)*3+9+2],"%02X ",data[i]); } fillfmt(&outln[64+(i%16)],"%c", (data[i]>=' ' ? data[i]: '.')); if (! ((i+1)%16) || i == (size-1)) { outln[80]=0; printf("%s\n",outln); memset(outln,' ',sizeof(outln)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -