0

I have an unsigned char* c that contains the element 0x1c. How can I add it into an std::vector<unsigned char>vect? I am working in c++.

std::vector<unsigned char>vect;  //the vect dimention is dynamic

std::string at="0x1c";
c=(unsigned char*)(at.c_str());
vect[1]=c //error? why?
5
  • 3
    I'd say the error is because vect[1] is an unsigned char while c is a pointer to one. Maybe vect[1] = *c? I haven't used C++ in a while though. Commented Jan 19, 2011 at 15:11
  • 1
    Note: You probably want vect[0] rather than vect[1] if you want to set the first value. Also, what error are you actually getting? Commented Jan 19, 2011 at 15:12
  • it i leave vect[0]=c i have the following error: error: invalid conversion from ‘unsigned char*’ to ‘unsigned char’ Commented Jan 19, 2011 at 15:16
  • if i put vect[0]=*c ..i have segmentation faul Commented Jan 19, 2011 at 15:18
  • @key Because you're trying to set the value of the first element, but you haven't made any elements yet. I recommend getting yourself a good C++ book; the basics of containers will be covered in it. Commented Jan 19, 2011 at 15:25

3 Answers 3

3
//The vect dimension is dynamic ONLY if you call push_back
std::vector <std::string> vect;  

std::string at="0x1c";
vect.push_back(at);

If you are using C++, use std::string. The above code will copy your "0x1c" string into the vector.

If you try to do

vect[0] = c;

Without first expanding the vector with

vect.resize(1);

You will get segmentation fault because operator[] doesn't expand the the vector dynamically. The initial size of a vector is 0 btw.

UPDATE: According to the OP's comment, here is what he would want: copying a unsigned char * to a std::vector (i.e.copying a C array to a C++ vector)

std::string at = "0x1c";
unsigned char * c = (unsigned char*)(at.c_str());
int string_size = at.size();

std::vector <unsigned char> vect;

// Option 1: Resize the vector before hand and then copy
vect.resize(string_size);
std::copy(c, c+string_size, vect.begin());

// Option 2: You can also do assign
vect.assign(c, c+string_size);
Sign up to request clarification or add additional context in comments.

4 Comments

i need to use std::vector<unsigned char> vect;
that code makes no sense.. why would you cast the const char* returned by c_str() to an unsigned char* and then call non existent method pushback with that? I don't believe there is a ctor of std::string which takes an unsigned char*!
I guess the point is that the OP has not really made what he wants clear, what you're doing above is basically going to insert 4 entries in to the vector, where each character's value will be inserted into the vector - i.e. [ '0', 'x', '1', 'c']. The OP has not said whether he wants this or a single entry in the vector with the value 0x1c.
@Nim: I agree. I just give a solution based on my interpretation.
0

c is an unsigned char*. vect is a std::vector<unsigned char>, so it contains unsigned char values. The assignment will fail, as operator [] on std::vector<unsigned char> expects an unsigned char, not a unsigned char *.

Comments

0

You have a hex representation of a character in a string, and you want the character?

easiest:

unsigned char c;
istringstream str(at);
str >> hex >> c; // force the stream to read in as hex
vect.push_back(c);

(I think that should work, have not tested it)


I just reread your question again, this line:

I have an unsigned char* c that contains the element 0x1c

Does this mean that actually your unsigned char* looks like this:

unsigned char c[] = {0x1c}; // i.e. contains 1 byte at position 0 with the value 0x1c?

or my assumption above...


to print the vector out to cout, use a simple for loop, or if you are feeling brave

std::cout << std::ios_base::hex;

std::copy(vect.begin(), vect.end(), std::ostream_iterator<unsigned char>(std::cout, " "));

std::cout << std::endl;

this will print the hex representations of each of the unsigned char values in the vector separated by a space.

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.