二进制字符串转换为char?

yangyunzhao 2009-05-14 01:10:55
比如我的字符串为"01000001",对应的ASCII字符为a
但是如何将这个string转换为char?
...全文
744 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2009-05-14
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>
char *binstr="01000001";
char c;
void main() {
c=(char)strtol(binstr,0,2);
printf("%c\n",c);
}
输出
A
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
sorry,是我错了,printf()居然还可以这样?
出乎我的意料:
printf("%c",1000001); //输出:A

长见识了
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
楼上的,你看看你打印出来的是什么?ia=1000001,赋给char,会发生截断的!
lingyin55 2009-05-14
  • 打赏
  • 举报
回复
楼主没说转成int啊,直接转成char。运行下看看。

#include<stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

void main()
{
string a = "01000001";
int ia;
ia = atoi( a.c_str() );
char aa = ia;
printf( "%d\n",aa );
printf( "%c\n",aa );
}


[Quote=引用 12 楼 crst_zh 的回复:]
引用 11 楼 lingyin55 的回复:
如果是string类型,也一样

C/C++ code
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

void main()
{
string a = "01000001";
int ia = atoi( a.c_str() );//结果ia=1000001,错误
printf( "%c\n",ia );
}
[/Quote]
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lingyin55 的回复:]
如果是string类型,也一样

C/C++ code
#include<stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

void main()
{
string a = "01000001";
int ia = atoi( a.c_str() );//结果ia=1000001,错误
printf( "%c\n",ia );
}
[/Quote]
lingyin55 2009-05-14
  • 打赏
  • 举报
回复
如果是string类型,也一样

#include<stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

void main()
{
string a = "01000001";
int ia = atoi( a.c_str() );
printf( "%c\n",ia );
}


crst_zh 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lingyin55 的回复:]
很简单啊,把字符串先转为整型,得到的就是65,用字符格式输出就是A了。


C/C++ code
#include<stdio.h>
#include <stdlib.h>
void main()
{
char *a = "01000001";
int ia = atoi( a );
printf( "%c",ia );
}
[/Quote]

这样不行的,ia=1000001了:(
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
#include <iostream>
#include <bitset>
using namespace std;

int main(int argc, char* argv[])
{
bitset<32> bs("01000001");
cout<<bs.to_ulong()<<endl;
return 0;
}


结果:
65
lingyin55 2009-05-14
  • 打赏
  • 举报
回复
很简单啊,把字符串先转为整型,得到的就是65,用字符格式输出就是A了。


#include<stdio.h>
#include <stdlib.h>
void main()
{
char *a = "01000001";
int ia = atoi( a );
printf( "%c",ia );
}

crst_zh 2009-05-14
  • 打赏
  • 举报
回复
前面看错了,sorry,没看清是二进制转化
garymao8888 2009-05-14
  • 打赏
  • 举报
回复
先用atoi转换为整型再用_itoa转换为字符型~~以下是转换为字符串,
_itoa(source,dest,10);
我想取第一应该可行,说得不对请大家多多帮助。
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
bitset<32> bs("01000001");
cout<<bs.to_ulong()<<endl;
xxf012005 2009-05-14
  • 打赏
  • 举报
回复
建议用sprintf代替atoi转换
mengde007 2009-05-14
  • 打赏
  • 举报
回复
根据你的意思只能先转化为char数组,然后计算了;最终根据值和65比较;
crst_zh 2009-05-14
  • 打赏
  • 举报
回复
转换成char,你已经得到了asii码,不就是char吗?

那就atoi,先转化成int,再转化成char
datacode 2009-05-14
  • 打赏
  • 举报
回复
       #include <stdlib.h>

long int strtol(const char *nptr, char **endptr, int base);



65,211

社区成员

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

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