2011-03-20 103 views
1

我有以下字符串:如何將ASCII轉換爲二進制文件?

char * strIn = "f2"; 

當我看着〜應變[0]我想獲得1111,而不是「F」。

我該怎麼做?

謝謝

+0

這功課嗎? – 2011-03-20 15:34:38

+2

你的意思是你想要將十六進制字符串轉換爲二進制文件? – 2011-03-20 15:34:57

+0

不,只是第一步在c ... – lulu 2011-03-20 15:35:23

回答

3

你的意思是一個十六進制字符串到二進制轉換?

strtol與16的基礎應該做的伎倆

+0

'stroul'可能是更好的選擇,但否則這是正確的方法。 – 2011-03-20 17:22:40

-1

要得到的二進制代碼必須將有問題的十進制數,把它和由兩個一再劃分它,保存餘數(這將成爲二進制數字),保存整個數字,除以二,然後重複整個過程直到達到0。

繼承人一個小的應用程序,我在我的集​​閤中將字符串轉換爲二進制。

/********************************************************/ 
/*     Binary converter     */ 
/*      By Matt Fowler     */ 
/*    [email protected]    */ 
/* 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 */ 
} 
+3

我不認爲這個代碼是非常好的。我看到全局聲明局部變量,主要是inane註釋,至少有一個緩衝區溢出,不需要它的遞歸,一個固定大小的輸入緩衝區,中央的代碼重複以及實現定義的行爲(當char有符號或無符號)。 – 2011-03-20 16:15:21

+0

FFS,您是否閱讀過版權聲明,是否說我已經創建了它,我從我的代碼片段中粘貼了這些內容,以幫助OP瞭解如何進行轉換,我當然不應該得到低票! – RobertPitt 2011-03-20 16:19:03

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

如果我沒有理解錯的話,你應該知道,你不能讀取字符串「1111」對於字符strIn [0]。但是,可以得到每個字符使用我提供的功能(解釋爲十六進制值)的二進制值...

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

如果〜應變分別設置爲「F2」,該代碼將在產生下面的輸出控制檯

Value 0: 15 
Value 1: 2 
相關問題