请问这个功能如何实现。

yishenbiao2 2011-10-29 10:39:34
需要将一个10进制数。转换成16进制的数。并且按照内存中的排列形式排列。比如

1000=0x3e8。最后这个函数的输出结果应该是 E8030000

要输出字符串的形式或者byte数组。
...全文
78 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yishenbiao2 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mingliang1212 的回复:]
C/C++ code


#include <iostream>
#include <algorithm>
//#include "test.h"
int main()
{
int i = 0;
std::cin>>i;
char res[9];
int count = sprintf(res,"%x",i);
std::revers……
[/Quote]



好久不用VC了都忘记了C语言中字符数组的元素是用单引号。我说那天晚上用双引号一直提示错误。这个问题在第二天看了点源码之后恍然大悟。已经自己写出来了。所以好久没来看。鉴于您是第一个给我回帖的。分数全给您。多谢帮助!
qq120848369 2011-10-29
  • 打赏
  • 举报
回复
1,按内存排列
2,16进制是4bits一个

#include <stdio.h>
#include <stdlib.h>

void convert(const void *bin,size_t bytes,char *outHex)
{
const unsigned char *ubin=(const unsigned char*)bin;

for(int i=0;i<bytes;++i)
{
unsigned char unit[2]={ubin[i]&0x0f,(ubin[i]>>4)&0x0f};

for(int j=0;j<2;++j,++outHex)
{
if(unit[j]<10)
{
*outHex=unit[j]+'0';
}
else
{
*outHex=unit[j]-10+'A';
}
}
}

*outHex='\0';
}

int main()
{
char hex[100];
long n;

while(scanf("%ld",&n)==1)
{
convert(&n,sizeof(n),hex);
printf("%s\n",hex);
}

return 0;
}


楼主给的答案本身就有问题,我的程序跑完之后从左往右看对应long的第0..第3字节的16进制表示.
GoonYangXiaofang 2011-10-29
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

void bar(int n)
{
int tmp = n % 16;
if (n >= 16)
{
bar(n / 16);
}
switch (tmp)
{
case 10:
cout << 'A';
break;
case 11:
cout << 'B';
break;
case 12:
cout << 'C';
break;
case 13:
cout << 'D';
break;
case 14:
cout << 'E';
break;
case 15:
cout << 'F';
break;
default:
cout << tmp;
break;
}
}

void foo(int n)
{
cout << "0X";
bar(n);
cout << endl;
}

int main()
{
int n = 0;
while (cin >> n)
{
foo(n);
cout << endl;
}
return 0;
}
lx5180 2011-10-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mingliang1212 的回复:]

C/C++ code


#include <iostream>
#include <algorithm>
//#include "test.h"
int main()
{
int i = 0;
std::cin>>i;
char res[9];
int count = sprintf(res,"%x",i);
std::reverse(res,res ……
[/Quote]
原来有专门的颠倒字符串的函数啊。
学到了,以前都是自己写循环来弄得。
lx5180 2011-10-29
  • 打赏
  • 举报
回复

#include <iostream>

void DecimalToHexadecimal(int n)
{
printf("%d\n",n);
char chHexadecimal[9] = {0};
char chBackUp[9] = {0};
sprintf(chBackUp,"%x",n);
printf("%s\n",chBackUp);
int nIndex = 0;
for (int i = (int)strlen(chBackUp) - 1;i >= 0;i -= 2)
{
if (i == 0)
{
chHexadecimal[nIndex] = '0';
chHexadecimal[nIndex+1] = chBackUp[i];
}
else
{
chHexadecimal[nIndex] = chBackUp[i-1];
chHexadecimal[nIndex+1] = chBackUp[i];
}
nIndex += 2;
}

for (;nIndex < 8;nIndex++)
{
chHexadecimal[nIndex] = '0';
}

printf("%s\n",chHexadecimal);
}

int main()
{
DecimalToHexadecimal(1000);
system("pause");
return 0;
}
iamnobody 2011-10-29
  • 打赏
  • 举报
回复


#include <iostream>
#include <algorithm>
//#include "test.h"
int main()
{
int i = 0;
std::cin>>i;
char res[9];
int count = sprintf(res,"%x",i);
std::reverse(res,res + count);
std::fill(res + count,res + 8,'0');
res[8] = '\0';
puts(res);
system("pause");
return 0;
}




这样实现。。
honbo 2011-10-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

void convert(const void *bin,size_t bytes,char *outHex)
{
const unsigned char *ubin=(const unsigned char*)bin;

for(int i=0;i<bytes;++i)
{
unsigned char unit[2]={ubin[i]&0x0f,(ubin[i]>>4)&0x0f};

for(int j=0;j<2;++j,++outHex)
{
if(unit[j]<10)
{
*outHex=unit[j]+'0';
}
else
{
*outHex=unit[j]-10+'A';
}
}
}

*outHex='\0';
}
void convert1(const void *bin,size_t bytes,char *outHex)
{
const unsigned char *ubin=(const unsigned char*)bin;

for(int i=0;i<bytes;++i)
{
if(*(ubin+i)>15)
sprintf(outHex,"%2X",*(ubin+i));
else
sprintf(outHex,"0%X",*(ubin+i));
++outHex;++outHex;
}

*outHex='\0';
}

int main()
{
char hex[100];
char hex1[100];
long n;

while(scanf("%ld",&n)==1)
{
//convert(&n,sizeof(n),hex);
convert1(&n,sizeof(n),hex1);
//printf("%s\n",hex);
printf("%s\n",hex1);
}

return 0;
}

64,651

社区成员

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

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