資源簡介
簡單的兩個函數:
一個將十六進制轉換成字符串,
一個將字符串轉換成十六進制數值。
VC實現。
一個將十六進制轉換成字符串,
一個將字符串轉換成十六進制數值。
VC實現。
代碼片段和文件信息
//?FormatBytes.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“stdafx.h“
#include?
#include?
//?字節數據轉換為可打印字符串
//?如:{0xC8?0x32?0x9B?0xFD?0x0E?0x01}?-->?“C8329BFD0E01“?
//?輸入:?pSrc?-?源數據指針
//???????nSrcLength?-?源數據長度
//?輸出:?pDst?-?目標字符串指針
//?返回:?目標字符串長度
int?gsmBytes2String(const?unsigned?char*?pSrc?char*?pDst?int?nSrcLength)
{
const?char?tab[]=“0123456789ABCDEF“; //?0x0-0xf的字符查找表
for?(int?i?=?0;?i? {
*pDst++?=?tab[*pSrc?>>?4]; //?輸出高4位
*pDst++?=?tab[*pSrc?&?0x0f]; //?輸出低4位
pSrc++;
}
//?輸出字符串加個結束符
*pDst?=?‘\0‘;
//?返回目標字符串長度
return?(nSrcLength?*?2);
}
//?可打印字符串轉換為字節數據
//?如:“C8329BFD0E01“?-->?{0xC8?0x32?0x9B?0xFD?0x0E?0x01}
//?輸入:?pSrc?-?源字符串指針
//???????nSrcLength?-?源字符串長度
//?輸出:?pDst?-?目標數據指針
//?返回:?目標數據長度
int?gsmString2Bytes(const?char*?pSrc?unsigned?char*?pDst?int?nSrcLength)
{
for?(int?i?=?0;?i? {
//?輸出高4位
if?((*pSrc?>=?‘0‘)?&&?(*pSrc?<=?‘9‘))
{
*pDst?=?(*pSrc?-?‘0‘)?<4;
}
else
{
*pDst?=?(*pSrc?-?‘A‘?+?10)?<4;
}
pSrc++;
//?輸出低4位
if?((*pSrc>=‘0‘)?&&?(*pSrc<=‘9‘))
{
*pDst?|=?*pSrc?-?‘0‘;
}
else
{
*pDst?|=?*pSrc?-?‘A‘?+?10;
}
pSrc++;
pDst++;
}
//?返回目標數據長度
return?(nSrcLength?/?2);
}
int?main(int?argc?char*?argv[])
{
char?intSprint[255]?=?“C8329BFD0E0199999999“;
unsigned?char?outBytes[255]?=?{0};
int?szLength?=?0;
int?byLength?=?0;
byLength?=?gsmString2Bytes(intSprintoutBytesstrlen(intSprint));
szLength?=?gsmBytes2String(outBytesintSprintstrlen(intSprint)/2);
printf(“%s\n“intSprint);
return?0;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????1557504??2008-09-24?14:04??FormatBytes\Debug\FormatBytes.bsc
?????文件?????155723??2008-09-24?14:04??FormatBytes\Debug\FormatBytes.exe
?????文件?????164548??2008-09-24?14:04??FormatBytes\Debug\FormatBytes.ilk
?????文件??????23592??2008-09-24?14:04??FormatBytes\Debug\FormatBytes.obj
?????文件?????269264??2008-09-24?12:02??FormatBytes\Debug\FormatBytes.pch
?????文件?????402432??2008-09-24?14:04??FormatBytes\Debug\FormatBytes.pdb
?????文件??????????0??2008-09-24?14:04??FormatBytes\Debug\FormatBytes.sbr
?????文件???????2051??2008-09-24?12:02??FormatBytes\Debug\StdAfx.obj
?????文件???????4072??2008-09-24?12:02??FormatBytes\Debug\StdAfx.sbr
?????文件??????82944??2008-09-24?14:04??FormatBytes\Debug\vc60.idb
?????文件?????126976??2008-09-24?14:04??FormatBytes\Debug\vc60.pdb
?????文件???????1833??2008-09-24?14:04??FormatBytes\FormatBytes.cpp
?????文件???????4600??2008-06-18?19:46??FormatBytes\FormatBytes.dsp
?????文件????????545??2008-06-18?10:18??FormatBytes\FormatBytes.dsw
?????文件??????41984??2008-09-24?14:04??FormatBytes\FormatBytes.ncb
?????文件??????55808??2008-09-24?14:04??FormatBytes\FormatBytes.opt
?????文件???????1536??2008-09-24?14:04??FormatBytes\FormatBytes.plg
?????文件????????113??2008-09-24?14:04??FormatBytes\FormatBytes.positions
?????文件???????1238??2008-06-18?10:18??FormatBytes\ReadMe.txt
?????文件????????298??2008-06-18?10:18??FormatBytes\StdAfx.cpp
?????文件????????769??2008-06-18?10:18??FormatBytes\StdAfx.h
?????目錄??????????0??2008-09-24?14:04??FormatBytes\Debug
?????目錄??????????0??2008-09-24?14:04??FormatBytes
-----------?---------??----------?-----??----
??????????????2897830????????????????????23
評論
共有 條評論