Jump to content
  • Advertisement

Archived

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

h0-0t

Converting data to binary

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
quote:
Original post by max621
Check out huffman trees
They can help you compress data by a lot.


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 this post


Link to post
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...)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!