 Double to hex...???[solved 4 C++] | Sololearn: Learn to code for FREE!

+28

# Double to hex...???[solved 4 C++]

I want to convert a double number to hexadecimal number.... x=11.6496829774349 👆this is the number.... or take any floating point number whichever you like... How to convert it ? edit:- and of course print the string value (hexadecimal). I need hexadecimal equivalent of the given floating point number....

+7

For C++ conversion, you can use this function: http://www.cplusplus.com/reference/ios/hexfloat/

+21

Seb TheS Thanks... but how this method will be applied to a floating point number... will it be different or same for floating point number... eg (1000.765)...

+18

that Ruby code is converting the numbers before dot and after dot to hex... check out this code is this valid👇 https://code.sololearn.com/c3qkS652zFYJ/?ref=app

+17

u mean this is valid 👇 https://code.sololearn.com/cq5f20Lt3mLu/?ref=app

+13

Max Timon Paßlick Base 16 math. rep

+2

Do you mean print in base 16 representation? In this case you could use this https://en.m.wikipedia.org/wiki/Positional_notation#Base_conversion Or do you mean printing the natural number that represents the double in memory in hex

+2

🌛DT🌜 That's the hex layout in memory.

+1

Base 16 mathematical representation or hex layout in memory?

+1

You can divide that into 2 problems: Before and behind the dot.

+1

The ruby code works correctly before the dot.

+1

🌛DT🌜 I'll stop writing, FranKs solution is better than mine could ever become.

+1

Hex has a logic, that can easily be made with any number like follows for 1 000: Step 1 first you have to get the biggest 16 ** X, but smaller than the number you wanted to convert to hex. 16, 256, 4 096, 16 ** 2 = 256 256 < 1000 < 4096 4096 won't be used, when number we want to convert is smaller than 4096. Then you start by floordividing 1 000 with 16 ** 2: 1 000 // 256 = 3 3 is the first number of the Hex. Then you multiplicate the result with 16 ** 2: 3 * 256 = 768 Then you subtract 1 000 with result of 3 * 16 ** 2: 1 000 - 768 = 232 Then 232 is like 1 000 from the begining, (16 ** 1 = 16) < 232, thus you floordivide 232 with 16, which is 15(E), whole hex value's first 2 numbers are 3 and 14, which is 3E, then you repeat this pattern until for 16 ** X equals 0. All steps: 1000 1000 // 16 ** 2 = 3 ----- 3 3 * 16 ** 2 = 768 1000 - 768 = 232 232 // 16 ** 1 = 14 ----- E 14 * 16 ** 1 = 224 232 - 224 = 8 --------------8 result is: 3B8

+1

🌛DT🌜 It might not be much different, you have just to split float from the dot to 2 parts, you have to set an end for the unlimited floating point number, for example 1/3 = 0.33333333333333... which would just be cut to 0.33333 and there 0.| 33333 would be easy to convert in to hex, if there are 0s in begining, you have to assing it's length before you change it to integer and when hex operators are done, you turn it back to string and give more 0s, if it lacks with some. If you don't like to cut it after decimal from any point, you should get the repeating part of the decimal, then you can add the repeating part back as many times as you want.

+1

Seb TheS log2(16) is integral so that you won't encounter infinite series when printing the hex representation of double on binary machines.

0

Before the dot: Cast your double to an integer type, then: std::stringstream s; s << std::hex << your_int_rep; auto before_dot = s.str();

0

🌛DT🌜 No idea, that's ruby.

0

Behind the dot is hard.

0

your_actual_double -= your_int_rep; maybe good start

0

If it was the math rep, it would print b.[some hex digits] Because 11 is b.

0

But after the dot, the 5 is too high. Because there's a 0 in the dec rep.