64,646
社区成员
发帖
与我相关
我的任务
分享
读文件将16进制字符串转为16进制
#include <cstdlib>
#include <iostream>
using namespace std;
void assicTohex(unsigned char *param);
void strTohexes(char *pc, int nsize, unsigned char *puc);
int main(int argc, char *argv[])
{
FILE *file_in;
FILE *file_out;
int nread = 0;
char strFileNameIn[100];
char strFileNameOut[120];
char cagain = 0;
char cbuffer[2048];
unsigned char ucbuffer[1024];
int i = 0;
do {
cout < < "Input the name of the file you want to translate:" < < endl;
cin >> strFileNameIn;
file_in = fopen(strFileNameIn, "rb");
if (!file_in) {
cout < < "Open file " < < strFileNameIn < < " error!" < < endl;
cout < < "Do you want to input a file again? (Y(y) for yes and N(n) for no)" < < endl;
cin >> cagain;
if (!(cagain == 'Y' ¦ ¦ cagain == 'y' ¦ ¦ cagain == 'N' ¦ ¦ cagain == 'n')) {
do {
cout < < "Please input Y(y) for yes or N(n) for no!" < < endl;
cin >> cagain;
} while (cagain == 'Y' ¦ ¦ cagain == 'y' ¦ ¦ cagain == 'N' ¦ ¦ cagain == 'n');
}
if (cagain == 'Y' ¦ ¦ cagain == 'y') {
continue;
} else if (cagain == 'N' ¦ ¦ cagain == 'n') {
return -1;
}
}
while (strFileNameIn[i] != '.') {
strFileNameOut[i] = strFileNameIn[i];
i++;
}
strFileNameOut[i] = 'w';
strcpy(strFileNameOut + i + 1, strFileNameIn + i);
file_out = fopen(strFileNameOut, "wb");
if (!file_out) {
cerr < < "Create file " < < strFileNameOut < < " error!" < < endl;
exit(1);
}
while (!feof(file_in)) {
nread = fread(cbuffer, sizeof(char), 2048, file_in);
strTohexes(cbuffer, nread, ucbuffer);
fwrite((char *)ucbuffer, sizeof(char), nread / 2, file_out);
}
fclose(file_in);
fclose(file_out);
cout < < "Do you want to open another file?" < < endl;
cin >> cagain;
if (!(cagain == 'Y' ¦ ¦ cagain == 'y' ¦ ¦ cagain == 'N' ¦ ¦ cagain == 'n')) {
do {
cout < < "Please input Y(y) for yes or N(n) for no!" < < endl;
cin >> cagain;
} while (cagain == 'Y' ¦ ¦ cagain == 'y' ¦ ¦ cagain == 'N' ¦ ¦ cagain == 'n');
}
if (cagain == 'Y' ¦ ¦ cagain == 'y') {
clearerr(file_in);
continue;
} else if (cagain == 'N' ¦ ¦ cagain == 'n') {
return -1;
}
} while (1);
system("PAUSE");
return EXIT_SUCCESS;
}
void assicTohex(unsigned char *param)
{
if (*param >= '0' && *param <= '9')
*param -= '0';
else if (*param >= 'a' && *param <= 'f')
*param -= ('a' - 10);
else if (*param >= 'A' && *param <= 'F')
*param -= ('A' - 10);
// else
// *param = 0;
}
void strTohexes(char *pc, int nsize, unsigned char *puc)
{
int i = 0;
unsigned char uctemp = 0;
for (; i < nsize; i += 2) {
*puc = *pc++;
assicTohex(puc);
*puc < <= 4;
uctemp = *pc++;
assicTohex(&uctemp);
*puc += uctemp;
puc++;
}
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[]="06ba7b33fcf5ae0f0b258ced";
char num[2];
int i;
int hex;
num[1]=0;
for(i=0; i<strlen(str); i++)
{
num[0]=str[i];
sscanf(num, "%x", &hex);
printf("%x", hex);
}
return 0;
}
#include <cstdlib>
//////////////////////////////////////////////////////////////////////////
//
// 从内存数据转化成字符串形式
// 用函数模板和标准库,从基层开始构建,不同于先前版本,从普遍化进行细化
//
//////////////////////////////////////////////////////////////////////////
char convert_to_char( BYTE num )
{
if ( num >= 0 && num <= 9 )
return num + 0x30;
if ( num >= 10 && num <= 15 )
return num + 0x41 + 10;
return 'Z';
}
//////////////////////////////////////////////////////////////////////////
template<class T>
string convert_to_string( const T& tValue)
{
string strReturn;
const T* pTValue = &tValue;
for ( size_t dwPos = 0;
dwPos < sizeof(T);
dwPos++ )
{
strReturn += convert_to_char( *( (const BYTE*)pTValue + dwPos ) >> 4 );
strReturn += convert_to_char( *( (const BYTE*)pTValue + dwPos ) & 0x0F );
strReturn += ' ';
}
return strReturn;
}
//////////////////////////////////////////////////////////////////////////函数算子
template<class T> struct Convert2String : public binary_function<string&, T, string&>
{
public:
string& operator()( string& s , const T& tValue) {return s+= convert_to_string(tValue);}
};
//////////////////////////////////////////////////////////////////////////普通模板
template< class In >
string convert_to_string( In first, In last)
{
return accumulate( first, last, string(), Convert2String<iterator_traits<In>::value_type>() ); }
//********另一个接口
template<class T>
string convert_to_string( const T* buf, size_t size )
{
return convert_to_string( buf,buf + size );
};
void ctoi(unsigned int *b, const char *a)
{
unsigned int i;
*b=0;
for(i=0; i<8; i++)
{
*b = (*b)<<4;
switch(a[i])
{
case '0':
*b |= 0;
break;
case '1':
*b |= 1;
break;
case '2':
*b |= 2;
break;
case '3':
*b |= 3;
break;
case '4':
*b |= 4;
break;
case '5':
*b |= 5;
break;
case '6':
*b |= 6;
break;
case '7':
*b |= 7;
break;
case '8':
*b |= 8;
break;
case '9':
*b |= 9;
break;
case 'a':
*b |= 0xa;
break;
case 'b':
*b |= 0xb;
break;
case 'c':
*b |= 0xc;
break;
case 'd':
*b |= 0xd;
break;
case 'e':
*b |= 0xe;
break;
case 'f':
* b |= 0xf;
break;
default:
break;
}
}
}
void main()
{
char *a = "abcdedff";
unsigned int b;
ctoi(&b, a);
printf("%x", b);
}
template<class T>
CString convert_char( const T& value)
{
CString strRet;
strRet.Format(_T("%d"), value);
return strRet;
}
template<class T> struct Convert : public unary_function<T, CString>
{
CString strRet;
public:
CString operator() (const T& value )
{
return strRet += convert_char<T>(value);
}
CString result()
{
return strRet;
}
};
template<class VL, class T>
void convert_array(VL it_begin, VL it_end, CString& strBuf)
{
Convert<T> Res;
Res = for_each( it_begin, it_end, Res);
strBuf = Res.result();
}