【问题标题】:How to use setfill and setw to stock an hex value in a string variable如何使用 setfill 和 setw 在字符串变量中存储十六进制值
【发布时间】:2018-09-28 20:03:59
【问题描述】:

我读取一个文件并将其转换为像这样的十六进制符号{0x4D,x0FF,0x01} 我将它存储在一个无符号字符数组中。 我可以打印我想储存的东西,但我无法在我的数组中储存数据。 我阅读了 bitset 类文档,但我不确定它是否是我需要的。 根据此源代码,我怎样才能读取存储数据以得到与此相同的结果:

unsigned char array[3] = {0x4D,x0FF,0x01};

请注意,向量没有很好的符号,这就是我使用 setfill 和 setw 的原因。

    size = file.tellg();
    unsigned char* rawData = new unsigned char[size];
    memblock = new uint8_t[size];
    std::vector<uint8_t> memblock(size);
    file.seekg(0, ios::beg);
    file.read(reinterpret_cast<char*>(memblock.data()), size);
    file.close();
    for (int i = 0; i < size; i = i++)
    {
        if (i == (int)size - 1)
        {
            cout << "0x" << setfill('0') << setw(2) << std::hex << (unsigned)memblock.at(i);
        }
        else
        {
            cout << "0x" << setfill('0') << setw(2) << std::hex << (unsigned)memblock.at(i) << ",";
        }

    }

编辑:这是我的实际代码:

    unsigned char* rawData[1] = {0x00}; // My rawData in out of function.

 void readFile(std::string p_parametre, unsigned char* rawData[])
{
    std::ifstream input{ p_parametre, std::ios::binary };
    if (!input.is_open()) {  // make sure the file could be opened
        std::cout << "Error: Couldn't open\"" << p_parametre << "\" for reading!\n\n";
    }

    // read the file into a vector
    std::vector<unsigned char> data{ std::istream_iterator<unsigned char>{ input },
                                     std::istream_iterator<unsigned char>{} };

    std::ostringstream oss;  // use a stringstream to format the data

                                 // instead of the glyph
    for (int i = 0; i < data.size(); i++)
    {
        if (i == i- 1)
        {
            oss <<'0'
                << 'x'
                << std::setfill('0') << std::setw(2) << uppercase << std::hex << static_cast<int>(data.at(i));
        }
        else
        {
            oss << '0'
                << 'x'
                << std::setfill('0') << std::setw(2) << uppercase << std::hex << static_cast<int>(data.at(i)) << ',';
        }

    }

    // create a unique_ptr and allocate memory large enough to hold the string:
    std::unique_ptr<unsigned char[]> memblock{ new unsigned char[oss.str().length() + 1] };

    // copy the content of the stringstream:
    int r = strcpy_s(reinterpret_cast<char*>(memblock.get()), oss.str().length() + 1, oss.str().c_str());

    OpenFile(memblock.get());
    getchar();
}

【问题讨论】:

  • 您不会“转换”任何东西,您只需将文件字节读入向量memblock。 “我可以打印我想要存储的内容,但我无法在我的数组中存储数据。”股票数据是什么意思?你想要一个将每个字节表示为十六进制数字的字符串吗?
  • 你想要一个将每个字节表示为十六进制数字的字符串吗? 确切地说,我希望我的无符号字符串数组中的每个字节都表示为十六进制。
  • 查看我的更新答案。
  • @Swordfish,感谢您的回答,但我绝对需要填写类型为 unsigned int[] 的 rawData 变量,因为我有一些限制。此外,不推荐使用 strcpy 作为 Visual Studio 2017。
  • memblock.get() unsigned int[] aka unsigned int *。 “此外,不推荐使用 strcpy 作为 Visual Studio 2017。”好吧,使用std::strncpy()?不过,我没有收到贬值警告,它是 std 的一部分。

标签: c++ arrays unsigned-char setw


【解决方案1】:
#include <cstdlib>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <memory>
#include <cstring>

int main()
{
    char const *input_filename{ "test.txt" };
    std::ifstream input{ input_filename, std::ios::binary };
    if (!input.is_open()) {  // make sure the file could be opened
        std::cout << "Error: Couldn't open\"" << input_filename << "\" for reading!\n\n";
        return EXIT_FAILURE;
    }

    // read the file into a vector
    std::vector<unsigned char> data{ std::istream_iterator<unsigned char>{ input },
                                     std::istream_iterator<unsigned char>{} };

    std::ostringstream oss;  // use a stringstream to format the data
    for (auto const &d : data)       // iterate over every element in vector data
        oss << std::setw(2)          // set the field-width to 2
            << std::setfill('0')     // fill up unused width with '0' instead space
            << std::hex              // output as hex number
            << static_cast<int>(d);  // cast the character to an int cause we
                                     // want to print the numeric representation
                                     // instead of the glyph

    // > Exact, I would like each byte as hex in my unsigned string array.

    // create a unique_ptr and allocate memory large enough to hold the string:
    std::unique_ptr<unsigned char[]> memblock{ new unsigned char[oss.str().length() + 1] };

    // copy the content of the stringstream:
    std::strcpy(reinterpret_cast<char*>(memblock.get()), oss.str().c_str());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2023-04-02
    • 1970-01-01
    • 2016-08-07
    • 2020-06-15
    • 1970-01-01
    • 2019-10-12
    相关资源
    最近更新 更多