1

I'm working on a very old machine in which the SDK I use to compile my C++ executable's does not support string, so I need to work with char arrays.

This code works fine for converting a string to 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;
}

But the function works with the string data type, which I cannot use.

I've tried using this as well, but to no avail.

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;
}
9
  • have you tried string.c_str() ? Commented Feb 22, 2018 at 2:43
  • @HyunIKim Like mentioned above, I'm unable to use the string data type due to this old broken SDK, but it is the only SDK available for the old machine I am working with so I need to deal with it. I can only work with char arrays Commented Feb 22, 2018 at 2:43
  • why you don't just replace the string part but rewrite the whole function? Commented Feb 22, 2018 at 2:46
  • @appleapple Because porting the reserve() and push_back() functions to work with char arrays would be a pain in the ass. Commented Feb 22, 2018 at 2:52
  • you replace reserve() with alloc memory, push_back is pretty easy to implement. Anyway, if you have hextostr with std::string, post it. string_to_hex helps nothing Commented Feb 22, 2018 at 2:53

3 Answers 3

2

Something very similar:

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just use the same function for std::string but with 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;                                               
}                                                                      

Comments

0

Two codes you clipped look like opposite codes.

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;
}

This is the string -> char* version of the first code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.