?? bytehexstrconvert.cpp
字號(hào):
#include "stdafx.h"
#include <stdio.h>
#include <crtdbg.h>
#include <string.h>
//作用:將 pBytes處,長(zhǎng)度為nBytes的字節(jié),轉(zhuǎn)換為十六進(jìn)制數(shù)字(字母大寫),存于str
//說明:調(diào)用程序必須保證 str的長(zhǎng)度為 2*nBytes + 1(NULL)
//返回:轉(zhuǎn)換后的字節(jié)數(shù),不包括加入的NULL,實(shí)際 == 2*nBytes
//作者:wangwwcn99@cn99.com
//最后修改:00-4-19 12:39:56
int ByteToHexStr(const unsigned char*pBytes, int nBytes, char* str)
{
if( nBytes <= 0 ) return 0;
int len = 0;
int temp;
while(nBytes--)
{
temp = *pBytes++;
len += sprintf(str, "%02X", temp );
str++; str++;
}
return len;
}
//作用:將十六進(jìn)制數(shù)字串str,轉(zhuǎn)換為BYTE,存于pBytes
//說明:調(diào)用程序必須保證pBytes 的長(zhǎng)度, str的長(zhǎng)度應(yīng)該是偶數(shù)
//返回:轉(zhuǎn)換后的字節(jié)數(shù),實(shí)際 == str長(zhǎng)度 / 2
//作者:wangwwcn99@cn99.com
//最后修改:00-4-19 12:39:56
int HexStrToByte(const char*str, unsigned char*pBytes)
{
_ASSERT( strlen( str ) % 2 == 0 );
int len = 0;
int temp;
while(*str)
{
//采用2x格式時(shí),sscanf會(huì)自動(dòng)根據(jù)數(shù)據(jù)長(zhǎng)度往后寫4個(gè)字節(jié)!!,而不是1個(gè)字節(jié)
sscanf(str, "%2X", &temp ); *pBytes++ = temp;
len++;
str++; str++;
}
return len;
}
//作用:將十六進(jìn)制數(shù)字串str,的前l(fā)en字節(jié) 轉(zhuǎn)換為BYTE,存于pBytes
//說明:調(diào)用程序必須保證pBytes 的長(zhǎng)度, len應(yīng)該是偶數(shù)
//返回:轉(zhuǎn)換得到的字節(jié)數(shù),實(shí)際就是len/2
//作者:wangwwcn99@cn99.com
//最后修改: 2000年5月27日11:49:29
int HexStrLenToByte(const char*str, int len, unsigned char*pBytes)
{
_ASSERT( len % 2 == 0 ); //must be an even number
int temp; int nReturn = len/2;
while( len )
{
//采用2x格式時(shí),sscanf會(huì)自動(dòng)根據(jù)數(shù)據(jù)長(zhǎng)度往后寫4個(gè)字節(jié)!!,而不是1個(gè)字節(jié)
sscanf(str, "%2X", &temp ); *pBytes++ = temp;
len--; len--;
str++; str++;
}
return nReturn;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -