• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Converting data to binary

This topic is 5828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m kinda new to this stuff so please bear with me. I wish to write a function in c or c++ that takes any data (be it text, or numbers) and convert it to base 2 form. I want the function to return the newly-converted information in the form of an array so I can then perform operations on it. There is probably an easy solution, but I just can see it. Thanks a lot for any help you can provide :-)

##### Share on other sites
Oh, I forgot to mention...

I need to know this for my computer science project...due in a week lol

I''m still in high school, I don''t know why my teacher would ask for something like this but such is life.

If you can help me, you are my GOD!

##### Share on other sites
I don''t know whether thisis the best solution, but

each character in a string is simply a number..
ie .. a = 30
or something
so all you need do is convert the number 30 to binary
which is just an excercise in base 10 to base 2. no big deal. google that one.
if you need to get the ascii value for a character simply cast it as in integer. c will handle the rest.
Hope that helps.
Later andy

##### Share on other sites
using bit operations (&, |, ...) operates per bit. i don''t know what you are going to do with it once you get the binary data, but you probably can just use the bitwise operations. i''m not totally sure what the others are, i''ve never used them. i think its ^ and one other.

##### Share on other sites
anyone know a good way of converting base 10 to base 2?

##### Share on other sites
I'm not going to do your homework for you (if you've been set the question, you've been taught the answer!), but I will give you a couple of big clues.

CLUE #1:

You may remember abacus style counting from basic maths at school, i.e. for a base 10 number you have units, tens , hundreds, thousands etc (...powers of ten...).

e.g. 3512 is

3 1000s
5 100s
1 10s
2 units

Now say you wanted write a display routine for base 10 numbers you'd divide by each of the above sizes to find the digit for that size, and then repeat with the remainder of the result:

display( number / 1000 ); // displays "3"
display( (number % 1000) / 100 ); // displays "5"
...etc...

CLUE #2:

"powers of two ", 128, 64, 32, 16, 8, 4, 2, 1...

--
Simon O'Connor
Creative Asylum Ltd
www.creative-asylum.com

[edited by - s1ca on February 8, 2003 10:23:49 PM]

##### Share on other sites
The actual assignment is an ISU(independent study unit)...he didn''t teach it to us, he wants us to find the answer for ourselves. The problem here is that I need to somehow show how to compress and decompress a string of data. I don''t know of any real easy ways, but the best I could come up with is convert everything to binary and doe RLE on the binary sequence. This way I can convert my data to binary, which at that point would be easilly reversible, and then compress it using RLE, after which I can reverse the RLE and then convert the result to base 10 and then convert to a character string....wow, run-on sentence and a half.

##### Share on other sites
Check out huffman trees

##### Share on other sites
quote:
Original post by max621
Check out huffman trees

Or even just plain LZ77 or even simple which is dead easy to implement once you know the algorithm.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

##### Share on other sites
Problem: I don''t know anything about that stuff lol

I''ve HEARD of them but never seen them. My school is on a crazy budget and hires crack heads for teachers(exaggerating...)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×