Convert one 32bit float number into two 16bit uint number and then convert back to that 32bit float again

thundium

I am working on transfter an 32bit float number from one platform to the other. Well it is only allowed to pass 16bit unsinged int member to the transfter register. I am thinking that I can then seperate the 32bit float into two 16bit and then conver to 32bit on the other side again.

(I am using C language)

Like:

float A = 3.14
uint16_t B = A & 0xffff;
uint16_t C = A & 0xffff0000;

float D = C<<16 & B;

Obevious this is not correct as float data will be converted to unsigned int when it is assigned. So how shall I do it usually? there shall be some quite mature methods to do similiar thing

Thanks

Paul R

You can use a union for this, e.g.:

typedef union {
    float f;
    uint16_t a[2];
} U;

U u;

u.f = 3.14f;

printf("%g -> %#x %#x\n", u.f, u.a[0], u.a[1]);

LIVE DEMO

Note: strictly speaking this is undefined behaviour, but it's such a widely used technique that it is unlikely to fail. Alternatively you can take a safer, but potentially somewhat less efficient approach, and just use memcpy, like this:

float f = 3.14f;
uint16_t a[2];
memcpy(a, &f, sizeof(a));
printf("%g -> %#x %#x\n", f, a[0], a[1]);

LIVE DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Combine and convert two 16 bit numbers into one 32 bit number (float) in JavaScript

From Dev

How to convert/store a double precision (64bit) number to a 32 bit float or a UInt16 in javascript

From Dev

Convert a number into a 16-bit float (stored as bytes) and back?

From Dev

How to Convert 32 bit float value to two 16 bit integers

From Dev

Is there an easy way to convert from 32bit integer to 16bit integer?

From Dev

How to convert 16bit binary file to 32bit file?

From Dev

Generating random 32bit number in assembly

From Dev

Two 16 bit ints to One 32 bit float value

From Dev

Why does 4294967295 (the highest number at 32bit) equal -1?

From Dev

Why does 4294967295 (the highest number at 32bit) equal -1?

From Dev

convert 32 bit, binary number to decimal in python

From Dev

Convert 64bit PHP to 32bit on WAMP 64bit

From Java

Convert PLC Modbus Signals to Float Value (pair of 16 bits integer into a 32 bit float)

From Java

Convert an integer to a float number

From Dev

Convert Number to Float in Python

From Dev

Convert Uint8 (floating-point AUDIO_F32) to int16_t (16bit PCM)

From Dev

Convert 24bit Two's Complement to float_32t

From Dev

Qt ( C++ ) Convert between 32 bit float and QRgba

From Dev

Convert C++ dll from 32bit to 64bit in MS VC 2013 Express

From Dev

Convert C++ dll from 32bit to 64bit in MS VC 2013 Express

From Dev

Should I Stick to 32bit or 64bit Types for Fixed-Point Number Class?

From Dev

Should I Stick to 32bit or 64bit Types for Fixed-Point Number Class?

From Dev

Converting 32bit PCM to 16bit PCM programmatically

From Dev

How to convert a codepoint 32bit integer array (UTF-32?) to Windows native string?

From Dev

How to convert dtype(uint16) data into a 16bit png image?

From Dev

Convert an image to a 16bit color image

From Dev

Texture filtering of 32bit integer or float textures on OpenGL ES 3.0

From Dev

Building 32bit Float from 4 Bytes [Binary File I/O] in C++

From Dev

Encoding Audio with lame: Converting 32bit float to mp3

Related Related

  1. 1

    Combine and convert two 16 bit numbers into one 32 bit number (float) in JavaScript

  2. 2

    How to convert/store a double precision (64bit) number to a 32 bit float or a UInt16 in javascript

  3. 3

    Convert a number into a 16-bit float (stored as bytes) and back?

  4. 4

    How to Convert 32 bit float value to two 16 bit integers

  5. 5

    Is there an easy way to convert from 32bit integer to 16bit integer?

  6. 6

    How to convert 16bit binary file to 32bit file?

  7. 7

    Generating random 32bit number in assembly

  8. 8

    Two 16 bit ints to One 32 bit float value

  9. 9

    Why does 4294967295 (the highest number at 32bit) equal -1?

  10. 10

    Why does 4294967295 (the highest number at 32bit) equal -1?

  11. 11

    convert 32 bit, binary number to decimal in python

  12. 12

    Convert 64bit PHP to 32bit on WAMP 64bit

  13. 13

    Convert PLC Modbus Signals to Float Value (pair of 16 bits integer into a 32 bit float)

  14. 14

    Convert an integer to a float number

  15. 15

    Convert Number to Float in Python

  16. 16

    Convert Uint8 (floating-point AUDIO_F32) to int16_t (16bit PCM)

  17. 17

    Convert 24bit Two's Complement to float_32t

  18. 18

    Qt ( C++ ) Convert between 32 bit float and QRgba

  19. 19

    Convert C++ dll from 32bit to 64bit in MS VC 2013 Express

  20. 20

    Convert C++ dll from 32bit to 64bit in MS VC 2013 Express

  21. 21

    Should I Stick to 32bit or 64bit Types for Fixed-Point Number Class?

  22. 22

    Should I Stick to 32bit or 64bit Types for Fixed-Point Number Class?

  23. 23

    Converting 32bit PCM to 16bit PCM programmatically

  24. 24

    How to convert a codepoint 32bit integer array (UTF-32?) to Windows native string?

  25. 25

    How to convert dtype(uint16) data into a 16bit png image?

  26. 26

    Convert an image to a 16bit color image

  27. 27

    Texture filtering of 32bit integer or float textures on OpenGL ES 3.0

  28. 28

    Building 32bit Float from 4 Bytes [Binary File I/O] in C++

  29. 29

    Encoding Audio with lame: Converting 32bit float to mp3

HotTag

Archive