C++二进制转换为十进制,八进制
C++ convert binary to decimal, octal
请我需要帮助调试下面的代码。我假设使用将二进制数转换为十进制或八进制的函数生成代码。我在 switch 语句"函数调用中的参数错误太少"中不断收到错误。
#include <iostream.>
long int menu();
long int toDeci(long int);
long int toOct(long int);
using namespace std;
int main ()
{
int convert=menu();
switch (convert)
{
case(0):
toDeci();
break;
case(1):
toOct();
break;
}
return 0;
}
long int menu()
{
int convert;
cout<<"Enter your choice of conversion: "<<endl;
cout<<"0-Binary to Decimal"<<endl;
cout<<"1-Binary to Octal"<<endl;
cin>>convert;
return convert;
}
long int toDeci(long int)
{
long bin, dec=0, rem, num, base =1;
cout<<"Enter the binary number (0s and 1s): ";
cin>> num;
bin = num;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout<<"The decimal equivalent of "<< bin<<" = "<<dec<<endl;
return dec;
}
long int toOct(long int)
{
long int binnum, rem, quot;
int octnum[100], i=1, j;
cout<<"Enter the binary number: ";
cin>>binnum;
while(quot!=0)
{
octnum[i++]=quot%8;
quot=quot/8;
}
cout<<"Equivalent octal value of "<<binnum<<" :"<<endl;
for(j=i-1; j>0; j--)
{
cout<<octnum[j];
}
}
我假设使用将二进制数转换为十进制或八进制的函数生成代码。
没有像根据数字表示将二进制数转换为十进制或八进制这样的事情,例如
long int toDeci(long int);
long int toOct(long int);
对于任何语义解释来说,这样的功能都是完全荒谬的。
数字是数字,其文本表示形式可以是十进制、十六进制、八进制或二进制格式:
dec 42
hex 0x2A
oct 052
bin 101010
在long int
数据类型中仍然是相同的数字。
使用 c++ 标准 I/O 操纵器,可以从这些格式的文本表示形式进行转换。
我不确定我是否理解你想做什么。下面是一个可能对您有所帮助的示例(演示):
#include <iostream>
int main()
{
using namespace std;
// 64 bits, at most, plus null terminator
const int max_size = 64 + 1;
char b[max_size];
//
cin.getline( b, max_size );
// radix 2 string to int64_t
uint64_t i = 0;
for ( const char* p = b; *p && *p == '0' || *p == '1'; ++p )
{
i <<= 1;
i += *p - '0';
}
// display
cout << "decimal: " << i << endl;
cout << hex << "hexa: " << i << endl;
cout << oct << "octa: " << i << endl;
return 0;
}
相关文章:
- 用c++把小数点转换成八进制
- 如何使用 SML 随机生成八进制元组
- 编译器始终打印十六进制/八进制数字
- 在 C++ 中使用带有十六进制十进制和八进制数的 unsetf
- 十进制到八进制递归C++.以标准::字符串格式输出
- 在C++中,将无符号整数转换为八进制表示,反之亦然的最佳方法是什么
- 如何使系统在十六进制数之前打印 0x,在八进制数之前打印 0?
- 如何在C++中使用setiosflags打印出十进制数作为八进制数
- 用于"Ctrl + left arrow"和"Ctrl + right arrow"的八进制代码
- 八进制文字怎么可能是负数?
- 图形面积(等分三角形、正方形、五角形、六边形、七重奏、八进制)使用 if-else (C++)
- 将八进制分数转换为十进制 - c++
- 我如何使用动态内存分配将小数转换为八进制
- 将八进制字符串转换为C 中的小数线
- 八进制到十进制的转换(调试)
- C++二进制转换为十进制,八进制
- 以十进制、八进制和十六进制表示法标识整数文本是否与转义字符中的整数文本相同
- 如何使用Clang知道IntegerLiteral是来自十进制还是八进制表示
- 将八进制分数转换为十进制分数
- 验证八进制到十进制的转换