ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.

Exclusive for LQ members, get up to 45% off per month. Click here for more info.

im trieing to convert 101011.01 to octal dec and hex :
so i took 101011.01 and to dec :
43 + 0 * 2^{-1} + 1* 2^{-2}= 43.25
101011.01 to oct.
53+ 0*2^-1 + 2^{-2} = 53.25
and to hex :
2b.25

but it was wrong (i try to go back by same option and i get other nombers)

I don't understand what your are trying to do and what a "binary fraction" is, as it doesn't make sense.
Do yo mean floating point numbers?

Oh, a binary fraction does make sense, of course! -It is the same as a decimal fraction, but with the base of 2 instead of 10.

jabka: Your algorithm for converting is not the best one, but basically correct. It even (and only) works for base 10. For any other base the 2^{-2} is not 0.25. Actually it is a fourth relative to the base. Means: for oct it would be 0.2 and for hex 0.4. I think you will be able to apply this to your problem...

Oh, a binary fraction does make sense, of course! -It is the same as a decimal fraction, but with the base of 2 instead of 10

I stand corrected I see what you are talking about is fixed point binary fractions( a relic from the past); which is not the same as a decimal fraction.
What does confuse me is where your input comes from, but that's another matter.

yeh im with you dmail. that just doesnt make any sense.

Why not? A fractional number is a number like any other and can be converted from base to base like any other. Have a look here and search for "fraction" (for example): http://en.wikipedia.org/wiki/Binary_numeral_system

Why not? A fractional number is a number like any other and can be converted from base to base like any other. Have a look here and search for "fraction" (for example): http://en.wikipedia.org/wiki/Binary_numeral_system

yeh.. i had started some searching right after i posted because i had never saw a fraction like that. i was not saying you were not correct, i was just saying that it didnt make sense to me (aka im ignorant to it ).. thanks for the link.

The way I do it, you take the binary number and ignore the decimal point (or is it called a binary point?) and convert it to decimal.
10101101=173
Then you divide by 4 to get 43.25
because there is 2 digits to the right of the point.
If there was 3 digits to the right of the point then divide by 8, and so on.

The way I do it, you take the binary number and ignore the decimal point (or is it called a binary point?) and convert it to decimal.
10101101=173
Then you divide by 4 to get 43.25
because there is 2 digits to the right of the point.
If there was 3 digits to the right of the point then divide by 8, and so on.

That method works nicely for converting to decimal, but not for octal or hex.

Since the radix of both octal and hex are powers of two, the easiest way to convert to those bases is to convert blocks of 3 binary digits for octal and of 4 binary digits for hex. Here's what i mean, using jabka's binary number 101011.01

Code:

binary: 101 011 . 010
to octal: 5 3 . 2
binary: 0010 1011 . 0100
to hex: 2 b . 4

Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE

Posts: 5,194

Rep:

"Binary fractions" make sense because you can stick with integer math instead of floating point, which is (was?) an order of magnitude fasters on processors which do not (did not?) have a FPU. Especially embedded systems.

Internally you would shift number to the left so they become real integers, but at some moment you have to go back to real world and shift back to world values. If you don't have the FP libraries either, you would do it like this.

Maybe nowadays embedded systems come with an FPU or speed is not an issue anymore... I am from the era that programs were fed into a 6800 system using a paper tape reader.

im trieing to convert 101011.01 to octal dec and hex :
so i took 101011.01 and to dec :
43 + 0 * 2^{-1} + 1* 2^{-2}= 43.25
101011.01 to oct.
53+ 0*2^-1 + 2^{-2} = 53.25
and to hex :
2b.25

but it was wrong (i try to go back by same option and i get other nombers)

You've converted the integer parts correctly, and recognized that binary 0.01 = decimal 0.25, but you need to convert that to octal and hex.

One algorithm for converting a fraction to another base is:

Code:

while (fraction != 0) and (you haven't found as many digits as you want):
fraction *= base;
digit = int(fraction)
add the digit to your result
fraction -= digit

"Binary fractions" make sense because you can stick with integer math instead of floating point, which is (was?) an order of magnitude fasters on processors which do not (did not?) have a FPU. Especially embedded systems.

Not only that, but by using binary fractions you can avoid those nasty rounding errors associated with floating point numbers. Native Cobol programs store all numbers internally as integers, the compiler keeping track of the implied decimal point for each variable. As a result, these applications never drop or add pennies here and there. (COBOL = COmmon Business-Oriented Language)

Quote:

Originally Posted by Dan04

But for converting between binary, octal, and hex, it's easiest to do it dogpatch's way.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.