怎样将16进制字符串转换成16进制?

yshuise 2008-06-06 09:40:43
"06ba7b33fcf5ae0f0b258ced9b9688e8a87f93db8efdacd17ec2401f2e7dac03
bf5515b9e42d78a8037b743d280529fc7630d62bd4c75492c78fd28b2c2aba67
9cc93d471b097ea30e29f7c89735dd88b4c3e9cb32e14ef50432a6ab37870a14
af81e471af496fa0e292e1e8168461acf6191017048deb62dd5fd7c784ba88aa
a921563b3143218981a38441910687a3202a5135e58ce2ecdf9c2d521c6df45b
aab245f99427574c148904e2a60104e97e6b05bfd9be9a086246a797a03dab25"

变成:
06ba7b33fcf5ae0f0b258ced9b9688e8a87f93db8efdacd17ec2401f2e7dac03
bf5515b9e42d78a8037b743d280529fc7630d62bd4c75492c78fd28b2c2aba67
9cc93d471b097ea30e29f7c89735dd88b4c3e9cb32e14ef50432a6ab37870a14
af81e471af496fa0e292e1e8168461acf6191017048deb62dd5fd7c784ba88aa
a921563b3143218981a38441910687a3202a5135e58ce2ecdf9c2d521c6df45b
aab245f99427574c148904e2a60104e97e6b05bfd9be9a086246a797a03dab25
...全文
3028 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lstyk 2008-06-07
  • 打赏
  • 举报
回复
谢楼上了。
昨天发的贴不知道是否是楼主的意思,我是将两个相邻的assic字符转为一个字节。
如:
"06"->0x06 "ba"->0xba
不是
'0'->0x0 '6'->0x6 'b'->0xb 'a' -> 0xa
yshuise 2008-06-07
  • 打赏
  • 举报
回复
读文件将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;
}
Lstyk 2008-06-06
  • 打赏
  • 举报
回复
发上去有些乱了,我不会插入代码…………
Lstyk 2008-06-06
  • 打赏
  • 举报
回复
读文件将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++;
}
}
guolihui112 2008-06-06
  • 打赏
  • 举报
回复
上面错了


//////////////////////////////////////////////////////////////////////////
//
// 从内存数据转化成字符串形式
// 用函数模板和标准库,从基层开始构建,不同于先前版本,从普遍化进行细化
//
//////////////////////////////////////////////////////////////////////////

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 );
};

zjw6861982 2008-06-06
  • 打赏
  • 举报
回复
代码也要加密?
不知道这个有用不
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);
}

guolihui112 2008-06-06
  • 打赏
  • 举报
回复


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();
}
zjw6861982 2008-06-06
  • 打赏
  • 举报
回复
首先这个数据只能用数组存放,至于把字符型的转十六进制数倒是可以。
你可以取8个字符一组用1个int存放。
太乙 2008-06-06
  • 打赏
  • 举报
回复
for循环,一个一个字符判断,最原始的方式~哈哈!
yshuise 2008-06-06
  • 打赏
  • 举报
回复
这是加密的内容,再用算法解密成程序代码,然后执行这代码。
jmulxg 2008-06-06
  • 打赏
  • 举报
回复
这么大的数据用数组存储,另外LZ的转化后的数据什么用途?
yshuise 2008-06-06
  • 打赏
  • 举报
回复
实际上的数据是这个的20,30倍长,不知道怎么搞?
sukyin 2008-06-06
  • 打赏
  • 举报
回复
这么长的数字没法存。只能用数组。
除非你四位四位的处理。才能用int存。
zjw6861982 2008-06-06
  • 打赏
  • 举报
回复
是转换成16进制数么?

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧