# Conversion of binary string

Discussion in 'Mac Programming' started by dawnraid, Mar 21, 2010.

1. ### dawnraid macrumors regular

Joined:
Nov 9, 2007
#1
Hi,
I have been given a problem as part of an assignment for college,
I cant contact any of the tutors at the moment as we are on holiday.
So I was just wondering If I could get some help on here.
The Question is
Now I know you guys dont like doing peoples homework for them. so dont post the answers. I just need your help understanding why there is a ".' in between the original binary string, does this mean the value is a fraction in which case its decimal representation is 15.3 . If so how would I show this in octal/hex notation would I put a "." in the answer. Some explanation would be a great help.

Thanks.

2. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#2
The binary point means there is a fractional component. It is not 3. It is 3/16. In decimal this is .1875. Each place after the binary/hexadecimal/octal point has a value of z*1/(x^n) where n is the number of positions to the right of the point and x is the base you're working in, and z is the quantity at that position.

1111.0011 binary
F.3 hex
17.14 octal
15.1875 decimal

binary to hex and binary to octal is super easy. Every 3 bits is one octet, every 4 bits is one hexit. Decimal is the real pain in the ass.

-Lee

3. ### Sydde macrumors 68020

Joined:
Aug 17, 2009
#3
Now that is a bit harsh. In a high-level language, it should be as simple as a series of shift-and-add operations (switching the shift/add order at the fraction point), then relying on printf to format the result. In AL, I seem to recall doing it (on an int) with modulus, though I never got to futzing with fractions.

4. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#4
i meant to do it in my head. in code it's all simple.

-Lee

5. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Sailing beyond the sunset
#5
It's basic arithmetic.

3/16 = .1875

.1875 = 1875 / 10000

3 * 10000 / 16 = 1875

6. ### Sydde macrumors 68020

Joined:
Aug 17, 2009
#6
In front of you, that is fine. I was speaking of many years ago when I wrote a simple integer calculator on a 6502 Commodore in AL. Converting binary integers into their decimal string was a simple matter of using modulus from right to left - creating a decimal representation of a binary fraction is a little more challenging at that level (meaning, essentially with no system tools unless you want to dive into calling the BASIC routines in ROM).

7. ### dawnraid thread starter macrumors regular

Joined:
Nov 9, 2007
#7
Hi im curious as to how you got the 17.14 as the octal?
I split it like this 001| 111 | 000 | 011.

Which results in 17.03
Im new to all of this so my way probably isnt right.

8. ### dawnraid thread starter macrumors regular

Joined:
Nov 9, 2007
#8
Edit: My bad just figured it out pad to 00 to the front and back and split.
Cheers guys, you have been a big help.