【问题标题】:How to convert char to it's ascii binary code如何将 char 转换为它的 ascii 二进制代码
【发布时间】:2011-08-10 19:54:50
【问题描述】:

我只想从 'a' 到 0110 0001

【问题讨论】:

  • 请更具体。
  • 你的意思是要显示八个字符'0110 0001'而不是'a'?

标签: c++ c binary


【解决方案1】:

如果你写int i = 'a';,你会得到你想要的,因为所有数字实际上都是以 2 为底的。但是如果需要得到一个包含 1 和 0 的字符串,那么这里是一个示例

/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i = 'a';
  char buffer [33]; //the variable you will store i's new value (binary value) in
  _itoa_s(i,buffer,2);
  printf ("binary: %s\n",buffer);
  return 0;
}

【讨论】:

  • 请注意 itoa() 不是标准的。没有标准函数可以获取整数的二进制表示。
【解决方案2】:

我可以自己写一个:Just be careful!

enum
{
  O32_LITTLE_ENDIAN = 0x03020100ul,
  O32_BIG_ENDIAN = 0x00010203ul,
  O32_PDP_ENDIAN = 0x01000302ul
};

static const union { unsigned char bytes[4]; uint32_t value; } o32_host_order =
  { { 0, 1, 2, 3 } };

#define O32_HOST_ORDER (o32_host_order.value)


template <class T> ostream & operator << ( ostream &os, const T &value )
{
  size_t i = sizeof(T);
  const char * val = "01";
  const unsigned char * addr = reinterpret_cast<const unsigned char *>(&value);
// x86
#if ( O32_HOST_ORDER == O32_LITTLE_ENDIAN )
  while ( i > 0 )
  {
    --i;
    os << val[bool(addr[i] & 128)];
    os << val[bool(addr[i] & 64)];
    os << val[bool(addr[i] & 32)];
    os << val[bool(addr[i] & 16)];
    os << val[bool(addr[i] & 8)];
    os << val[bool(addr[i] & 4)];
    os << val[bool(addr[i] & 2)];
    os << val[bool(addr[i] & 1)];
  }
// PowerPC
#else
  size_t j = 0;
  while ( j < i )
  {
    os << val[bool(addr[j] & 128)];
    os << val[bool(addr[j] & 64)];
    os << val[bool(addr[j] & 32)];
    os << val[bool(addr[j] & 16)];
    os << val[bool(addr[j] & 8)];
    os << val[bool(addr[j] & 4)];
    os << val[bool(addr[j] & 2)];
    os << val[bool(addr[j] & 1)];
    ++j;
  }
#endif
  return os;
}

大端测试:link.

【讨论】:

  • os &lt;&lt; "01"[bool(addr[i] &amp; N)]; ;-P
  • 我实际上是在开玩笑...嗯,抱歉...'0' + bool(addr[i] &amp; N) 可能更快,而且也少了一点hacky... :-)。无论如何 +1。
【解决方案3】:
#include <cmath>
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

char*  toBinary(char* doubleDigit)
{
  int digit = atoi(doubleDigit);
  char* binary = new char();

  int x = 0 ;
  for(int i = 9 ; digit != 0; i--)
  {
    //cout << "i"<< i<<endl;
    if(digit-pow(2,i)>=0)
    {
      digit = digit- pow(2,i);
      binary[x]= '2';
      binary[x+1]='^';
      binary[x+2]=i+'0';
      binary[x+3]= '+';
    x+=4;

    }

  }
  return binary;
}

int main()
{
 char value[3]={'8','2','0'};



 cout<< toBinary(value); 



  return 0 ;
}`enter code here`

【讨论】:

    【解决方案4】:
        #include "global.h" 
        #include <iostream> 
        using namespace std; 
        void binaryConvert(int number);
        static int n=0;
        char cipher[256]; // cipher text
        void binaryConvert(int number) {
        while (number > 0) {
        int bin = number % 2;
        number /= 2;
        cipher[n] = bin;
       }
     }
    int main(){
     char c;
     int val=0;
     char buff[9];
     for (int i = 0; i < 8; i++)
     {
       cin >> c;
       val = static_cast<int>(c);
       binaryConvert(val);
     }
    }
    

    【讨论】:

    • 您能否详细说明您的代码示例的功能?
    【解决方案5】:

    你可以使用一个更灵活的函数:

    #include <iostream>
    #include <string>
    #include <bitset>
    
    std::string ASCIITextToBinaryText(const std::string& text, const unsigned int blockSize = 8, const std::string& separator = " ")
    {
        std::string binaryText(""), block;
        std::bitset<8U> asciiCharToBinary;
    
        for (unsigned int i = 0; i < text.length(); i++)
        {
            asciiCharToBinary = text[i];
            block += asciiCharToBinary.to_string();
    
            while (block.length() > blockSize)
            {
                binaryText += block.substr(0, blockSize);
                binaryText += separator;
    
                block = block.substr(blockSize, block.length() - blockSize);
            }
        }
    
        if (block.length() != 0)
        {
            binaryText += block;
        }
    
        return binaryText;
    }
    
    
    int main()
    {
        //your example is a binary text with blocks of 4 bits and " " between blocks, like this:
        std::cout << ASCIITextToBinaryText("a", 4, " ") << std::endl;
    
        //another example can be blocks of 8 bits and as a block separator " "
        std::cout << ASCIITextToBinaryText("example", 8, " ") << std::endl;
    
        //13 bits blocks and "akdn213" block separator
        std::cout << ASCIITextToBinaryText("Hello World!", 13, "akdn213");
    }
    

    【讨论】:

    • 如果您添加了 cmets 或解释了您的代码是如何工作的,这将对发帖者更有帮助。还不清楚发帖者应该如何调用代码。
    • Idk 伙计,对我来说他将如何调用函数似乎很明显,而且代码并不难理解......让我调整一下。
    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 2011-06-21
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2020-06-14
    • 2017-07-11
    相关资源
    最近更新 更多