在C++中使用char*进行从二进制到十进制以及从十进制到二进制的转换

Using char* in C++ for conversion from binary to decimal and decimal to binary

本文关键字:二进制 十进制 转换 C++ char      更新时间:2023-10-16

更新:

char* DecToBin(int n){
char* ptr = new char[];
char* CharReturn = new char[9];
CharReturn[0] = n % 2;
CharReturn[1] = n % 3;
ptr = CharReturn;
return ptr;
}

已经弄清楚了其他的一切,但我无法让这个char*返回任何实际值。如何为char*(指针)分配内存。我如何实现像(x&1),(x>>1)这样的位函数,以及如何使用(x==0),这将类似于for循环中的参数?

原始问题


感谢您阅读我的问题。我的编程类有一个问题,需要使用char*使用switch语句从二进制转换为十进制,从十进制转换为二进制。到目前为止,我的代码非常粗糙,根本没有完成。我在理解char*的使用时遇到问题,该程序必须使用功能

int BinToDec(char* s);
char* DecToBin(int n); 

我是一个初级程序员,这是我的第三个学期,我从来没有用C++编程过。这个作业是给我的CPS 260的,在汇编类编程。我尽力做到最好,我主要想知道我的工作方向是否正确。我知道二进制和十进制的算法都很粗糙。

代码:

#include <iostream>
using namespace std;
int BinToDec(char* s);
char* DecToBin(int n);
int BinToDec(char* BinIn){
int intOut = 0;
intOut = intOut + BinIn[0] * 128;
intOut = intOut + BinIn[1] * 64;
intOut = intOut + BinIn[2] * 32;
intOut = intOut + BinIn[3] * 16;
intOut = intOut + BinIn[4] * 8;
intOut = intOut + BinIn[5] * 4;
intOut = intOut + BinIn[6] * 2;
intOut = intOut + BinIn[7] * 1;
return intOut;
}
char* DecToBin(){
unsigned int intInput, Holder;
char charReturn[7];
char* ptr;
cout << "Please Enter the num number you wish to convert to Binary. n";
cin >> intInput;
Holder = intInput % 2;
charReturn[0] = Holder;
Holder = intInput / 2;
charReturn[1] = Holder % 2;

ptr = charReturn;
return ptr;
}
int main()
{
bool done = false;
unsigned short int intSelect;
while (!done)
{
cout << "Please select a conversion type: n";
cout << "1. Convert from Binary to Decimal n";
cout << "2. Convert from Decimal to Binary n";
cout << "3. Exit the program. n";
cin >> (intSelect);
switch (intSelect)
{
case 1: //How to call BinToDec()
{
char* Input;
cout << "Please enter the 8-bit binary:n";
cin >> (Input);
BinToDec(Input);
break; }
case 2: //How to call DecToBin()
{cout << "case 2n";
cout << DecToBin();
system("pause");
break; }
case 3: //Exit
{cout << "The Program will now exitn";
system("pause");
done = true;
break; }
default: //Check others
{cout << "Invalid Entry try again. nn";
return 0;
}
}
}
cout << "nn";
return 0;
}

任何输入都将是非常有建设性的,即使它说我是一个糟糕的程序员,朝着完全错误的方向前进。即使你没有回复,也要提前谢谢你。

附言:我用Java和Visual Basic编程,没有C++经验,现在还在研究函数的使用。你必须先申报吗?

让我们假设二进制整数的长度不超过8位。

调用BinToDec()

char Input[9];
cout << "Please enter the 8-bit binary:n";
cin >> (Input);
cout << BinToDec(Input);

您需要创建一个至少有8+1个元素的char数组,char数组字符串的最后一个元素总是0。

int BinToDec(char* BinIn){
int intOut = 0;
intOut = intOut + (BinIn[0]-'0') * 128;
//convert '0' or '1' to 0 or 1.
intOut = intOut + (BinIn[1]-'0') * 64;
intOut = intOut + (BinIn[2]-'0') * 32;
intOut = intOut + (BinIn[3]-'0') * 16;
intOut = intOut + (BinIn[4]-'0') * 8;
intOut = intOut + (BinIn[5]-'0') * 4;
intOut = intOut + (BinIn[6]-'0') * 2;
intOut = intOut + (BinIn[7]-'0') * 1;
return intOut;
}

DecToBin()内部

unsigned int intInput, Holder;
//char charReturn[7];
//if you make it 7, you can only convert from 0-63
char* ptr;
char* charReturn = new char[9];
cout << "Please Enter the num number you wish to convert to Binary. n";
cin >> intInput;
Holder = intInput % 2;
charReturn[0] = Holder+'0';
//Holder is 0 or 1, you need to convert it to '0' or '1'
Holder = intInput / 2;
charReturn[1] = Holder % 2+'0';
//...
charReturn[8] = 0; //last character is 0 in char* style string.
ptr = charReturn;
return ptr;

您需要使用newmalloc来创建一个数组,以便在函数返回后将其存储在内存中。