-6

Probably from the time I am trying to convert and wandering internet solely for the answer of this question but I could not find. I just got I can convert hexadecimal to decimal either by some serious programming or manually through math. I am looking to convert. If there is any way to do that then please share. Well I have searched and found IEEE754 which seems not to be working or I am not comprehending it. Can I do it manually through any equation, I think I heard about it? Or a neat C program which may do it.

Please help! Any help would be highly appreciated.

  • Is the hex value a string, constant ... – Ed Heal Jun 28 '15 at 06:57
  • You have a string of 16 hex digits and you want to convert it to a number? Why floating point? Is it a fixed point value? Your question is very unclear as written. – Retired Ninja Jun 28 '15 at 06:57
  • possible duplicate of [How can I convert a hexadecimal number to base 10 efficiently in C?](http://stackoverflow.com/questions/10324/how-can-i-convert-a-hexadecimal-number-to-base-10-efficiently-in-c) – Jite Jun 28 '15 at 06:57
  • Let me clear my question, I don't want conversion of hexadecimal to decimal but rather to floating points. I am trying to convert my mac address to floating so that it could do some stuff that I want it do. Leave is there any type of equation to do it. – kapil karnani Jun 28 '15 at 08:58
  • A FP MAC address? What could you do with such a repesentation that you could not do with an array of bytes, (that, I could understand). – Martin James Jun 28 '15 at 10:22
  • There is something I want to do while don't want to declare so please if you have a method then share! – kapil karnani Jun 29 '15 at 06:56

1 Answers1

0

You need to study the IEEE floating point spec.

This would be quite straightforward in Java. You have handy methods like Float.floatToRawIntBits(float x) and Float.intBitsToFloat(int x) You might be able to do it with a union.

In C its a bit more hacky. You can abuse a union. Unions in C reuse the same memory for two different variables. A union like

union DoubleLong {
  long l;
  double d;
} u;

would allow you to treat the same bit of memory as either a long u.i or a double u.f. There are both 8 byte so they take the same space. So doing u.d = M_PI; printf("%lx\n", u.l); prints the binary representation of pi 0x400921fb54442d18.

For 16 byte we need the union to have an array or two 8 byte longs.

#include <stdio.h>

union Data {
  long i[2];
  long double f;
} u;

int main(int argc, char const *argv[]) {
  // Using random IP6 address 2602:306:cecd:7130:5421:a679:6d71:a660 
  // Store in two separate 8-byte longs
  u.i[0] = 0x2602306cecd7130;
  u.i[1] = 0x5421a6796d71a660;
  // Print out in hexidecimal
  printf("%.15La %lx %lx\n", u.f,u.i[0],u.i[1]);
  // print out in decimal
  printf("%.15Le %ld %ld\n", u.f,u.i[0],u.i[1]);
  return 0;
}

One problem is 16 byte hexadecimal floating point numbers might not be defined on you system. float is typically 32 bit - 4 byte, double is 64 bit - 8 byte. There is an long double type but on my mac its only 80-bit - 10 byte. It might be simpler to convert to two double precision numbers. So on my system only the last 4 hexadecimal digits of the second number are significant.

Not all hexadecimal numbers correspond to valid floating point numbers, a lot of values will correspond to NaN's. If the higher bits are 7FFF or FFFF (or 7FF, FFF for double) that will either give infinity of NaN.

Salix alba
  • 7,536
  • 2
  • 32
  • 38