Question:
Trying to Convert String to Binary array?
Sean
2011-04-29 14:29:07 UTC
I am reading a file in as a string then i want to save each characters binary value in an array..

The only thing i have successfuly done is..
for( int i=0; i < str.length(); i++)
cout<(str)<
but i want to save that in a byte array not output to screen.
BYTE [ ] has not been working for me (do i need a #include?)
so i believe i should use 'unsigned char [] byte'

Thanks
Three answers:
2011-04-29 14:50:41 UTC
Firstly cout is used to output characters on the screen therefore I don't know why you are using it if you don't want to output them two the screen.



Secondly a string is actually an array of BYTE's, unsigned chars is the same thing just the C version of BYTE, therefore you already have the binary values but I don't know if you have access to this array with what you have used to read the file.



Thirdly reading an entire file as a string is DEADLY. For small files this is OK but for larger files this can quickly eat up all your memory. Rather read it line by line or character by character.



My suggestion for what you would do is get the file size and create an array of that size, make a loop that reads the file character by character and store the corresponding character in its corresponding index in the array you created but like I said it is not a good idea to read an entire file in an array. Rather read it in chunks (lines) and deal with that data separately than read the whole file and deal with all the data.
Cubbi
2011-04-29 22:19:45 UTC
arrays are not the best containers for this purpose. You're using C++, the correct data structure is std::vector or std::vector where uint8_t is not supported.



The elements of a string are chars, which means you can simply assign:



vector array(str.length());

for( size_t i=0; i < str.length(); i++)

     array[i] = str[i];



or push-back



vector array;

for( size_t i=0; i < str.length(); i++)

     array.push_back(str[i]);



or just create the vector directly:



vector array(str.begin(), str.end());



If you must use the array, the syntax is similar to the first loop



unsigned char* array = new unsigned char[str.length()];

for( size_t i=0; i < str.length(); i++)

     array[i] = str[i];

delete[] array;



or, with a copy



unsigned char* array = new unsigned char[str.length())];

copy(str.begin(), str.end(), array);

delete[] array;



However, it is unclear

1) why do you have your data in a string if it is binary data. Read them into vector right from the file

and

2) why do you need an array of bytes if you already have the string, which can be accessed like an array. Each element of the string is implicitly convertible to unsigned char / uint8_t.
Dany
2011-04-29 22:43:21 UTC
BYTE is under the windows header so you would have to include it but you don't really need it. You could use a bitset to store it also is just like any other type...



std::string str = "Hello world";

std::vector< std::bitset<8> > b;

std::string::iterator i = str.begin();

for(;i != str.end();i++)

{

b.push_back( *i );

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...