2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
每两个作为一组,读入一个字符数组中,有没有比较好的方法?API有没有相应的函数?
谢谢!!!
6 个解决方案
#1
更正一下,上面的意思是将两个 十六进制数读入一个 字节的内存中。
#2
上面的意思是将两个 十六进制数读入一个 字节的内存中。
更
直接說出你要做什麽比較好,
更
直接說出你要做什麽比較好,
#3
int AscToHex(const unsigned char* pszAsciiBuf, int nLen, unsigned char* pszBcdBuf)
{
int i = 0;
char cTmp;
if (pszAsciiBuf == NULL)
{
return -1;
}
if (nLen & 0x01)/*判别是否为奇数*/
{
return -1 ; //奇数(可以设计左对齐右对齐)
}
for (i = 0; i < nLen; pszAsciiBuf ++, i ++)
{
if ( *pszAsciiBuf >= 'a' )
{
cTmp = *pszAsciiBuf - 'a' + 10 ;
}
else if ( *pszAsciiBuf >= 'A' )
{
cTmp = *pszAsciiBuf - 'A' + 10 ;
}
else if ( *pszAsciiBuf >= '0' )
{
cTmp = *pszAsciiBuf - '0' ;
}
else
{
cTmp = *pszAsciiBuf;
cTmp&=0x0f;
}
*pszBcdBuf ++ = cTmp << 4 | cTmp;
}
}
return 0;
}
#4
static unsigned char a2h[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char *asc2hex(const char *a, int n, char *h)
{
const unsigned char *pa;
unsigned char *ph;
int i, m;
pa = (const unsigned char *)a;
ph = (unsigned char *)h;
m = n / 2;
for (i = 0; i < m; i++, pa += 2, ph++)
*(ph) = (a2h[*pa] << 4) | a2h[*(pa + 1)];
return h;
}
#5
/* 测试程序 */
int main(int argc, char *argv[])
{
char a[] = "2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5";
char h[1024];
int i, n;
n = strlen(a);
asc2hex(a, n, h);
for (i = 0; i < n / 2; i++)
printf("%02x", (unsigned char)h[i]);
printf("\n");
printf("%s\n", a);
return 0;
}
#6
谢谢了,问题解决了
#1
更正一下,上面的意思是将两个 十六进制数读入一个 字节的内存中。
#2
上面的意思是将两个 十六进制数读入一个 字节的内存中。
更
直接說出你要做什麽比較好,
更
直接說出你要做什麽比較好,
#3
int AscToHex(const unsigned char* pszAsciiBuf, int nLen, unsigned char* pszBcdBuf)
{
int i = 0;
char cTmp;
if (pszAsciiBuf == NULL)
{
return -1;
}
if (nLen & 0x01)/*判别是否为奇数*/
{
return -1 ; //奇数(可以设计左对齐右对齐)
}
for (i = 0; i < nLen; pszAsciiBuf ++, i ++)
{
if ( *pszAsciiBuf >= 'a' )
{
cTmp = *pszAsciiBuf - 'a' + 10 ;
}
else if ( *pszAsciiBuf >= 'A' )
{
cTmp = *pszAsciiBuf - 'A' + 10 ;
}
else if ( *pszAsciiBuf >= '0' )
{
cTmp = *pszAsciiBuf - '0' ;
}
else
{
cTmp = *pszAsciiBuf;
cTmp&=0x0f;
}
*pszBcdBuf ++ = cTmp << 4 | cTmp;
}
}
return 0;
}
#4
static unsigned char a2h[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char *asc2hex(const char *a, int n, char *h)
{
const unsigned char *pa;
unsigned char *ph;
int i, m;
pa = (const unsigned char *)a;
ph = (unsigned char *)h;
m = n / 2;
for (i = 0; i < m; i++, pa += 2, ph++)
*(ph) = (a2h[*pa] << 4) | a2h[*(pa + 1)];
return h;
}
#5
/* 测试程序 */
int main(int argc, char *argv[])
{
char a[] = "2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5";
char h[1024];
int i, n;
n = strlen(a);
asc2hex(a, n, h);
for (i = 0; i < n / 2; i++)
printf("%02x", (unsigned char)h[i]);
printf("\n");
printf("%s\n", a);
return 0;
}
#6
谢谢了,问题解决了