以下是将十六进制数字转换为八进制数字的 C++ 代码,可以帮助你破解古老宝箱的密码:

#include <iostream>
#include <string>
#include <algorithm>

std::string hexToOct(std::string hexNum) {
    std::string octNum = "";
    std::string octDigit[8] = {"000", "001", "010", "011", "100", "101", "110", "111"};

    // Remove the dot if present
    hexNum.erase(std::remove(hexNum.begin(), hexNum.end(), '.'), hexNum.end());

    // Convert each hexadecimal digit to its corresponding octal digit
    for (int i = 0; i < hexNum.length(); i++) {
        char hexDigit = std::toupper(hexNum[i]);
        if (hexDigit >= '0' && hexDigit <= '9') {
            octNum += octDigit[hexDigit - '0'];
        } else if (hexDigit >= 'A' && hexDigit <= 'F') {
            octNum += octDigit[hexDigit - 'A' + 10];
        }
    }

    // Remove leading zeros from the octal number
    octNum.erase(0, octNum.find_first_not_of('0'));

    return octNum;
}

int main() {
    std::string hexNum;
    std::cin >> hexNum;

    std::string octNum = hexToOct(hexNum);
    std::cout << octNum << std::endl;

    return 0;
}

这段代码接受一个十六进制数作为输入,并将其转换为对应的八进制数。首先,我们定义一个字符串数组octDigit,其中存储了八进制数的每个数字的二进制表示。然后,我们去除输入字符串中的小数点,并使用一个循环将每个十六进制数字转换为对应的八进制数字。最后,我们去除八进制数中的前导零,并输出结果。

请注意,这段代码假设输入的十六进制数是有效的,即只包含数、点(.)和大写字母(A-F)。

C++ 十六进制转八进制:古老宝箱密码破解

原文地址: https://www.cveoy.top/t/topic/nP22 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录