-1

I have a question that I have been wondering for a long time.

Is it possible to append a hex string to a byte array as it is in C++?

Example:

string hexString = "0x30";

I want to append to a byte array or a vector like this.

static const unsigned char example[] = {
   0x30
}

How can I do that?

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Please do not tag with all existing versions of C++, only use relevant tags. If the question is about C++ in general just tag C++. – Marco Bonelli Jun 01 '21 at 22:04
  • @marco I am so sorry. I am using it for the first time. Forgive my ignorance. – cesqueax Jun 01 '21 at 22:07
  • 1
    I don't think there is any standard library function that will get the job done. You'll have to write code to parse the string, compute the value of the character from string, and then append the character to the byte array. I'll wait for somebody to prove me wrong :) – R Sahu Jun 01 '21 at 22:12
  • @cesqueax no worries, I've gone ahead and modified the tags for you. – Marco Bonelli Jun 01 '21 at 22:18
  • How long might `hexString` be? – Paul Sanders Jun 01 '21 at 22:25
  • From the Just In Case file: Remember that the size of an array is fixed at creation. You cannot append to `example`. It is full at one byte. – user4581301 Jun 01 '21 at 22:25
  • You can’t append anything to an array, an array’s size is fixed at compile time. You could convert the string into the equivalent binary data and append it to a std::vector though. (Or better yet, avoid the hex string entirely and just declare the data as an array of unsigned chars instead, so no string-parsing is necessary) – Jeremy Friesner Jun 01 '21 at 22:28
  • Something like this might be tweaked to do what you want: https://onlinegdb.com/ns5Sx6rII – Jerry Jeremiah Jun 01 '21 at 22:43
  • @JerryJeremiah Thank you so much! So, do you have any idea how I can insert it into a vector? – cesqueax Jun 01 '21 at 22:48
  • @cesqueax Simply use [`vector::push_back()`](https://en.cppreference.com/w/cpp/container/vector/push_back) – Remy Lebeau Jun 01 '21 at 23:02
  • @JerryJeremiah `std::stoi()` does prefix checking for you if you set `base=0`. The only format it wouldn't handle is the `b` suffix for base 2. – Remy Lebeau Jun 01 '21 at 23:04
  • @RemyLebeau That's why I like stackoverflow - you learn something new every day! – Jerry Jeremiah Jun 02 '21 at 01:16

2 Answers2

0

You can use the following. istringstream lets you read from a string. hex lets you read hexadecimal.

#include <iostream>
#include <sstream>

using namespace std;

string hexString = "0x30";

static unsigned char example[16] = {0};

int main()
{
    int pos = 0;
    istringstream iss(hexString);

    int x;
    while(iss >> hex >> x)
    {
        cout << x << std::endl;
        example[pos++] = x;
    }
}
Jeffrey
  • 11,063
  • 1
  • 21
  • 42
  • This will let you write past the end of the array. You'll need code to deal with that. This also converts `int` to `char`, another thing to deal with. – Jeffrey Jun 01 '21 at 22:33
  • 1
    And don't use `using namespace std` in production code. It's unmaintainable. – Jeffrey Jun 01 '21 at 22:34
  • Exactly what I want is a string structure, for example: std::string x = "0x30"; Adding the value x to a vector in the same way. So: std::vector bytes { 0x30 }; – cesqueax Jun 01 '21 at 22:35
  • 2
    [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/) – Remy Lebeau Jun 01 '21 at 23:01
  • @cesqueax you won't get to rewrite the C++ language. The code above can fill a vector or array from a string. I'm not getting what exactly you mean by "what I want" – Jeffrey Jun 02 '21 at 02:47
0

Use the stringstream and std::hex. Look this example, later just you add (char)x in your array.

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    string hexString = "0x30";
    unsigned int x;   
    
    stringstream ss;
    ss << hex << hexString;
    ss >> x;
    
    return 0;
}
Arcaniaco
  • 400
  • 2
  • 10
  • `std::hex` has no effect on inserting a `std::string`, but it does affect reading an integer, so you should remove `hex` from before `<< hexString` and add it before `>> x` instead: `ss << hexString; ss >> hex >> x;` That being said, consider using [`std::stoi()`](https://en.cppreference.com/w/cpp/string/basic_string/stol) instead of a `stringstream`, eg: `unsigned int x = stoi(hexString, nullptr, 16);` – Remy Lebeau Jun 01 '21 at 23:00