hi, so im reading in binary numbers from a text file in c++.
So the first line of binary numbers go like this: 1100000000000000
so i use a string array to store these numbers and it is stored exactly as it is read i.e. 1100000000000000
what i would like to do is read it as it is but then store it in a integer array ; for example my
int a[1] would be , a[1] = {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} and then a[2] would be my second line from my file how do i implement this?
i hope you guys get what im trying to say. [english isnt my primary language lol!]
thanks alot!
i am not very familiar with that considering im only a beginner, could you explain to me how that works? i want to be able to work with those numbers later, will bitsets allow me to do that?
thanks for your reply btw!
Well okay then, let's not use std::bitset and instead do what you say. We store the data in an int array.
You read the binary numbers not into a
string array
, but into a string:
1 2
std::string binaries;
// Now you load the data from a file, into this string, it contains 1 line.
Now you have a string containing a sequence of 0's and 1's, and you want them to be part of an int array.
1 2 3 4 5 6 7
int *ptr = newint[binaries.size()] // A dynamic array, the size of the loaded string.
for (int i = 0; i < binaries.size(); ++i) // Every item is copied to the int array.
{
ptr[i] = ((binaries[i] == '1') ? 1:0); // if the char is '1', then int 1 will be put into ptr[i], else 0 will be put into that element.
}
// Now you can access each element like so:
int num = ptr[3];
Remember to call delete [] ptr; after you're done using ptr..