1

I have a string value: std::string bl="0x"+"a0"; //where a0 is a heva number. I did add 0x because i want my vector

std::vector<unsigned char>vect; 

vect.push_back(bl.begin(), bl.end()); //error is not working.

Need help. What to do? I am working in ubuntu c++ code.

8
  • std::vectorvect is not valid C++ code. Please specific std::vector<T> for some type T. Then, what values of type T do you expect the string bl to be converted into? Commented Jan 19, 2011 at 10:09
  • i've edited my post: it's std::vector<unsigned char> vect; And the string bl is a simple string containg a hex value:) Commented Jan 19, 2011 at 10:11
  • 1
    The letter 'o' is not a valid hex digit. Commented Jan 19, 2011 at 10:12
  • 1
    it was 0. Sorry for mistake:). So what is the result? how should i write? Commented Jan 19, 2011 at 10:15
  • see also stackoverflow.com/questions/3825553/… Commented Jan 19, 2011 at 10:20

4 Answers 4

7

I'm not 100% sure on what you want, because of the contents of your string bl.

Taking you literally:

std::string bl = "0xA0"; // ^ this is what you meant to write ("0x"+"A0" is actually adding pointers) std::vector<unsigned char> vect; vect.insert(vect.begin(), bl.begin(), bl.end()); // ^ you use ranges with .insert not push_back 

Or you can use the constructor:

std::string bl = "0xA0"; std::vector<unsigned char> vect(bl.begin(), bl.end()); // ^ you use ranges with the constructor too 

In both these cases, the vector contains the characters '0', 'x', 'A' and '0'.

Alternatively, you might have meant for the string to contain the single character whose ASCII value is (in hex) 0xA0. If so, "0x"+"a0" is very wrong.

std::string bl = "\xA0"; std::vector<unsigned char> vect(bl.begin(), bl.end()); 

The vector contains one character, whose ASCII value is 0xA0.

I hope this helps.

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

1 Comment

Disclaimer: I haven't actually tested this and it's possible that you'll need a std::vector<char>.
1

Do not use push_back(...), it classically can not push an interval, it pushs the one item. You can use assign(begin, end) or insert(iter_position, vect_begin, vect_end).

For example:

vect.assign(bl.begin(), bl.end()); vect.insert(vect.begin(), bl.begin(), bl.end()); vect.push_back(bl[0]); // '0' vect.push_back(bl[1]); // 'x' vect.push_back(bl[2]); // 'a' vect.push_back(bl[3]); // '0' 

Comments

0

give type to vector. std::vector vec;
to
std::vector< string> vec or whatever you need. and vector push_back signature is void push_back(const T&). It does not take two arguments.

4 Comments

refer to stackoverflow.com/questions/2206050/… if you want to convert string to unsigned char.
No, std::vector<string> is not what he means.
@std258367: You can't convert a string to an unsigned char. Perhaps you meant unsigned char array (as did the author of that question that you linked us to).
@Tomalak Yes i meant unsigned char aaray
-1

the reason is simple, you are trying to insert an invalid element into your container.

std::string has no implicit conversion to change its data into vector of unsigned char.

1 Comment

What? I don't see anywhere where he attempted an implicit conversion of a string to a vector.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.