c++ - 如何将表示为字符串的整数转换为 ASCII 符号?

标签 c++ string char integer converters

我有一个输入字符串,看起来像这样:“126022034056098012”。这是我从某个文件中读取的符号的 ASCII 代码串联的结果。例如,代码为 126 22 34 56 98 12。问题是如何将这个字符串解码回字符?注意:字符串不得包含除数字(\、| 等)以外的任何分隔符。接下来我该做什么?

我想出了一种使用 ASCII 符号映射的方法:key->string with numeric representain of ASCII symbol, value->ASCII symbol。在循环中,我将传入的数字累积到一个字符串中,直到该字符串与映射中的某个键匹配。匹配时,我将生成的代码转换为字符。我继续,直到用完输入数据。但此方法适用于字符串和 txt 文件,但不适用于二进制文件。

从ASCII码字符串生成字符串的函数:

string Utils::from_number_to_ascii(string number, int size) {
    Utils ut;
    while(number.size() % 3) {
        number = "0" + number;
    }

    string out;
    for (int i = 0; i < size;){
        string st;
        auto it = ut.triple_dict.end();
        while (it == ut.triple_dict.end() && i < size){
            st += number[i++];
            it = ut.triple_dict.find(st);
        }
        out += it->second;
        st = "";
    }
    return out;
}

填充 map :

Utils::Utils() {
    for (int i = 0; i <= 255; i++){
        string s =  to_string(static_cast<int>(i));
        if (s.size() == 1) {
            s = "00" + s;
        } if (s.size() == 2){
            s = "0" + s;
        }
        triple_dict.insert(make_pair(s, static_cast<unsigned char>(i)));
    }
}

不难看出我用三个字节填充容器:如果symbol的ASCII码是两位数我追加“0”,如果symbol的代码是一位数我追加“00”使代码成为三位数。我这样做是为了明确解码符号。

最佳答案

如果每个 ascii 码正好由 3 位数字表示,我们可以很容易地用一个循环来完成:

std::string toAscii(char const* digits, size_t size) {
    std::string output(size / 3, '\0');
    for(char& c : output) {
        char d0 = *digits++; // Get 3 digits
        char d1 = *digits++;
        char d2 = *digits++; 

        int ascii_value = (d0 - '0') * 100 + (d1 - '0') * 10 + (d2 - '0'); 
        c = (char)ascii_value; 
    }
    return output; 
}

示例用法

我有一个带有示例输入的 C 字符串,以及一个带有预期输出的字符串。该程序验证它们是否相等。

int main() {
    auto&& input = "126022034056098012";
    std::string expected_output = {char(126), char(22), char(34), char(56), char(98), char(12)};
    std::cout << (toAscii(input, sizeof(input)) == expected_output); // Prints true
}

fstream.write() 是否在文件末尾添加'\0'?

否。如果您的字符串包含 0 字符,它会添加它,但不会添加。我们可以使用一些非常短的示例代码来自行测试。

#include <fstream>
#include <iostream>
#include <string>

int main()
{
    {
        std::ofstream file("test.txt");
        std::string   message = "Hello!";

        file.write(message.data(), message.length());
        // file gets closed automatically 
    }
    {
        std::ifstream file("test.txt");


        while (file)
        {
            std::cout << file.get() << '\n';
        }
        // file gets closed automatically
    }
}

当我编译并运行这段代码时,它会输出以下内容。除了最后一个,每个值都对应"Hello!"中相应字符的值。 -1 表示您已到达文件末尾,但如果您使用的是 file.read 之类的方法,它就不会出现。 \0 没有出现在文件中的任何位置。

72
101
0
108
111
33
-1

关于c++ - 如何将表示为字符串的整数转换为 ASCII 符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100480/

相关文章:

c++ - 了解取消引用的类型 - const_iterator

c++ - "error: invalid operands to binary expression"来自字符串库组件内部

python - 如何使用 __str__ 更改对象属性的显示方式?

c - 尝试将一组字符分配给字符串

c++ - 以 root 身份运行时如何获取用户的语言环境?

c++ - 为什么 C++ 没有 &&= 或 ||= bool 值?

c++ - std::forward_list 和 std::forward_list::push_back

php - 检查 PHP 中是否为多字节字符串

java - 字符串中字符的快速排序

c - 是否有机器,其中 sizeof(char) != 1,或至少 CHAR_BIT > 8?