📄 hexa.c
字号:
/* ============================================================================ Project Name : jayaCard TCK Module Name : proto/tck/common/hexa.c Version : $Id: hexa.c,v 1.6 2004/01/11 09:56:34 dgil Exp $ Description: common sources for hexa manipulation The Original Code is jayaCard TCK code. The Initial Developer of the Original Code is Gilles Dumortier Portions created by the Initial Developer are Copyright (C) 2002-2004 the Initial Developer. All Rights Reserved. Contributor(s): Philippe Fremy (phil@freehackers.org) Permission is granted to any individual to use, copy, or redistribute this software so long as all of the original files are included unmodified, that it is not sold for profit, and that this copyright notice is retained. 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. History Rev Description 022303 dgil wrote it from reader.c add string manipulation ============================================================================*/#include "precomp.h"/* ============================================================================ hexa2byte() note that c2 can be \0 ========================================================================= */jbyte hexa2byte(char c1,char c2){ jbyte b = 0x00; if ((c1>='0') && (c1<='9')) { b = (c1 - '0')<<4; } if ((c1>='a') && (c1<='f')) { b = (c1 - 'a' + 10)<<4; } if ((c1>='A') && (c1<='F')) { b = (c1 - 'A' + 10)<<4; } if ((c2>='0') && (c2<='9')) { b |= (c2 - '0'); } if ((c2>='a') && (c2<='f')) { b |= (c2 - 'a' + 10); } if ((c2>='A') && (c2<='F')) { b |= (c2 - 'A' + 10); } /* LOG5("HEXA","0x%.2X 0x%.2X %c%c 0x%.2X ",c1,c2,c1,c2,b); */ return b;}/* ============================================================================ hexa2bytes() warning: buf must be correctly sized to receive all the bytes. ========================================================================= */jword hexa2bytes(jbyte* buf,char *str){ jword len = 0; while (*str!='\0') { while (isspace(*str)) { str++; } buf[len] = hexa2byte(str[0],str[1]); str +=2; len++; } return len;}/* ============================================================================ byte2hexa() fill 3 chars with each hexadecimal digits then '\0' ========================================================================= */static jbyte __byte[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };void byte2hexa(jbyte b,char* s){ s[0] = __byte[b>>4]; s[1] = __byte[b&0x0F]; s[2] = '\0';}/* ============================================================================ bytes2hexa() warning: str must be correctly sized to receive all the characters. ========================================================================= */void bytes2hexa(char* str,jbyte* buf,jword len,char sep){ jword i = 0; *str = '\0'; while (i<len) { byte2hexa(buf[i],str); str += 2; i++; if ((sep!='\0') && (i<len)) { *str++ = sep; *str = '\0'; } }}/* ============================================================================ That's all folks ! ========================================================================= */#ifdef TEST_HEXA#include <stdio.h>int main(){ char s[1024]; jbyte b; jbyte tab[10]; int i; b = 4; byte2hexa( b, s ); printf("byte2hexa %x: '%s'\n", b, s ); b = 64; byte2hexa( b, s ); printf("byte2hexa %x: '%s'\n", b, s ); for( i=0; i<10; i++) tab[i] = i; bytes2hexa( s, tab, 10, '\0' ); printf("bytes2hexa : '%s'\n", s ); return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -