Reputation: 109
Say I have an array that looks like:{'0b00011000','0b10001000'}
How do I convert each element within that array into a uint8_t
keeping its output in binary format. (e.g. '0b10001000' == 0b10001000
).
Are there functions that can over turn this, or will I have to make my own function?
p.s. I get a 'precision lost' error for casting, and when I bypass that obviously it loses its precision.
Edit: I did end up figuring out using bitsets instead of using uint8_t
.
Upvotes: 1
Views: 291
Reputation: 109
I found my own answer:
You can use 'bitsets' within cpp and cast strings/char* within them
string s = "10011001";
std::bitset<8> binaryNum(string);
binaryNum will hold value 10011001, instead of converting to dec or hex.
Upvotes: 3
Reputation: 939
You could simply do
uint8_t out = 0;
for(int i=2; i<10; i++)
if(arr[i])
out += pow(2, i-2);
Certainly not the best in terms of performance but if I understand the question it should work. But if you are worried about performance certainly you should look into why you are storing the value of a char into an array of 8 of it.
Upvotes: 1