how to convert char* to vector<char> ?

J

jiing

[my bad code]
char ch ;
unsigned int temp_counter =0;
vector<int> buffer, buffer4;
vector<char> charBuffer;
while(! fin.eof() ){
fin.get( ch );
int temp;
(int(ch) < 0) ? (temp = int(ch)+256) : (temp = int(ch) );
buffer.push_back(temp);
charBuffer.push_back(ch);
}


//vector<char> to unsigned char*
char* buffer0;
buffer0 = & charBuffer; //????? here is a lot of questions



//unsigned char* to vector<int>
// I am not quite sure the following is right or not
for(unsigned int i = 0; i<charBuffer.size(); i++){
buffer4.push_back( ((int) *buffer0)+256 );
buffer0++;
}
[/my bad code]

I have two questions owing to poor knowledge of pointer
How to transform vector<char> to char*
and how to transform char* to vector<int>
Can any one teach me?
Thanks in advance.

-jiing-
 
Y

Yuriy Solodkyy

Try the following if you just need to read characters into a vector:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
std::vector<char> text;

std::copy(
std::istream_iterator<char>(std::cin),
std::istream_iterator<char>(),
std::back_inserter(text)
);

// to output vector back use the following:
std::copy(
text.begin(),
text.end(),
std::eek:stream_iterator<char>(std::cout, "")
);

return text.size();
}

Yuriy
 
J

jiing

I'm trying to integrate others' code.
my function input is vector<int>
But other's output is char*
so I want to transform char* to vector<int> or vector<char>

-jiing-
 
J

jiing

//vector<char> to unsigned char* //this is to make some test data
for testing
char* buffer0;
char* tempBuffer0;
for (unsigned int i=0; i < charBuffer.size(); i++) {
buffer0 = &(charBuffer);
tempBuffer0 = buffer0;
buffer0++;
}



//unsigned char* to vector<int>
for(unsigned int i = 0; i<charBuffer.size(); i++){
buffer4.push_back( ((int) *tempBuffer0)+256 );
tempBuffer0++;
}


dab_usb_get_lost( buffer4);
Can anyone tell me what's problem in my code and concept.
Thanks
-jiing-
 
D

Daniel Cer

jiing said:
[my bad code]
char ch ;
unsigned int temp_counter =0;
vector<int> buffer, buffer4;
vector<char> charBuffer;
while(! fin.eof() ){
fin.get( ch );
int temp;
(int(ch) < 0) ? (temp = int(ch)+256) : (temp = int(ch) );
buffer.push_back(temp);
charBuffer.push_back(ch);
}


//vector<char> to unsigned char*
char* buffer0;
buffer0 = & charBuffer; //????? here is a lot of questions



//unsigned char* to vector<int>
// I am not quite sure the following is right or not
for(unsigned int i = 0; i<charBuffer.size(); i++){
buffer4.push_back( ((int) *buffer0)+256 );
buffer0++;
}
[/my bad code]

I have two questions owing to poor knowledge of pointer
How to transform vector<char> to char*
and how to transform char* to vector<int>
Can any one teach me?

Here's some working code that demonstrates everything it looks like
you're trying to do (i.e. char* string representation ==> vector<int> as
well as vector<char> to char* string representation).

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
string input_line;
while(getline(cin, input_line)) {
// get the traditional C-style representation of input_line
// i.e. a null-terminate character array
const char *c_str = input_line.c_str();

////////////////////////////////////////
// char* to vector<int> representation
////////////////////////////////////////
vector<int> int_vec_rep;
for (int i=0; c_str; i++)
// it's not clear to me why in your sample code
// you add 256 here.....but it's included here
// for the sake of being consistent with the
// original code
int_vec_rep.push_back(((int)c_str)+256);

// Display vector<int> representation
cout<<"vector<int> generated from char *c_str"<<endl;
for (int i=0; i < int_vec_rep.size(); i++)
cout<<"\tvector<int> position["<<i<<"]: "<<int_vec_rep<<
" character rep("<<(char)int_vec_rep<<") "<<endl;

// char* to vector<char> representation
// needed to demonstrate how to turn a vector<char>
// representation back into a char* representation
vector<char> char_vec_rep;
for (int i=0; c_str; i++)
char_vec_rep.push_back(((int)c_str)+256);

////////////////////////////////////////
// vector<char> to char*
////////////////////////////////////////
char *char_rep = new char[char_vec_rep.size()+1];
int i;
for (i=0; i < char_vec_rep.size(); i++)
char_rep = char_vec_rep;
char_rep = '\0';

cout<<"Reconstructed char* representation: "<<char_rep<<endl;
}
return 0;
}

-Dan
 
D

Daniel Cer

//vector<char> to unsigned char* //this is to make some test data
for testing
char* buffer0;
char* tempBuffer0;
for (unsigned int i=0; i < charBuffer.size(); i++) {
buffer0 = &(charBuffer);
tempBuffer0 = buffer0;
buffer0++;
}


A few things....

1) It looks like you don't allocate any memory for the new character
array representation (i.e. char* buffer0). So, somewhere, probably right
before the 'for' loop, you should have something like the following:

buffer0 = new char[charBuffer.size()];

Also, if buffer0 should be null terminated then you should use the code
below instead:

buffer0 = new char[charBuffer.size()+1];

As an aside, making the character array null terminated maybe desirable
in some cases since it will allow you to operate over the character
array using any code from the C standard library designed for working
with C-style strings.

2) The loop should probably be re-written as either:

// if we're going to be incrementing buffer0 in the loop,
// then we need to store the original value and then
// restore it after the loop is completed
tempBuffer0 = buffer0;
for (unsigned int i=0; i < charBuffer.size(); i++) {
*buffer0 = charBuffer;
buffer0++;
}
buffer0 = tempBuffer0;

Or, even more elegantly as:

for (unsigned int i=0; i < charBuffer.size(); i++)
buffer0 = charBuffer

In either case, if you want buffer0 to be null-terminated, after the
loop have a statement like:

buffer0[charBuffer.size()] = '\0';
//unsigned char* to vector<int>
for(unsigned int i = 0; i<charBuffer.size(); i++){
buffer4.push_back( ((int) *tempBuffer0)+256 );
tempBuffer0++;
}

Presuming tempBuffer points to a character array that has the same
length as the vector charBuffer, then I think the above code should work.

However, if tempBuffer0 is null terminated, then you can remove your
dependency on using charBuffer.size() to determine the length of the
character array tempBuffer0.

So, for a null terminated character array, tempBuffer0, you could have:

for(unsigned int i = 0; tempBuffer0; i++){
buffer4.push_back( ((int) tempBuffer0)+256 );
}

Hope that helps,

-Dan
 
R

Rolf Magnus

jiing said:
[my bad code]
char ch ;
unsigned int temp_counter =0;
vector<int> buffer, buffer4;
vector<char> charBuffer;
while(! fin.eof() ){
fin.get( ch );
int temp;
(int(ch) < 0) ? (temp = int(ch)+256) : (temp = int(ch) );


You should either write:

temp = (int(ch) < 0) ? int(ch)+256 : int(ch);

or:

if (int(ch) < 0)
temp = int(ch)+256;
else
temp = int(ch);

or actually:

temp = unsigned char(ch);

buffer.push_back(temp);
charBuffer.push_back(ch);
}


//vector<char> to unsigned char*
char* buffer0;
buffer0 = & charBuffer; //????? here is a lot of questions

And the answer to all of them is "no" ;-)
If you want the address of the first element, you can do:

buffer0 = &charBuffer[0];

or

buffer0 = &charBuffer.front();

or

buffer 0 = &*charBuffer.begin();
//unsigned char* to vector<int>
// I am not quite sure the following is right or not
for(unsigned int i = 0; i<charBuffer.size(); i++){
buffer4.push_back( ((int) *buffer0)+256 );

Why "+256"? That will put your resulting values in the range 256..511. Was
that intended?
buffer0++;
}
[/my bad code]

std::copy(buffer0, buffer0 + charBuffer.size(),
std::back_inserter(buffer4));

and, if you need to add 256:

std::for_each(buffer4.begin(), buffer4.end(),
std::bind2nd(std::plus<int>(), 256));
 
R

red floyd

jiing said:
[my bad code]
char ch ;
unsigned int temp_counter =0;
vector<int> buffer, buffer4;
vector<char> charBuffer;
while(! fin.eof() ){
fin.get( ch );
int temp;
(int(ch) < 0) ? (temp = int(ch)+256) : (temp = int(ch) );
buffer.push_back(temp);
charBuffer.push_back(ch);
}

Unrelated to your original question, your read loop is bad.

istream::eof() doesn't say that the next read would fail because
we're at EOF, but rather that the LAST READ failed because we're at EOF.

Should be something along the lines of...

while (fin.get(ch))
{
// ...
}
 
O

Old Wolf

Rolf said:
temp = unsigned char(ch);

That's a syntax error on my compiler, it only allows the
expression typename(value) if typename is all one word.

I don't know if it's a compiler bug or what..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top