128位二进制如何转化为十六进制?

sjhy19890401 2012-10-17 05:07:07
例如:CString str= "01011111110001111111111111100000000000000000000111";
它对应的十六进制CString strOut怎么求得?
...全文
887 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyrain 2012-10-17
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <math.h>

using namespace std;
char valueToHex(int value)
{
if (value < 10)
{
return (char)(value+'0');
}
else
{
return (char)(value-10+'a');
}
}

string binToHex(string binStr)
{
string hexStr;
int len = binStr.length();
int mod = len%4;
binStr.insert(0, mod, '0');

string str4bit;
int value = 0;
for (int i=0; i<len+mod; i=i+4)
{
str4bit = binStr.substr(i, 4);
value = 0;
for (int j=0; j<4; j++)
{
switch (str4bit.at(j))
{
case '0':
break;
case '1':
value += (int)pow(2, 3-j);
break;
default:
cout << "string input is not a binary string!";
return "";
}
}
hexStr.append(1, valueToHex(value));
}

return hexStr;
}

int main()
{
string str= "01011111110001111111111111100000000000000000000111";
string strOut = binToHex(str);
cout << "binary:" << str << endl;
cout << "hex:" << strOut << endl;

return 0;
}
mymtom 2012-10-17
  • 打赏
  • 举报
回复
8位一组转成int型, 然后在sprintf就可以了。
当然也可以16位或32位一组的。
[code=C/C++]
/**
* @file b2h.c
* @brief
*/

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

char *b2h(const char *b, char *h)
{
int i, m, n, len;
char s[8 + 1];

s[8] = '\0';
len = strlen(b);
n = len / 8;
m = len % 8;
len = 0;

if (m > 0) {
memset(s, '0', 8);
memcpy(s + (8 - m), b, m);
len += sprintf(h + len, "%02x", (unsigned)strtoul(s, NULL, 2));
}
for (i = 0; i < n; i++) {
memcpy(s, b + m + i * 8, 8);
len += sprintf(h + len, "%02x", (unsigned)strtoul(s, NULL, 2));
}
return h;
}

char *b2h_32(const char *b, char *h)
{
int i, m, n, len;
char s[32 + 1];

s[32] = '\0';
len = strlen(b);
n = len / 32;
m = len % 32;
len = 0;

if (m > 0) {
memset(s, '0', 32);
memcpy(s + (32 - m), b, m);
len += sprintf(h + len, "%08x", (unsigned)strtoul(s, NULL, 2));
}
for (i = 0; i < n; i++) {
memcpy(s, b + m + i * 32, 32);
len += sprintf(h + len, "%08x", (unsigned)strtoul(s, NULL, 2));
}
return h;
}

int main(int argc, char *argv[])
{
char b[] = "01011111110001111111111111100000000000000000000111";
char h[1024];

printf("%s\n", b2h(b, h));
printf("%s\n", b2h_32(b, h));
return 0;
}
[/Code]
mymtom 2012-10-17
  • 打赏
  • 举报
回复
[code=C/C++]
/**
* @file b2h.c
* @brief
*/

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

char *b2h(const char *b, char *h)
{
int i, m, n, len;
char s[8 + 1];

s[8] = '\0';
len = strlen(b);
n = len / 8;
m = len % 8;
len = 0;

if (m > 0) {
memset(s, '0', 8);
memcpy(s + (8 - m), b, m);
len += sprintf(h + len, "%02x", (unsigned)strtol(s, NULL, 2));
}
for (i = 0; i < n; i++) {
memcpy(s, b + m + i * 8, 8);
len += sprintf(h + len, "%02x", (unsigned)strtol(s, NULL, 2));
}
return h;
}

int main(int argc, char *argv[])
{
char b[] = "01011111110001111111111111100000000000000000000111";
char h[1024];

printf("%s\n", b2h(b, h));
return 0;
}
[/Code]
dpdp_2012 2012-10-17
  • 打赏
  • 举报
回复
比较丑陋的C代码
#include <stdio.h>
#include <string.h>

int main()
{
char *s="01011111110001111111111111100000000000000000000111";
char out[1000];
int i=strlen(s)-1,j=0,temp=0,len=0,mi=1;
while(i>=0)
{
temp+=(s[i]-'0')*mi;
if(j==3)
{
if(temp<10)
out[len++]=temp+'0';
else
out[len++]=temp-10+'A';
temp=0;
}
mi=mi*2;
if(mi>8)
mi=1;
j=(j+1)%4;
i--;
}
if(j!=0)
out[len++]=temp+'0';
out[len]=0;
for(i=0;i<len/2;i++)
{
char c=out[i];
out[i]=out[len-i-1];
out[len-i-1]=c;
}
printf("%s\n",out);
}
sjhy19890401 2012-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

2进制到16进制
因为4个2进制位恰好是一个16进制位,所以依次从最末端开始,4位转化成1位16进制
[/Quote]

能给个代码不?
十八道胡同 2012-10-17
  • 打赏
  • 举报
回复
2进制到16进制
因为4个2进制位恰好是一个16进制位,所以依次从最末端开始,4位转化成1位16进制

64,636

社区成员

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

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