【问题标题】:C++ Convert Char Array To Hex StringC++ 将字符数组转换为十六进制字符串
【发布时间】:2018-02-22 02:41:15
【问题描述】:

我正在一台非常旧的机器上工作,我用来编译我的C++ 可执行文件的SDK 不支持string,所以我需要使用char arrays

此代码适用于将string 转换为hex

std::string string_to_hex(const std::string& input)
{
    static const char* const lut = "0123456789ABCDEF";
    size_t len = input.length();

    std::string output;
    output.reserve(2 * len);
    for (size_t i = 0; i < len; ++i)
    {
        const unsigned char c = input[i];
        output.push_back(lut[c >> 4]);
        output.push_back(lut[c & 15]);
    }
    return output;
}

但该函数适用于 string 数据类型,我无法使用。

我也尝试过使用它,但无济于事。

char *hextostrTest(char *hexStr)
{
    size_t len = strlen(hexStr);
    int k = 0;
    if (len & 1) return NULL;

    char* output = new char[(len / 2) + 1];
    for (size_t i = 0; i < len; i += 2)
    {
        output[k++] = (((hexStr[i] >= 'A') ? (hexStr[i] - 'A' + 10) : (hexStr[i] - '0')) << 4) |
            (((hexStr[i + 1] >= 'A') ? (hexStr[i + 1] - 'A' + 10) : (hexStr[i + 1] - '0')));
    }
    output[k] = '\0';
    return output;
}

【问题讨论】:

  • 你试过 string.c_str() 吗?
  • @HyunIKim 如上所述,由于这个旧的损坏的 SDK,我无法使用字符串数据类型,但它是唯一可用于我正在使用的旧机器的 SDK,所以我需要处理它。我只能使用 char 数组
  • 为什么不只是替换 string 部分,而是重写整个函数?
  • @appleapple 因为移植 reserve()push_back() 函数来处理 char 数组会很麻烦。
  • 你用分配内存替换reserve()push_back 很容易实现。无论如何,如果您有hextostrstd::string,请发布。 string_to_hex 无济于事

标签: c++ string hex


【解决方案1】:

非常相似的东西:

const char* string_to_hex(const char *str, char *hex, size_t maxlen)
{
    static const char* const lut = "0123456789ABCDEF";

    if (str == NULL) return NULL;
    if (hex == NULL) return NULL;
    if (maxlen == 0) return NULL;

    size_t len = strlen(str);

    char *p = hex;

    for (size_t i = 0; (i < len) && (i < (maxlen-1)); ++i)
    {
        const unsigned char c = str[i];
        *p++ = lut[c >> 4];
        *p++ = lut[c & 15];
    }

    *p++ = 0;

    return hex;
}

int main()
{
    char hex[20];
    const char *result = string_to_hex("0123", hex, sizeof(hex));
    return 0;
}

【讨论】:

    【解决方案2】:

    只需对 std::string 使用相同的函数,但使用 char *,

    #include <iostream>                                                    
    #include <cstdlib>                                                     
    #include <cstring>                                                     
    #include <string>                                                      
    
    using namespace std;                                                   
    
    char *string_to_hex(char *input) {                                     
        static const char *const lut = "0123456789ABCDEF";                 
        size_t len = strlen(input);                                        
        int k = 0;                                                         
        if (len & 1)                                                       
            return NULL;                                                   
    
        char *output = new char[(len / 2) + 1];                            
    
        for (size_t i = 0, j = 0; i < len; i++, j += 2) {                  
            const unsigned char c = input[i];                              
            output[j]     = lut[c >> 4];                                       
            output[j + 1] = lut[c & 15];                                   
        }                                                                  
    
        return output;                                                     
    }                                                                      
    
    std::string string_to_hex(const std::string &input) {                  
        static const char *const lut = "0123456789ABCDEF";                 
        size_t len = input.length();                                       
    
        std::string output;                                                
        output.reserve(2 * len);                                           
        for (size_t i = 0; i < len; ++i) {                                 
            const unsigned char c = input[i];                              
            output.push_back(lut[c >> 4]);                                 
            output.push_back(lut[c & 15]);                                 
        }                                                                  
        return output;                                                     
    }                                                                      
    
    int main() {                                                           
        string test = "Test";                                              
        std::string res(string_to_hex(test.c_str()));                      
        cout << res << endl;                                               
        res = string_to_hex(test);                                         
        cout << res << endl;                                               
    }                                                                      
    

    【讨论】:

      【解决方案3】:

      您剪切的两个代码看起来像相反的代码。

      char* string_to_hex(const char* input) {
          static const char* const lut = "0123456789ABCDEF";
          size_t len = strlen(input);
      
          char* output = new char[len*2+1];
          int index = 0;
          for(size_t i = 0; i < len; ++i) {
              const unsigned char c = input[i];
              output[index++] = lut[c >> 4];
              output[index++] = lut[c & 15];
          }
          output[index] = '\0';
          return output;
      }
      

      这是第一个代码的字符串 -> char* 版本。

      【讨论】:

        猜你喜欢
        • 2018-01-31
        • 2017-06-17
        • 1970-01-01
        • 1970-01-01
        • 2022-07-22
        • 2014-06-09
        • 2018-06-06
        • 1970-01-01
        • 2017-02-23
        相关资源
        最近更新 更多