二进制八进制十六进制转十进制 与相反的C#实现
返回值带有开头,0b,0o,0x,返回值是string类型
static void Main(string[] args){//0b或者0B//0O 或者0//直接写//0x或者0XConvertDecimalToBinary(7).ForEach(x => { Console.Write(x); });Console.WriteLine();ConvertDecimalToOctal(10).ForEach(x => { Console.Write(x); });Console.WriteLine();ConvertDecimalToHexadecimal(10).ForEach(x => { Console.Write(x); });Console.WriteLine();// 二进制转十进制string binaryString = "0B111110101";int binaryToDecimal = BinaryToDecimal(binaryString);Console.WriteLine($"Binary {binaryString} to Decimal: {binaryToDecimal}");// 八进制转十进制string octalString = "0o15231413416";int octalToDecimal = OctalToDecimal(octalString);Console.WriteLine($"Octal {octalString} to Decimal: {octalToDecimal}");// 十六进制转十进制string hexString = "0x1AAD123";int hexToDecimal = HexToDecimal(hexString);Console.WriteLine($"Hexadecimal {hexString} to Decimal: {hexToDecimal}");Console.ReadKey();Console.ReadKey();Console.ReadKey();}#region 十进制转成二进制,八进制,十六进制,使用取余法/// <summary>/// 十进制转二进制/// </summary>/// <param name="number">十进制数字</param>/// <returns>二进制List,以0b开头</returns>private static List<string> ConvertDecimalToBinary(int number){List<string> list = new List<string>();while (number > 0){int remainder = number % 2;list.Add(remainder.ToString());number = number / 2;}list.Add("0b");list.Reverse();return list;}/// <summary>/// 十进制转八进制/// </summary>/// <param name="number">八进制数字</param>/// <returns>八进制List,以0o开头</returns>private static List<string> ConvertDecimalToOctal(int number){List<string> list = new List<string>();while (number > 0){int remainder = number % 8;list.Add(remainder.ToString());number = number / 8;}list.Add("0o");list.Reverse();return list;}/// <summary>/// 十进制转十六进制进制/// </summary>/// <param name="number">十进制数字</param>/// <returns>十六进制List,以0x开头</returns>private static List<string> ConvertDecimalToHexadecimal(int number){List<string> list = new List<string>();while (number > 0){int remainder = number % 16;if (remainder == 10){list.Add("A");}else if (remainder == 11){list.Add("B");}else if (remainder == 12){list.Add("C");}else if (remainder == 13){list.Add("D");}else if (remainder == 14){list.Add("E");}else if (remainder == 15){list.Add("F");}else{list.Add(remainder.ToString());}number = number / 16;}list.Add("0x");list.Reverse();return list;}#endregion#region 二进制八进制十六进制 转成二进制//public static double Pow(double base, double exponent); //base:基数,即要进行幂运算的数。 exponent:指数,即幂运算的次数。//Math.Pow(2, 10) = 1024/// <summary>/// 二进制转成十进制/// </summary>/// <param name="binary">0B二进制数</param>/// <returns>十进制数</returns>static int BinaryToDecimal(string binary){if (binary.StartsWith("0b") || binary.StartsWith("0B")){binary = binary.Substring(2);}int decimalValue = 0;int power = 0;for (int i = binary.Length - 1; i >= 0; i--){if (binary[i] == '1'){decimalValue += (int)Math.Pow(2, power);}power++;}return decimalValue;}/// <summary>/// 八进制转成十进制/// </summary>/// <param name="octal">0O八进制数字</param>/// <returns>十进制数字</returns>static int OctalToDecimal(string octal){if (octal.StartsWith("0o") || octal.StartsWith("0O")){octal = octal.Substring(2);}int decimalValue = 0;int power = 0;for (int i = octal.Length - 1; i >= 0; i--){int digit = octal[i] - '0';decimalValue += digit * (int)Math.Pow(8, power);power++;}return decimalValue;}/// <summary>/// 十六进制转成十进制/// </summary>/// <param name="hex">0x十六进制数字</param>/// <returns>十进制数</returns>/// <exception cref="ArgumentException">字符串中包含的字符不在0-9或者A-F中</exception>static int HexToDecimal(string hex){if (hex.StartsWith("0x") || hex.StartsWith("0X")){hex = hex.Substring(2);}int decimalValue = 0;int power = 0;for (int i = hex.Length - 1; i >= 0; i--){int digit;if (hex[i] >= '0' && hex[i] <= '9'){digit = hex[i] - '0';}else if ((hex[i] >= 'A' && hex[i] <= 'F') || (hex[i] >= 'a' && hex[i] <= 'f')){digit = hex[i] >= 'a' ? hex[i] - 'a' + 10 : hex[i] - 'A' + 10;}else{throw new ArgumentException("Invalid hexadecimal character");}decimalValue += digit * (int)Math.Pow(16, power);power++;}return decimalValue;}#endregion
二进制八进制十六进制转十进制 与相反的C++实现
c++版本是c++11
auto
推断类型,类似与c#中的var,
std::vector<std::string>
是 C++ 标准库中的一个容器类型,用于存储一组 std::string 类型的元素,动态数组的一种实现。在c#表现为List。
头文件,转化文件,main.cpp
头文件NumberConversion.h
#ifndef NUMBERCONVERSION_H
#define NUMBERCONVERSION_H#include <vector>
#include <string>// 十进制转二进制
std::vector<std::string> ConvertDecimalToBinary(int number);// 十进制转八进制
std::vector<std::string> ConvertDecimalToOctal(int number);// 十进制转十六进制
std::vector<std::string> ConvertDecimalToHexadecimal(int number);// 二进制转十进制
int BinaryToDecimal(const std::string& binary);// 八进制转十进制
int OctalToDecimal(const std::string& octal);// 十六进制转十进制
int HexToDecimal(const std::string& hex);#endif // NUMBERCONVERSION_H
转换文件,NumberConversion.cpp
#include "NumberConversion.h"
#include <algorithm>
#include <cmath>// 十进制转二进制
std::vector<std::string> ConvertDecimalToBinary(int number) {std::vector<std::string> list;while (number > 0) {int remainder = number % 2;list.push_back(std::to_string(remainder));number = number / 2;}list.push_back("0b");std::reverse(list.begin(), list.end());return list;
}// 十进制转八进制
std::vector<std::string> ConvertDecimalToOctal(int number) {std::vector<std::string> list;while (number > 0) {int remainder = number % 8;list.push_back(std::to_string(remainder));number = number / 8;}list.push_back("0o");std::reverse(list.begin(), list.end());return list;
}// 十进制转十六进制
std::vector<std::string> ConvertDecimalToHexadecimal(int number) {std::vector<std::string> list;while (number > 0) {int remainder = number % 16;if (remainder == 10) {list.push_back("A");} else if (remainder == 11) {list.push_back("B");} else if (remainder == 12) {list.push_back("C");} else if (remainder == 13) {list.push_back("D");} else if (remainder == 14) {list.push_back("E");} else if (remainder == 15) {list.push_back("F");} else {list.push_back(std::to_string(remainder));}number = number / 16;}list.push_back("0x");std::reverse(list.begin(), list.end());return list;
}// 二进制转十进制
int BinaryToDecimal(const std::string& binary) {std::string bin = binary;if (bin.substr(0, 2) == "0b" || bin.substr(0, 2) == "0B") {bin = bin.substr(2);}int decimalValue = 0;int power = 0;for (int i = bin.length() - 1; i >= 0; i--) {if (bin[i] == '1') {decimalValue += static_cast<int>(std::pow(2, power));}power++;}return decimalValue;
}// 八进制转十进制
int OctalToDecimal(const std::string& octal) {std::string oct = octal;if (oct.substr(0, 2) == "0o" || oct.substr(0, 2) == "0O") {oct = oct.substr(2);}int decimalValue = 0;int power = 0;for (int i = oct.length() - 1; i >= 0; i--) {int digit = oct[i] - '0';decimalValue += digit * static_cast<int>(std::pow(8, power));power++;}return decimalValue;
}// 十六进制转十进制
int HexToDecimal(const std::string& hex) {std::string hexa = hex;if (hexa.substr(0, 2) == "0x" || hexa.substr(0, 2) == "0X") {hexa = hexa.substr(2);}int decimalValue = 0;int power = 0;for (int i = hexa.length() - 1; i >= 0; i--) {int digit;if (hexa[i] >= '0' && hexa[i] <= '9') {digit = hexa[i] - '0';} else if ((hexa[i] >= 'A' && hexa[i] <= 'F') || (hexa[i] >= 'a' && hexa[i] <= 'f')) {digit = hexa[i] >= 'a' ? hexa[i] - 'a' + 10 : hexa[i] - 'A' + 10;} else {throw std::invalid_argument("Invalid hexadecimal character");}decimalValue += digit * static_cast<int>(std::pow(16, power));power++;}return decimalValue;
}
main.cpp
#include <iostream>
#include "NumberConversion.h"int main() {// 十进制转二进制auto binaryList = ConvertDecimalToBinary(7);for (const auto& x : binaryList) {std::cout << x;}std::cout << std::endl;// 十进制转八进制auto octalList = ConvertDecimalToOctal(10);for (const auto& x : octalList) {std::cout << x;}std::cout << std::endl;// 十进制转十六进制auto hexList = ConvertDecimalToHexadecimal(10);for (const auto& x : hexList) {std::cout << x;}std::cout << std::endl;// 二进制转十进制std::string binaryString = "0B111110101";int binaryToDecimal = BinaryToDecimal(binaryString);std::cout << "Binary " << binaryString << " to Decimal: " << binaryToDecimal << std::endl;// 八进制转十进制std::string octalString = "0o15231413416";int octalToDecimal = OctalToDecimal(octalString);std::cout << "Octal " << octalString << " to Decimal: " << octalToDecimal << std::endl;// 十六进制转十进制std::string hexString = "0x1AAD123";int hexToDecimal = HexToDecimal(hexString);std::cout << "Hexadecimal " << hexString << " to Decimal: " << hexToDecimal << std::endl;return 0;
}