Help with Function To Change Decimal to Binary.
Hi, It''s me again ~~SURG~~ I have been working on Coomputer Trivia Game as some of you may know. Someone named Darious King gave me a couple good ideas, thanx Darious, but now i need help with them. I need to create a function that changes random decimals into binary for the question, that way everytime the question comes up, it has changed to be a different question.
Hopefully you all understand that,
~~SURG~~
LTM, LAUGHING TO MYSELF, LOL, IS A LIE
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Hi
first you need a function to extract single bit values from a number.
You can get the value of bit n in the number c with
( c >> n ) & 1
then you just loop trough every bit in your number, get its value and assign it to a string.
If you only want numbers up to one byte use
If you want bigger numbers just change the 8 to the number of bits in your variable
you call the function like this:
Runicsoft -- home of my open source Function Parser and more
first you need a function to extract single bit values from a number.
You can get the value of bit n in the number c with
( c >> n ) & 1
then you just loop trough every bit in your number, get its value and assign it to a string.
If you only want numbers up to one byte use
void getbin (char* bin, char c){ for ( int i = 1; i <= 8; i++ ) bin[8-i] = ''0'' + (char)getbit ( c, i );}
If you want bigger numbers just change the 8 to the number of bits in your variable
you call the function like this:
char str[numbits+1]; // numbits = 8 in the examplememset ( str, 0, numbits+1 );getbin ( str, your_random_number );
Runicsoft -- home of my open source Function Parser and more
Thanx, for your help ill try that.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
September 23, 2002 07:17 PM
are you trying to convert from base 10 to base 2?
I made a simple algorithm that can do that.
I made a simple algorithm that can do that.
Take note that a base 2 number has to be represented as a string. The example that Burning_Ice provides does that.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Actually, thanx for the help, but i have found out my own way, just make a random number then use that number and convert it to binary.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement