MATLAB: Floating point to 16 bit hexadecimal

floating point

Hello all, how can i convert floating point decimal to 16 bit hexadecimal value?
Thank you

Best Answer

You can use this file exchange submission. It will convert your number to 16-bit half precision format. But instead of returning as a string, it will return the equivalent uint16 MATLAB object. To get binary string, you can do this,
halfPrecisionNumber = halfprecision(1.2); % halfprecision() is provided in the file exchange package.
halfPrecisionString = dec2bin(halfPrecisionNumber, 16);
In case you face the following error on running the package
"Floating point bit pattern is not IEEE 754"
you can try following
  • Open halfprecision.c file.
  • Add following to include section
#include <stdint.h>
  • Replace 4 #define with the following macros
#define INT16_TYPE int16_t
#define UINT16_TYPE uint16_t
#define INT32_TYPE int32_t
#define UINT32_TYPE uint32_t
  • Run mex halfprecision.c.