# How to represent a fraction in HEX?

Discussion in 'C++' started by Steve, Apr 15, 2004.

1. ### SteveGuest

I am having trouble finding the answer to this question, I'm thinking
the solution must be blindingly obvious, so therefore of course I
cannot see it.

I wish to take a fraction in DEC say .4 and convert it to a HEX value.
I also need to find a way to determine if a number is a fraction in
HEX and convert it back to DEC.

(This would be from a disassembly, and we are learning this for
troubleshooting purposes)

My instructor showed us this, but he has got to be the most boring
person on earth and to be honest I think I dozed off for a second. I
really want to be a proffesional computer programmer, and nothing
and I have 3 days to get this done. There is a list of 50 numbers in
both HEX and DEC. I won't list them here cuz I want to solve the
problems myself, I just happen to need to be pointed in the right
direction for solving them, a formula or something would be nice.

BTW I have tried the calc.exe HEX function, and when I select .4 then
switch to HEX I get 0, which I know cannot be the right answer (does
explain alot of problems with windows though

I know that the problem is related to disassembly, but the course is
C++ so I'm really hoping to not got flamed for an irrelevant post.

Steve, Apr 15, 2004

2. ### Allan BruceGuest

"Steve" <> wrote in message
news:...
> I am having trouble finding the answer to this question, I'm thinking
> the solution must be blindingly obvious, so therefore of course I
> cannot see it.
>
> I wish to take a fraction in DEC say .4 and convert it to a HEX value.
> I also need to find a way to determine if a number is a fraction in
> HEX and convert it back to DEC.
>
> (This would be from a disassembly, and we are learning this for
> troubleshooting purposes)
>
> My instructor showed us this, but he has got to be the most boring
> person on earth and to be honest I think I dozed off for a second. I
> really want to be a proffesional computer programmer, and nothing
> and I have 3 days to get this done. There is a list of 50 numbers in
> both HEX and DEC. I won't list them here cuz I want to solve the
> problems myself, I just happen to need to be pointed in the right
> direction for solving them, a formula or something would be nice.
>
> BTW I have tried the calc.exe HEX function, and when I select .4 then
> switch to HEX I get 0, which I know cannot be the right answer (does
> explain alot of problems with windows though
>
> I know that the problem is related to disassembly, but the course is
> C++ so I'm really hoping to not got flamed for an irrelevant post.
>
> only reply here... Thank you

This depends on which representation you use for storing numbers, for
example floating point or fixed point. Then if you choose floating point,
there are a number of ways of representing them. Now, the standard does not
say which of these have to be used, otherwise people would have to designt
hardware around the language!
The reason for calc not showing you the answer is because hex is almost
always used for displaying integers, although there is no reason it can't be
used for anything else as it is just a short-hand representation for binary.
So the short answer is, you will have to program your own.
Allan

Allan Bruce, Apr 15, 2004

3. ### Till CruegerGuest

Re: OT How to represent a fraction in HEX?

On Wed, 14 Apr 2004 22:54:15 +0000, Steve wrote:

> I am having trouble finding the answer to this question, I'm thinking
> the solution must be blindingly obvious, so therefore of course I cannot
> see it.
>
> I wish to take a fraction in DEC say .4 and convert it to a HEX value. I
> also need to find a way to determine if a number is a fraction in HEX
> and convert it back to DEC.

[snip]

> I won't list them here cuz I want to solve the problems myself, I just
> happen to need to be pointed in the right direction for solving them, a
> formula or something would be nice.

There is a simple algorithm you can use for conversion of fractions. I can
give you the algorithm, so you can implement ist for yourself.

100 multiply the number by the base you are converting to
200 take the number greater than one, and use it as the
next digit in your converted number
300 discard anything greater than one
400 repeat until satisfied or left with zero

Example:
..4 * 16 = 6.4 (result 0.6)
..4 * 16 = 6.4 (result 0.66)
....

Example 2:
0.21875 * 16 = 3.5 (result 0.3)
0.5 * 16 = 8 (result 0.38)
done

When you have figured out how fractions work, it should be fairly easy to
see how this algorithm works.
Till

--
Please add "Salt and Peper" to the subject line to bypass my spam filter

Till Crueger, Apr 15, 2004
4. ### David HarmonGuest

On 14 Apr 2004 22:54:15 -0700 in comp.lang.c++,
(Steve) wrote,
>BTW I have tried the calc.exe HEX function, and when I select .4 then
>switch to HEX I get 0, which I know cannot be the right answer (does
>explain alot of problems with windows though

A lot of people apparently think there is something about base 16 that
makes it apply only to integers, unlike base 10. At least you are ahead
of them. OK, I will take pity on your plight, even though I failed to
see anything whatsoever regarding C++ in your question. Don't let it
happen again.

In a decimal number 0.wxyz the 'w' is the number of tenths, 'x' is the
number of hundredths, 'y' is the number of thousandths, and z is the
number of ten thousandths.

If it was a hexadecimal, 'w' is the number of sixteenths, 'x' is the
number of 1/(16*16) i.e 256ths, 'y' is the number of 1/(16*16*16) or
4096ths, and 'z' the number of 1/(16^4) or 65536ths.

So, how many 16ths is 0.40 decimal, and what is the remainder?
How many 256ths is the remainder? And so on.

You might be likely to see hex fractions related to C++ code if you dump
float or double numbers in hex.

David Harmon, Apr 15, 2004
5. ### Allan BruceGuest

>
> A lot of people apparently think there is something about base 16 that
> makes it apply only to integers, unlike base 10. At least you are ahead
> of them. OK, I will take pity on your plight, even though I failed to
> see anything whatsoever regarding C++ in your question. Don't let it
> happen again.
>
> In a decimal number 0.wxyz the 'w' is the number of tenths, 'x' is the
> number of hundredths, 'y' is the number of thousandths, and z is the
> number of ten thousandths.
>
> If it was a hexadecimal, 'w' is the number of sixteenths, 'x' is the
> number of 1/(16*16) i.e 256ths, 'y' is the number of 1/(16*16*16) or
> 4096ths, and 'z' the number of 1/(16^4) or 65536ths.
>
> So, how many 16ths is 0.40 decimal, and what is the remainder?
> How many 256ths is the remainder? And so on.
>
> You might be likely to see hex fractions related to C++ code if you dump
> float or double numbers in hex.
>

I agree, however it depends on what type of number is being represented.
e.g. 0xFF can be:

1) 255 if we are using an obvious represntation for an unsigned 8-bit type

2) -127 if we are using an obvious representation for a signed 8-bit type

3) 127.9375 if we are using a fixed point 8-bit type with 7bit before the
'.' and 1 bit after

My point is, there is not only one way of doing this, things get even more
difficult if floating points are considered.
Allan

Allan Bruce, Apr 15, 2004
6. ### Allan BruceGuest

"David Harmon" <> wrote in message
news:...
> On 14 Apr 2004 22:54:15 -0700 in comp.lang.c++,
> (Steve) wrote,
> >BTW I have tried the calc.exe HEX function, and when I select .4 then
> >switch to HEX I get 0, which I know cannot be the right answer (does
> >explain alot of problems with windows though

>
> A lot of people apparently think there is something about base 16 that
> makes it apply only to integers, unlike base 10. At least you are ahead
> of them. OK, I will take pity on your plight, even though I failed to
> see anything whatsoever regarding C++ in your question. Don't let it
> happen again.
>
> In a decimal number 0.wxyz the 'w' is the number of tenths, 'x' is the
> number of hundredths, 'y' is the number of thousandths, and z is the
> number of ten thousandths.
>
> If it was a hexadecimal, 'w' is the number of sixteenths, 'x' is the
> number of 1/(16*16) i.e 256ths, 'y' is the number of 1/(16*16*16) or
> 4096ths, and 'z' the number of 1/(16^4) or 65536ths.
>
> So, how many 16ths is 0.40 decimal, and what is the remainder?
> How many 256ths is the remainder? And so on.
>
> You might be likely to see hex fractions related to C++ code if you dump
> float or double numbers in hex.
>

I agree, however it depends on what type of number is being represented.
e.g. 0xFF can be:

1) 255 if we are using an obvious represntation for an unsigned 8-bit type

2) -127 if we are using an obvious representation for a signed 8-bit type

3) 127.9375 if we are using a fixed point 8-bit type with 7bit before the
'.' and 1 bit after

My point is, there is not only one way of doing this, things get even more
difficult if floating points are considered.
This, unfortunately, is not part of the C++ language, however if you defined
your own representation or decided on one to use, then tried to implement it
HTH
Allan

Allan Bruce, Apr 15, 2004
7. ### SteveGuest

"Allan Bruce" <> wrote in message news:<c5lurt\$l7g\$2surf.net>...
> "David Harmon" <> wrote in message
> news:...
> > On 14 Apr 2004 22:54:15 -0700 in comp.lang.c++,
> > (Steve) wrote,
> > >BTW I have tried the calc.exe HEX function, and when I select .4 then
> > >switch to HEX I get 0, which I know cannot be the right answer (does
> > >explain alot of problems with windows though

> >
> > A lot of people apparently think there is something about base 16 that
> > makes it apply only to integers, unlike base 10. At least you are ahead
> > of them. OK, I will take pity on your plight, even though I failed to
> > see anything whatsoever regarding C++ in your question. Don't let it
> > happen again.
> >
> > In a decimal number 0.wxyz the 'w' is the number of tenths, 'x' is the
> > number of hundredths, 'y' is the number of thousandths, and z is the
> > number of ten thousandths.
> >
> > If it was a hexadecimal, 'w' is the number of sixteenths, 'x' is the
> > number of 1/(16*16) i.e 256ths, 'y' is the number of 1/(16*16*16) or
> > 4096ths, and 'z' the number of 1/(16^4) or 65536ths.
> >
> > So, how many 16ths is 0.40 decimal, and what is the remainder?
> > How many 256ths is the remainder? And so on.
> >
> > You might be likely to see hex fractions related to C++ code if you dump
> > float or double numbers in hex.
> >

>
> I agree, however it depends on what type of number is being represented.
> e.g. 0xFF can be:
>
> 1) 255 if we are using an obvious represntation for an unsigned 8-bit type
>
> 2) -127 if we are using an obvious representation for a signed 8-bit type
>
> 3) 127.9375 if we are using a fixed point 8-bit type with 7bit before the
> '.' and 1 bit after
>
> My point is, there is not only one way of doing this, things get even more
> difficult if floating points are considered.
> This, unfortunately, is not part of the C++ language, however if you defined
> your own representation or decided on one to use, then tried to implement it
> HTH
> Allan

I guess to simplfy the question

float MyVar = 0.4;

What should I be looking for in disassembly.
Everytime I try to calculate 0.4 DEC to HEX I keep coming up with 0x0
which HAS to be wrong. So can someone please show me what that would
look like in a memory map and how to convert it into a hex value? I
really don't know where to look, nor where to go.

I don't need a program here, just a formula would be nice.

Steve, Apr 16, 2004
8. ### PaulGuest

Re: [OT}How to represent a fraction in HEX?

Steve wrote:
>
> I don't need a program here, just a formula would be nice.

Here is the way I was taught to generate base 16 floating point numbers
(using your example to 6 places).

0.4 x 2 = 0.8
HexDigit = Int(0.8) = 0 *
Next multiplier = Frac(0.8) = 0.8

0.8 x 2 = 1.6
HexDigit = Int(1.6) = 1 *
Next multiplier = Frac(1.6) = 0.6

0.6 x 2 = 1.2
HexDigit = Int(1.2) = 1 *
Next multiplier = Frac(1.2) = 0.2

0.2 x 2 = 0.4
HexDigit = Int(0.4) = 0 *
Next multiplier = Frac(0.4) = 0.4

0.4 x 2 = 0.8
HexDigit = Int(0.8) = 0 *
Next multiplier = Frac(0.8) = 0.8

0.8 x 2 = 1.6
HexDigit = Int(1.6) = 1 *
Next multiplier = Frac(1.6) = 0.6

Now gather up the hexdigits that you generated in order (the ones with
the * next to them. This will be your fractional hex answer:

0.4 (decimal) = 0.011001 (hex)

Hopefully you see the pattern. I don't have time to write the general
formula, but hopefully you understand.

Paul

Paul, Apr 16, 2004
9. ### PaulGuest

Re: [OT}How to represent a fraction in HEX?

Paul wrote:

> Hopefully you see the pattern. I don't have time to write the general
> formula, but hopefully you understand.
>
> Paul

oops, sorry. Forgot that this is only the binary. The hex is one more
step, which I will leave to you for an exercise.

Paul McKenzie

Paul, Apr 16, 2004
10. ### PaulGuest

Re: [OT}How to represent a fraction in HEX?

Paul wrote:

> Steve wrote:
>
>>
>> I don't need a program here, just a formula would be nice.

>
>
> Here is the way I was taught to generate base 16 floating point numbers
> (using your example to 6 places).
>
> 0.4 x 2 = 0.8
> HexDigit = Int(0.8) = 0 *
> Next multiplier = Frac(0.8) = 0.8
>
> 0.8 x 2 = 1.6
> HexDigit = Int(1.6) = 1 *
> Next multiplier = Frac(1.6) = 0.6
>
>
> 0.6 x 2 = 1.2
> HexDigit = Int(1.2) = 1 *
> Next multiplier = Frac(1.2) = 0.2
>
> 0.2 x 2 = 0.4
> HexDigit = Int(0.4) = 0 *
> Next multiplier = Frac(0.4) = 0.4
>
> 0.4 x 2 = 0.8
> HexDigit = Int(0.8) = 0 *
> Next multiplier = Frac(0.8) = 0.8
>
> 0.8 x 2 = 1.6
> HexDigit = Int(1.6) = 1 *
> Next multiplier = Frac(1.6) = 0.6
>
> Now gather up the hexdigits that you generated in order (the ones with
> the * next to them. This will be your fractional hex answer:
>
> 0.4 (decimal) = 0.011001 (hex)
>
> Hopefully you see the pattern. I don't have time to write the general
> formula, but hopefully you understand.
>
> Paul

Paul, Apr 16, 2004
11. ### Allan BruceGuest

>
> I guess to simplfy the question
>
> float MyVar = 0.4;
>
> What should I be looking for in disassembly.
> Everytime I try to calculate 0.4 DEC to HEX I keep coming up with 0x0
> which HAS to be wrong. So can someone please show me what that would
> look like in a memory map and how to convert it into a hex value? I
> really don't know where to look, nor where to go.
>
> I don't need a program here, just a formula would be nice.

if you are going to be persistent!

for (i=0; notFinished; i++)
{
F *= base; (let suppose F is 0.4
digit = integer part of F (F is now 6.4 so the integer part is 6)
F-= digit (so F now = 0.4)
}

so you can see for your example 0.4 converted to base 16 is 0.6666666666...

Now to prove hex is a short hand represenation for binary:
4bits are grouped together to form a hex digit, i.e.

0000 = 0x0
0001 =0x1
....
1110 = 0xE
1111 = 0xF

so, when we do your example we need to convert the decimal fraction to
binary first.

Converting to binary 0.4 = 0.011001100110011....
so the hex representation of this is broken up like
0. | 0110 | 0110 | 0110 | 0110 | (assuming the '.' lies on a 4-bit
boundary)

0110B = 0x6, therefore 0.4D = 0x6666666...

If your number representation means that the '.' doesnt lie on the 4bit
boundary (which is very unlikely) then the answer may be different

Hope that helps
Allan

Allan Bruce, Apr 16, 2004
12. ### Dave MooreGuest

(Steve) wrote in message news:<>...
> I am having trouble finding the answer to this question, I'm thinking
> the solution must be blindingly obvious, so therefore of course I
> cannot see it.
>
> I wish to take a fraction in DEC say .4 and convert it to a HEX value.
> I also need to find a way to determine if a number is a fraction in
> HEX and convert it back to DEC.

The examples you give are *not* fractions but floating point numbers,
which are more general. *Any* value can be represented as a floating
point number (up to the precision of the machine), whereas "fraction"
implies a rational number (i.e. three-fourths). As an academic aside,
the inherent discretization of computers (which produces the
precision-limits) effectively means that *all* numbers, even
irrationals or transcendentals end up represented as fractions
(n/MAX_REPRESENTABLE_VALUE) in some sense, although the details are
fairly complicated.

*snip*

> There is a list of 50 numbers in
> both HEX and DEC. I won't list them here cuz I want to solve the
> problems myself, I just happen to need to be pointed in the right
> direction for solving them, a formula or something would be nice.
>

You really haven't given enough information here. If you are talking
number, then it is simple. Just take the binary representation of the
number (watch out for big/little-endian issues), and convert that into
hexadecimal notation. Of course such a representation is nonsensical
taken out of context. You need to know the byte-size (float or
double) and internal floating-point representation (radix and
exponent) to be able to translate back and forth. However that is all
pretty straightforward bookkeeping, and your prof should have narrowed

If you are talking about mathematically representing non-integral
you by another poster, so I won't repeat it here. However, I will
note that the example you gave (0.4) has no exact representation as a
hexadecimal fraction ... it is a repeating, non-terminating
hexidecimal number (0.66666...). Here are a couple more examples

Decimal Hex
0.4 0.6666
0.25 0.4
0.33333 0.5555 (Cool huh?)
0.01 0.028f
1.41421 1.6a0b (square root of 2)
31.62278 1f.9f9e (square root of 1000)
2.71828 2.b7e0 (base of natural logarithms - e)

and so on ... I did these on a calculator in about 5 minutes. Without
being too specific, the method is to multiply the decimal number by
the denominator of the least significant hexadecimal place (65336 for
4-digit precision). Then it should be immediately obvious how to
extract the full hexadecimal representation from the result.

HTH, and good luck

Dave Moore, Apr 16, 2004
13. ### Nick KeighleyGuest

(Steve) wrote in message news:<>...
> "Allan Bruce" <> wrote in message
> news:<c5lurt\$l7g\$2surf.net>...
> > "David Harmon" <> wrote in message
> > news:...
> > > On 14 Apr 2004 22:54:15 -0700 in comp.lang.c++,
> > > (Steve) wrote,

<snip>

> > > In a decimal number 0.wxyz the 'w' is the number of tenths, 'x' is the
> > > number of hundredths, 'y' is the number of thousandths, and z is the
> > > number of ten thousandths.
> > >
> > > If it was a hexadecimal, 'w' is the number of sixteenths, 'x' is the
> > > number of 1/(16*16) i.e 256ths, 'y' is the number of 1/(16*16*16) or
> > > 4096ths, and 'z' the number of 1/(16^4) or 65536ths.
> > >
> > > So, how many 16ths is 0.40 decimal, and what is the remainder?
> > > How many 256ths is the remainder? And so on.

you pretty well have an algorithm here...

<snip>

> I guess to simplfy the question
>
> float MyVar = 0.4;
>
> What should I be looking for in disassembly.

what?

> Everytime I try to calculate 0.4 DEC to HEX I keep coming up with 0x0
> which HAS to be wrong. So can someone please show me what that would
> look like in a memory map and how to convert it into a hex value? I
> really don't know where to look, nor where to go.
>
> I don't need a program here, just a formula would be nice.

try long divison with paper and pencil- but in base 16.

4/10 (dec) = 4/A (hex)

------------------------
A )4.000

A won't go into 4. That is 4/A = 0 rem 4.

0.
------------------------
A )4.000

Move to the next column.
A will go into 40. 40/A = 6 rem 4

0.6
------------------------
A )4.000
3.C
---
4

Same again for the next column.

0.66
------------------------
A )4.000
3.C
---
.40
.3C
--
4

We have a repeating fraction 0.666...

We can even double check this:-

n = 0.666...
10n = 6.666... (that's 10 hex remember..)

now subtract the first from the second:-
Fn = 6.000
n = 6/F (hex) = 6/15 (dec) = 0.4 (dec)

Try that with a few examples. Then code it in C++ and try and get the same
result. Note the % operator gives the remainder...

0x40 % 0xA == 0x4

--
Nick Keighley

Nick Keighley, Apr 16, 2004
14. ### SteveGuest

The reason for calling it a fraction instead of a floating point
number, is because the original question is worded like this...

Take the following fractions, assign them to variables and show what
they would look like if you had to find them under disassembly

4/10
5/8
1 1/16

etc and so forth.

Well I have ALWAYS done fractional math when programming, just as
simple division.

4/10 = .4 in HEX it seems to me that 4/10 would still be .4 or 0.4

Placing 4/10 in the HEX function of calc.exe shows me 0, so I knew
that wasn't right.

Finally someone in another NG turned me on to a program specifically
designed for this solving these types of questions.

The program has four seperate areas for entering numbers, each has
it's own label.
DEC HEX OCT BIN

Then it has a pull down for each of C's standard datatypes. Float is
one of the options.

After selecting a datatype, entering anything in any of the boxes
causes an automatic update to all the boxes.

Ergo automatic conversion from .4 DEC to HEX is 3ECCCCCD which tells
me it was rounding my hex value after a certain length. Interestingly
enough it also has a label called "Rev hexa chain" which spit out "CD
CC CC 3E" which is actually the format I was looking for.

So now a new question, what was I missing before, how did this program
convert .4 into "CD CC CC 3E", and what the heck could "Rev hexa
chain" mean.

My first instinct is to say it means reverse hexadecimal chain, but
what would be the purpose of a reverse hexadecimal chain? Also it
doesn't quite look reversed unless the reverse is applied to every 2
digits. I think I've heard of that being called XOR but, honestly ATM
my books are not at hand and I am still quite new at this.

Also in the format I am looking for wouldn't "3E CC CC CD" actually be

If either was the correct answer, how did the program calculate it?
This is the piece I am missing, why did it truncate where it did, and
for that matter how did it come by the answer of 3ECCCCCD. Is there
something special about 4 sets of 2 digits?

Sorry for so many questions, but it seems the more I think I
understand, the more questions seem to pop-up.

And again for the guy asking why this is in a C++ newsgroup, it's
because I'm in a C++ programming class, and this was part of some
homework that needed to be done. For part of my finals I will need to
be able to solve this on paper and pencil, and will also have to