Decimal to Binary String competition
Hi All!
I've not posted on here before, but I thought I'd start somewhere with a small competition/point of interest!
Anyway, i've always had a nagging for producing an efficient method of creating a binary representation of a string. It's a deceptively simple problem - perhaps because it relates so closely with the raw bits of the system, but more often that not, the solution is a boring for() loop!
So the challenge (if you choose to accept it) is to come up with an interesting, efficient, fast way of producing a binary representation of a string in either C/C++.
Mine is,
Code:
std::string to_bin(const int *data){
/* Were going to assume 32-bit ints */
unsigned int mask = 0x00000001;
std::string ret_string;
while( !(mask & 0x00000000) ){
if(mask & *(data))
ret_string = '1' + ret_string;
else
ret_string = '0' + ret_string;
mask <<= 1;
}
return ret_string;
}
Happy Coding!
D