Aug 19, 2015 at 7:24pm UTC
I want to convert vector<vector<unsigned char > > v
to char a[12]
and then convert char a[12]
to string
?? how can do this in c or c++??tank you
Last edited on Aug 19, 2015 at 7:25pm UTC
Aug 19, 2015 at 7:37pm UTC
How do you want to convert it? It is not at all obvious because a vector of vectors of unsigned char is something quite different from an array of 12 chars .
Aug 20, 2015 at 9:56am UTC
for example vector<vector<unsigned char >> is:E200207705030085231027F330E2008318180C01081900518C5A
then output is string a:E200207705030085231027F330
,
string b:E2008318180C01081900518C5A
show in output ,how??
Last edited on Aug 20, 2015 at 10:08am UTC
Aug 20, 2015 at 10:02am UTC
You could go directly from vector to string
1 2 3 4 5 6 7 8
std::vector<std::vector<char >> char_vector;
//Fill with chars
std::string result;
for (auto & i : char_vector) {
for (auto & j : i) {
result += j;
}
}
and then if you particularly want a char array, you can use c_str
Last edited on Aug 20, 2015 at 10:02am UTC
Aug 20, 2015 at 10:12am UTC
@shadowmouse thank you,but doesn't matter vector<vector<unsigned char >>
or vector<vector<char >>
is it?? beacause my question about unsigned char!!!
Aug 20, 2015 at 10:17am UTC
I use this code but my output is bad word :( :( :(
Last edited on Aug 20, 2015 at 10:17am UTC
Aug 20, 2015 at 4:18pm UTC
Why I can't see
cout<<result<<endl;
or in this code:
1 2 3 4 5 6 7 8 9 10
string text;
for ( unsigned int row = 0; row < cards.size(); ++row)
{
for (unsigned int column = 0; column < cards[row].size(); ++column)
{
const char c = cards[row][column];
text += c;
}
text += "\n" ; // Use newline as row separator
}
why I can't see
cout<<text<<endl;
?? just print bad word!!
my compiler doesn't resolve
to_string
Last edited on Aug 20, 2015 at 4:27pm UTC
Aug 21, 2015 at 7:18am UTC
The point is that unsigned char is a numeric type, whereas char represents an ASCII character. This means that when you put the value of an unsigned char into a char, it finds what that number represents in ASCII. Is there any chance you could upgrade to C++11 and get to_string()? It's by far the easiest way to do this.
Aug 21, 2015 at 8:00am UTC
@shadow hmmm thank you I understand your mean know :)
Aug 23, 2015 at 6:20am UTC
I use
stringstream
instead of to_string but doesn't
cout
correctly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
string text;
for (int var = 0; var < (unsigned char ) cards.size(); var++) {
printf("sizeof cards:%d" , cards.size());
for (int z = 0;z < (unsigned char ) cards[var].size(); z++) {
printf("size of cards member:%d" ,cards[var].size());
printf("cards:%02X\n" , cards[var][z]);
const char c = cards[var][z];
printf("c:%02X\n" , c);
text += c;
stringstream stream;
stream << text.c_str();
cout << stream << endl;
}
text += "\n" ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
sizeof cards:3
size of cards member:12
cards:E2
c:E2
size of cards member:12
cards:00
c:00
size of cards member:12
cards:20
c:20
size of cards member:12
cards:67
c:67
size of cards member:12
cards:55
c:55
size of cards member:12
cards:0A
c:0A
...........
but cout of stream is
�`ɤ� w�#'���<<>Q�
Last edited on Aug 23, 2015 at 6:20am UTC
Aug 23, 2015 at 8:29am UTC
> my compiler doesn't resolve to_string
What's the text of the error diagnostic?
Aug 23, 2015 at 9:20am UTC
@JLBorges tank you but my compiler error:
range-based ‘for ’ loops are not allowed in C++98 mode
what can use instead of for ??
Aug 23, 2015 at 9:31am UTC
I use this code before but doesn't cout correctly : �`ɤ� w�#'���<<>Q�
Aug 23, 2015 at 9:32am UTC
what can use instead of for ??
Highy suggested is to stop using standard which soon would be able to drink legally and turn on C++11/C++14 mode.
If you are using Code::Blocks:
http://stackoverflow.com/a/22859334
Edit: show your code. Do you use non-ascii characters?
Last edited on Aug 23, 2015 at 9:36am UTC
Aug 24, 2015 at 4:13am UTC
@JLBorges yeeeeeeeeeeeeeeeees tank you very very much it is work....