我有一個C++代碼,它將2位八進制數轉換爲二進制數。對於代碼的測試有效性我用了幾個在線轉換的網站,如八進制到二進制轉換混淆
當我輸入58或爲一個八進制值,它說無效的八進制值59,但是當我在我的代碼輸入58它給二進制數爲 - 101000.再次測試我輸入101000作爲二進制數在上面的網站的計算器,然後他們給我的結果50爲八進制值。
我需要一些解釋爲什麼這樣。
這裏是C++代碼 -
#include <iostream.h>
#include <conio.h>
void octobin(int);
void main()
{
clrscr();
int a;
cout << "Enter a 2-digit octal number : ";
cin>>a;
octobin(a);
getch();
}
void octobin(int oct)
{
long bnum=0;
int A[6];
//Each octal digit is converted into 3 bits, 2 octal digits = 6 bits.
int a1,a2,quo,rem;
a2=oct/10;
a1=oct-a2*10;
for(int x=0;x<6;x++)
{
A[x]=0;
}
//Storing the remainders of the one's octal digit in the array.
for (x=0;x<3;x++)
{
quo=a1/2;
rem=a1%2;
A[x]=rem;
a1=quo;
}
//Storing the remainders of the ten's octal digit in the array.
for(x=3;x<6;x++)
{
quo=a2/2;
rem=a2%2;
A[x]=rem;
a2=quo;
}
//Obtaining the binary number from the remainders.
for(x=x-1;x>=0;x--)
{
bnum*=10;
bnum+=A[x];
}
cout << "The binary number for the octal number " << oct << " is " << bnum << "." << endl;
}
'void main'不是'main'的有效簽名。使用'int main'。 – chris
'clrscr'不是標準的C或C++函數。爲什麼每次都需要清除屏幕(它會清除以前可能對解決問題有用的數據)? –