如何将ASCII转换为二进制?

时间:2011-03-20 15:33:00

标签: c binary ascii

我有以下字符串:

char * strIn = "f2";

当我看到strIn [0]时,我想得到1111而不是'f'。

我该怎么做?

由于

3 个答案:

答案 0 :(得分:3)

你的意思是十六进制字符串到二进制转换?

基数为16的

strtol应该可以做到这一点

答案 1 :(得分:2)

......有人早些时候说过

  

和二进制f将是11001100

我只能说哇...不,F二进制等于1111(十进制15)

如果我理解你的问题,你想获得任何ascii字符的二进制值...即。

  

当我看到strIn [0]时,我想   得到1111而不是'f'。

所以......这里有一个小功能......

int ConvertHexAsciiValue(char c)
{
    if(isalpha(c))
    {
        char r = tolower(c);
        if(r > 'f')
        {
            // - Handle error here - character is not base 16
            return 0;
        }

        int nIndex = (int)('a' - r);
        nIndex = -nIndex;
        nIndex += 10;
        return nIndex;
    }
    else if(isdigit(c))
    {
        int nIndex = c - '0';
        return nIndex;
    }

    // Handle error here - character is not A-F or 0-9
    return 0;
}

如果我没有正确理解你,你应该知道你不能为字符strIn [0]读取字符串“1111”。但是,您可以使用我提供的函数为每个字符获取二进制值(解释为十六进制值)...

 for(int x = 0; x < strlen(strIn); x++)
 {
     int val = ConvertHexAsciiValue(strIn[x]);
     printf("Value %d: %d\n", x, val); 
 }

如果strIn设置为“f2”,则此代码将在控制台上生成以下输出

Value 0: 15
Value 1: 2

答案 2 :(得分:-1)

  

要获得二进制代码,必须使用有问题的十进制数,取其并重复除以2,保存余数(将成为二进制数),保存整数,除以2,然后重复整个过程直到达到0。

这是我在我的集​​合中的一个小应用程序,它将字符串转换为二进制文件。

/********************************************************/
/*                    Binary converter                  */
/*                     By Matt Fowler                   */
/*                philosopher150@yahoo.com              */
/*  converts text into binary using the division method */
/*                   through ASCII code                 */
/*compiled with the Dev-C++ compiler (www.bloodshed.net)*/
/********************************************************/

#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>

char *entry, letter, choice[2];
int ascii, len, binary[8], total;
void prog();

int main()
{
      prog();     
      return 0;
}        

void prog()
{
   entry = new char[501];

   /* entry should be dynamic, otherwise a new string entry of 501 chars would be created each time function is called! Talk about memory hog! */

   cout<<"Enter string to convert (up to 500 chars): ";

   cin.getline(entry, 500);
   len = strlen(entry);  /* get the number of characters in entry. */

   /* this loop is executed for each letter in the string. */
   for(int i = 0; i<len; i++)
   {
      total = 0;
      letter = entry[i]; /* store the first letter */
      ascii = letter;    /* put that letter into an int, so we can                                 see its ASCII number */

      while(ascii>0) /* This while loop converts the ASCII # into binary,                             stores it backwards into the binary array. */
      {
      /* To get the binary code one must take the decimal number in
        question, take it and divide it by two repeatedly, save
        the remainder (which will become the binary number), save
        the whole number, divide by two, and repeat the whole
        process until 0 is reached.  This if-else statement serves
        this functionality, by getting the remainder of the ascii
        code, storing it in the array and then dividing the int
        ascii by two */

         if((ascii%2)==0)
         {
            binary[total] = 0;
            ascii = ascii/2;
            total++; /* increasing by one each time will yeild the
                        number of numbers in the array. */
         }
         else
         {
            binary[total] = 1;
            ascii = ascii/2;
            total++;
         }
      }
      total--; /* due to data type factors, the program will actually
                  add a 0 at the end of the array that is not supposed
                  to be there, decrementing total will solve this
                  problem, as that 0 will not be displayed. */
      /* this while loop displays the binary code for that letter. */
      while(total>=0)
      {
         cout<<binary[total];
         total--;
      }
   }
   delete[] entry; /* free up the memory used by entry */
   cout<<endl<<"Do again(1 = yes, 2= no)?: ";
   cin.getline(choice,3);
   if(choice[0] == '1')
      prog(); /* program is recursive, it calls itself.  It's kinda
                 like a function loop of sorts. */
   else
      exit(0); /* quits the program */  
}