如何使用C ++语言将ascii字符转换为二进制数字。 [英] How do I convert ascii character into binary digits using C++ language.?

查看:354
本文介绍了如何使用C ++语言将ascii字符转换为二进制数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将ascii字符转换为二进制数字的程序。



而且我想知道一个句子或一个单词的每个输入的位和字节



我尝试了什么:



Program that converts ascii characters into binary digit.

And also i want to know the bits and bytes per input of a sentence or a word

What I have tried:

#include <iostream> 
#include <bitset> 
using namespace std;  

int main() {     
   char character;     
   int i;     
   int s[8];          
   cout<<"enter a character: ";     
   cin>>character;       
   for(i=0;i<8;i++) {     
       s[i]=character%2;     
       character=character/2;  
   }     
   int a,b;     
   for(b=0,a=7;b<8;b++,a--) { 
       cout<<s[a]; 
   }  
   return 0; 
}

推荐答案

你所拥有的代码可以做你想要的:'a'变成01100001; 'b'变为01100010,依此类推。

这对你来说可能不合适,但那是因为你现在可能没有现代计算机中人物的价值观。请参阅此处: Ascii表 - ASCII字符代码和html,八进制,十六进制和十进制图表转换 [ ^ ]以ASCII显示值(现在大多数计算机都使用UNICODE略有不同,但不是7位字符你可能在您的编译器中使用。)
The code you have does what you want: 'a' becomes 01100001; 'b' becomes 01100010, and so on.
It may not seem right to you, but that's because you may not now what values characters have in modern computers. See here: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^] which shows the values in ASCII (most computers nowadays use UNICODE which is slightly different, but not for the 7 bit characters you are probably using in your compiler.)


这篇关于如何使用C ++语言将ascii字符转换为二进制数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆