在C++中,不同进制之间的转换可以通过标准库函数和手动算法实现。以下是各进制转换的详细方法和示例代码:
一、使用标准库进行输入输出转换
1. 输出不同进制
#include <iostream>
#include <bitset>
int main() {
int num = 255;
// 十进制 → 十六进制
std::cout << "Hex: " << std::hex << num << std::endl; // 输出 ff
// 十进制 → 八进制
std::cout << "Oct: " << std::oct << num << std::endl; // 输出 377
// 十进制 → 二进制 (需使用 bitset)
std::cout << "Bin: " << std::bitset<8>(num) << std::endl; // 输出 11111111
return 0;
}
2. 输入不同进制
#include <iostream>
int main() {
int num;
// 输入十六进制
std::cout << "输入十六进制数: ";
std::cin >> std::hex >> num; // 输入 ff → 输出 255
std::cout << "Dec: " << num << std::endl;
// 输入八进制
std::cout << "输入八进制数: ";
std::cin >> std::oct >> num; // 输入 377 → 输出 255
std::cout << "Dec: " << num << std::endl;
return 0;
}
二、字符串与数值的转换
1. 字符串转十进制
#include <string>
int main() {
// 二进制字符串 → 十进制
std::string bin_str = "11111111";
int dec1 = std::stoi(bin_str, nullptr, 2); // 255
// 八进制字符串 → 十进制
std::string oct_str = "377";
int dec2 = std::stoi(oct_str, nullptr, 8); // 255
// 十六进制字符串 → 十进制
std::string hex_str = "ff";
int dec3 = std::stoi(hex_str, nullptr, 16); // 255
return 0;
}
2. 十进制转字符串
#include <string>
#include <sstream>
std::string decToBase(int num, int base) {
std::stringstream ss;
ss << std::nouppercase;
if (base == 16) ss << std::hex << num;
else if (base == 8) ss << std::oct << num;
else if (base == 2) ss << std::bitset<32>(num).to_string(); // 固定32位
else ss << num; // 十进制
return ss.str();
}
// 示例
std::string hex_str = decToBase(255, 16); // "ff"
三、手动转换算法
1. 十进制转任意进制字符串
#include <algorithm>
std::string decimalToBase(int num, int base) {
if (num == 0) return "0";
const char* digits = "0123456789abcdef";
std::string result;
bool negative = num < 0;
num = std::abs(num);
while (num > 0) {
result.push_back(digits[num % base]);
num /= base;
}
if (negative) result.push_back('-');
std::reverse(result.begin(), result.end());
return result;
}
// 示例
std::string bin_str = decimalToBase(255, 2); // "11111111"
2. 其他进制转十进制
#include <string>
int baseToDecimal(const std::string& num_str, int base) {
int result = 0;
for (char c : num_str) {
int digit = (c >= '0' && c <= '9') ? c - '0' : std::tolower(c) - 'a' + 10;
result = result * base + digit;
}
return result;
}
// 示例
int dec = baseToDecimal("ff", 16); // 255
四、进制间直接转换
1. 二进制 ↔ 八进制/十六进制
// 二进制 → 八进制(3位一组)
std::string binary = "11111111";
binary = std::string(3 - binary.length() % 3, '0') + binary; // 补前导0
std::string octal;
for (size_t i = 0; i < binary.length(); i += 3) {
std::string group = binary.substr(i, 3);
octal += std::to_string(std::stoi(group, nullptr, 2));
}
// 二进制 → 十六进制(4位一组)
std::string binary = "11111111";
binary = std::string(4 - binary.length() % 4, '0') + binary; // 补前导0
std::string hex;
for (size_t i = 0; i < binary.length(); i += 4) {
std::string group = binary.substr(i, 4);
hex += "0123456789abcdef"[std::stoi(group, nullptr, 2)];
}
2. 任意进制互转(通过十进制中转)
std::string convertBase(const std::string& num, int from, int to) {
int dec = baseToDecimal(num, from);
return decimalToBase(dec, to);
}
// 示例:二进制 "11111111" → 十六进制 "ff"
std::string hex = convertBase("11111111", 2, 16);
五、总结表格
转换方向 | 推荐方法 |
---|---|
十进制 ↔ 其他 | std::oct/hex , std::bitset |
字符串 → 十进制 | std::stoi 或 baseToDecimal |
十进制 → 字符串 | std::stringstream 或手动算法 |
非十进制互转 | 通过十进制中转或按位分组转换 |
注意事项:
- 使用
std::stoi
时需确保字符串合法,否则会抛出异常。 bitset
的位数需足够大,避免溢出。- 手动算法支持负数,但需额外处理符号位。