【问题标题】:Convert int to hex and store it into char buffer [duplicate]将int转换为十六进制并将其存储到char缓冲区中[重复]
【发布时间】:2017-01-20 06:09:15
【问题描述】:

我有一个整数向量。

vector <int> myvect;

我想将矢量数据转换成十六进制格式并存储到一个字符数组中。

如何将整数值转换为 (0x..) 形式的十六进制并将其存储到字符缓冲区中?

【问题讨论】:

标签: c++


【解决方案1】:

您可以使用该函数将任何类型转换为十六进制格式的 std::string,然后您可以将其转换并存储为您喜欢的任何内容。

模板化函数及其依赖包括:

#include <string>
#include <sstream>
#include <iomanip>

template<typename T>
typename std::enable_if<std::is_integral<T>::value, std::string>::type         
toHex(const T& value) {
    std::stringstream convertingStream;
    convertingStream << "0x" << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex << value;
    return convertingStream.str();
}

及功能用法:

#include <vector>

int main() {
    std::vector<int> myvect;
    myvect.push_back(2);
    myvect.push_back(33);
    myvect.push_back(66);
    myvect.push_back(99);
    myvect.push_back(-1);
    myvect.push_back(-1024);

    std::vector<std::string> mybuff;

    for (const auto& integer : myvect) {
        mybuff.push_back(toHex(integer));
        printf("%s\n", (*mybuff.rbegin()).c_str());
    }
    return 0;
}

但我强烈建议你以后寻找类似的问题并尝试编写代码,如果你仍然不知道如何解决问题,那么再发布一个新问题。

【讨论】:

    【解决方案2】:

    试试这个。

    #include <iostream>
    #include <iomanip>
    
    int main()
    {
        int input ;
        std::cout << "Enter decimal number: " ;
        std::cin >> input ;
        std::cout << "0x" << std::hex << input << '\n' ;
    }
    

    【讨论】:

      【解决方案3】:

      我知道用户要求将其存储在 char 数组中,但为了简单起见,我选择使用 std::string。看看这个函数只是为了看看它在做什么。

      #include <string>
      #include <vector>
      #include <stringstream>
      #include <iostream>
      
      
      std::vector<std::string> decimalToHex( std::vector<unsigned> decimalValues ) {
          std::vector<std::string> output;
          for each ( auto value in decimalValues ) {
              std::stringstream hex;
              hex << "0x";
              if ( (value % 16) == 0 ) {
                  hex << "0";
                  hex << std::hex << decimalValues[value];
              } else {
                  hex << std::hex << decimalValues[value];
              }
              output.push_back( hex.str() );
          }
          return output;
      }
      
      int main() {
          std::vector<unsigned values>;
          for ( unsigned u = 0; u < 256; u++ ) {
              values.push_back( u );
          }
      
          std::vector<std::string> results = decimalToHex( values );
      
          for ( unsigned u = 0; u < results.size(); ++u ) {
              std::cout << results[u] << std::endl;
          }
      
          return 0;
      }
      

      上述函数采用unsigned integervector 值,并通过在stringstream 类对象上使用bit shiftinsertion operators 将每个value 转换为hex value。它会在所有值之前预先附加"0x",如果值为&lt;= 15,它还会预先附加0 以获得更好的格式,否则它只会填充值。对于 2 列格式,这仅适用于 2550xff,因为这仅占十六进制数字中的 2 列,但该函数仍将转换超过 255 或 0xff 的数字。只要数据类型可以支持这样的数字,这将适用于您需要的尽可能多的数字。现在至于将其保存为char array,我相信您应该能够将std::string 转换为char array

      在填充无符号整数向量的 main 中,您可以将其更改为 u &lt; 65536 并观察函数发挥其魔力。最后一个输出应该是0xffff,这应该是正确的,因为十进制的 65,535 等于十六进制的 0xffff。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-24
        • 2011-11-27
        • 1970-01-01
        • 2015-04-15
        • 1970-01-01
        • 2016-07-05
        • 2018-11-11
        • 2016-03-14
        相关资源
        最近更新 更多