Question: I've recently been assigned to a C++ project involving information being sent between computers via UDP. When a packet arrives, I have a program which accepts the data and can display it as a raw hexadecimal string. However, I'm struggling to grasp exactly how this whole process is supposed to work. The hex string supposedly contains several fields (e.g. a 4-char array, some float\_32s, and some uint\_32s).

How do I translate the sections of this string into the correct variable types? The first value, an ASCII title, was simple enough; the first eight chars in the hex string are a hexadecimal representation of an ASCII word (0x45 hex can be translated directly to the capital letter E). But the next value, a 32-bit float, doesn't really make sense to me. What is the relation between the hex value "42 01 33 33" and the float value "32.3" (a given example)?

I'm a bit in over my head here, I feel I'm missing some essential information regarding the way number systems work.

Answer: 
All types in C have a representation (which for most types is defined by a particular implementation). Most C implementations use [IEEE 754](http://en.wikipedia.org/wiki/IEEE_754-2008) for representing the floating types (this may actually be a requirement for C and C++, but from memory it is not). The Wikipedia article explains how the floating types are represented in memory. In most C and C++ implementations, `float` is a 32-bit type and `double` is a 64-bit type. Therefore, in these implementations `float` is 4 bytes wide and `double` is 8 bytes wide.

Be careful, because the byte order can be different. Some architectures store the floating type in little endian, some in big endian. There is also a Wikipedia article on [endianness](http://en.wikipedia.org/wiki/Endianness) too.

To copy the bytes to the floating type, you have to make sure that the floating type is the same size as the number of bytes you have, and then you can copy the bytes one-by-one ‘into’ the floating type. Something like this will give you the gist of it:

```
unsigned char rep[] = { 0x42, 0x01, 0x33, 0x33 };
float someFloat;

if (sizeof(someFloat) == 4)
{
    memcpy(&someFloat, rep, sizeof(someFloat));
}
else
{
    // throw an exception or something
}

```

There are other ways of copying the bytes to the floating type, but be careful about ‘breaking the rules’ (type-punning etc.). Also, if the resulting value is incorrect, it may be because the byte order is wrong, and therefore you need to copy the bytes in reverse, so that the 4th byte in the representation is the 1st byte of the float.
If you have a hex value:

```
42 01 33 33

```

It is the equivalent of

```
0100 0010 0000 0001 0011 0011 0011 0011

```

in binary code.

Now, there is a floating point standard called [IEEE 754](http://en.wikipedia.org/wiki/IEEE_754-2008) which tells you how to format a floating point number into binary or back.

The gist of it is that the first bit is the sign (positive/negative number), the next 8 bits are the exponent and the last 23 are the mantisse. This is how the computer internally saves floating point numbers, since it's only able to store 1's and 0's.

If you add it all together in the way the IEEE specifies you get 32.3.