Hey,
It has been a while since I've programmed in C++, but some school thing just turned up and it came in handy.
I've created a function to make a binary code from any number under 255, (ASCII -> Bin) but it seems a bit long and simple, I was wondering if there was a easier more neat way of doing it. But at the same time it shouldn't become a string, they should stay intergers, and if possible in an array.
This works for me:
Btw, is it possible to return an array? Or should I go about doing it some other way? And can I take an array and give it to a function as a variable to use?Code:void ascii_bin(int ascii_v) { int binary[7]; if(ascii_v<128) { binary[0] = 0; }else{ binary[0] = 1; ascii_v -= 128; } if(ascii_v<64) { binary[1] = 0; }else{ binary[1] = 1; ascii_v -= 64; } if(ascii_v<32) { binary[2] = 0; }else{ binary[2] = 1; ascii_v -= 32; } if(ascii_v<16) { binary[3] = 0; }else{ binary[3] = 1; ascii_v -= 16; } if(ascii_v<8) { binary[4] = 0; }else{ binary[4] = 1; ascii_v -= 8; } if(ascii_v<4) { binary[5] = 0; }else{ binary[5] = 1; ascii_v -= 4; } if(ascii_v<2) { binary[6] = 0; }else{ binary[6] = 1; ascii_v -= 2; } if(ascii_v<1) { binary[7] = 0; }else{ binary[7] = 1; ascii_v -= 1; } return binary; }
Thanks in advanced
Devouring One