How convert vector 2D to char array??

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
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.
for example vector<vector<unsigned char>> is:E200207705030085231027F330E2008318180C01081900518C5A
then output is string a:E200207705030085231027F330 ,
string b:E2008318180C01081900518C5A show in output ,how??
Last edited on
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
@shadowmouse thank you,but doesn't matter vector<vector<unsigned char>> or vector<vector<char>> is it?? beacause my question about unsigned char!!!
I use this code but my output is bad word :( :( :(
Last edited on
Have a look at http://www.cplusplus.com/reference/string/to_string/ Because unsigned char is numeric, you have to convert it to a string.
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
anyone?plz
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.
@shadow hmmm thank you I understand your mean know :)
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
#include <string>

std::string to_string( const std::vector< std::vector< unsigned char > >& bytes )
{
    std::string result ;
    for( const auto& vec : bytes ) for( char c : vec ) result += c ;
    return result ;
}

int main()
{
    std::vector< std::vector< unsigned char > > seq { {'a','b','c'}, {'d','e','f', 'g' }, {}, {'h', 'i','j'} } ;
    std::cout << to_string(seq) << '\n' ;
}

http://coliru.stacked-crooked.com/a/b2e0e995e0ab7c19
> my compiler doesn't resolve to_string

What's the text of the error diagnostic?
@JLBorges tank you but my compiler error:

range-based ‘for’ loops are not allowed in C++98 mode

what can use instead of for ??
Either use classical for-loops
1
2
3
4
5
6
7
8
9
10
std::string to_string( const std::vector< std::vector< unsigned char > >& bytes )
{
    std::string result ;

    for( std::size_t row = 0 ; row < bytes.size() ; ++row  )
        for( std::size_t col = 0 ; col < bytes[row].size() ; ++col )
           result += bytes[row][col] ; // unsigned char implicitly converted to char

    return result ;
}


Or compile with the option -std=c++11
I use this code before but doesn't cout correctly : �`ɤ� w�#'���<< >Q�
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
To convert from the value of the unsigned char as an integer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>
#include <string>
#include <sstream> // *** added
#include <iomanip> // *** added 

std::string to_string( const std::vector< std::vector< unsigned char > >& bytes )
{
    std::string result ;

    for( std::size_t row = 0 ; row < bytes.size() ; ++row  )
        for( std::size_t col = 0 ; col < bytes[row].size() ; ++col )
        {
            std::ostringstream stm ;
            stm << std::hex << std::uppercase << std::setfill('0') << std::setw(2) << int( bytes[row][col] ) ;
            result += stm.str() ;
        }

    return result ;
}
@JLBorges yeeeeeeeeeeeeeeeees tank you very very much it is work....
Topic archived. No new replies allowed.