-3

I have a string like

string test = "48656c6c6f20576f726c64"; 

and I would like to convert it to

unsigned char state[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 
                           0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};

unsigned char bytearray[60];
int w;
for (w=0;w<str.length();w+2) {
    bytearray[w] = "0x" + str2[w];  
}

It doesn't seem to work. Any help would be appreciated

2
  • You need the std::hex` iostream manipulator, and a std::ostringstream. or a std::istringstream Commented Jul 21, 2018 at 13:53
  • 1
    Firstly, you are conceptually assigning multiple characters, "0x" (2 characters), to a single character element, bytearray[w]. I recommend reviewing the sections about data types in your favorite C++ reference. A single element in a character array can only contain a single character, not multiple characters. Commented Jul 21, 2018 at 16:33

2 Answers 2

0

Try something more like this instead:

#include <vector>
#include <string>

std::string test = "48656c6c6f20576f726c64"; 
size_t numbytes = test.size() / 2;

std::vector<unsigned char> bytearray;
bytearray.reserve(numbytes);

for (size_t w = 0, x = 0; w < numbytes; ++w, x += 2) {
    unsigned char b;

    char c = test[x];
    if ((c >= '0') && (c <= '9'))
        b = (c - '0');
    else if ((c >= 'A') && (c <= 'F'))
        b = 10 + (c - 'A');
    else if ((c >= 'a') && (c <= 'f'))
        b = 10 + (c - 'a');
    else {
        // error!
        break;
    }

    b <<= 4;

    c = test[x+1];
    if ((c >= '0') && (c <= '9'))
        b |= (c - '0');
    else if ((c >= 'A') && (c <= 'F'))
        b |= 10 + (c - 'A');
    else if ((c >= 'a') && (c <= 'f'))
        b |= 10 + (c - 'a');
    else {
        // error!
        break;
    }

    bytearray.push_back(b);
}

// use bytearray as needed...

Alternatively:

#include <vector>
#include <string>
#include <iomanip>

std::string test = "48656c6c6f20576f726c64"; 
size_t numbytes = test.size() / 2;

std::vector<unsigned char> bytearray;
bytearray.reserve(numbytes);

for(size_t w = 0, x = 0; w < numbytes; ++w, x += 2)
{
    std::istringstream iss(test.substr(x, 2));
    unsigned short b;

    if (!(iss >> std::hex >> b)) {
        // error!
        break;
    }

    bytearray.push_back(static_cast<unsigned char>(b));
}

// use bytearray as needed...

Live Demo

Sign up to request clarification or add additional context in comments.

Comments

-1

explained here C++ convert hex string to signed integer

so do something like this:

unsigned int x;  
std::string substring; // you will need to figure out how to get this
std::stringstream ss;
ss << std::hex << substring;
ss >> x;

x is what you would need to store an your array. "48" would actually be a parsed part of your string. Look here So you might need to change the type. Play around with it. Also i think you were parsing your string incorrectly. check this out Split string using loop to specific length sub-units

2 Comments

sorry i miss typed. i meant to say substing would actually be the parsed part of your string. your array is a char array so you might need to change the type of x. Also i think you were parsing your string incorrectly. check this out Split string using loop to specific length sub-units
hey phaile thank you for the reply. I already used your above method and fed it to string test but I want to make it in 0x00, 0x11 format and put it into the char array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.