如何将 char 转换为 ascii 二进制代码

发布于 2024-11-03 09:22:59 字数 30 浏览 1 评论 0原文

我想要的只是从 'a' 到 0110 0001

All I want is to go from 'a' to 0110 0001

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

念﹏祤嫣 2024-11-10 09:22:59

如果你写 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;
}

if you write int i = 'a'; you get you want since all numbers physically are base 2. But if it's needed to get a string with ones and zeros then here is an example

/* 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;
}
猥琐帝 2024-11-10 09:22:59

我可以写一个自己的:请小心!

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;
}

大端测试:链接

I may write an own one: 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;
}

Big endian testing: link.

生生漫 2024-11-10 09:22:59
#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`
#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`
九局 2024-11-10 09:22:59
    #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);
 }
}
    #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);
 }
}
尘世孤行 2024-11-10 09:22:59

您可以使用以下更灵活的功能:

#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");
}

Here is a more flexible function you can use:

#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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文