回答
要得到的二進制代碼必須將有問題的十進制數,把它和由兩個一再劃分它,保存餘數(這將成爲二進制數字),保存整個數字,除以二,然後重複整個過程直到達到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 */
}
我不認爲這個代碼是非常好的。我看到全局聲明局部變量,主要是inane註釋,至少有一個緩衝區溢出,不需要它的遞歸,一個固定大小的輸入緩衝區,中央的代碼重複以及實現定義的行爲(當char有符號或無符號)。 – 2011-03-20 16:15:21
FFS,您是否閱讀過版權聲明,是否說我已經創建了它,我從我的代碼片段中粘貼了這些內容,以幫助OP瞭解如何進行轉換,我當然不應該得到低票! – RobertPitt 2011-03-20 16:19:03
...有人說前面
和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
- 1. 如何將二進制文件轉換爲ASCII
- 2. 如何將ascii字符轉換爲二進制文件emu8086
- 3. 將Ascii代碼文件轉換爲二進制文件
- 4. 如何將ASCII JSON 3D模型文件轉換爲二進制文件?
- 5. ASCII到二進制轉換?
- 6. 轉換ASCII /二進制文件的彙編語言轉換爲十進制
- 7. 將ascii文件讀取爲二進制
- 8. 將二進制文件解釋爲ASCII
- 9. 如何在JAVA中將二進制值轉換爲ASCII?
- 10. 如何將這些ASCII值轉換爲二進制C
- 11. 將二進制IOstream轉換爲文件
- 12. 將二進制(或十六進制)轉換爲ASCII
- 13. 如何通過java代碼將ASCII轉換爲大型機二進制文件?
- 14. 如何將二進制文件轉換爲源代碼管理的ascii?
- 15. 如何將十六進制文件數據轉換爲ASCII?
- 16. 將二進制字符串轉換爲ASCII文本?
- 17. 如何將二進制文件轉換爲numpy文件?
- 18. 轉換二進制文件轉換成ASCII在Python
- 19. Python - 將原始二進制轉儲轉換爲ASCII HEX字節
- 20. 將ASCII文件轉換爲十進制文件
- 21. 如何將ZZ轉換爲二進制?
- 22. 如何將二進制轉換爲qpixmap
- 23. 如何將UCHAR轉換爲二進制
- 24. 如何將十六進制文件轉換爲二進制文件?
- 25. 如何將大十六進制文件轉換爲二進制文件?
- 26. Pharo:如何將ASCII字符轉換爲ASCII十進制
- 27. 將一串文本轉換爲替代表示(二進制,十六進制,ASCII)
- 28. 如何將二進制值轉換爲python 3中的文本或ascii?
- 29. 二進制如何轉換爲硬件?
- 30. ASCII轉換成二進制和寫入二進制文件 - Python 3.x都有
這功課嗎? – 2011-03-20 15:34:38
你的意思是你想要將十六進制字符串轉換爲二進制文件? – 2011-03-20 15:34:57
不,只是第一步在c ... – lulu 2011-03-20 15:35:23