필터 지우기
필터 지우기

Floating point to 16 bit hexadecimal

조회 수: 26 (최근 30일)
Tousif Ahmed
Tousif Ahmed 2018년 4월 23일
댓글: Tousif Ahmed 2018년 4월 24일
Hello all, how can i convert floating point decimal to 16 bit hexadecimal value?
Thank you
  댓글 수: 2
Guillaume
Guillaume 2018년 4월 24일
Specifying the encoding used would be helpful. Is it IEEE 754 half precision you're after?
Tousif Ahmed
Tousif Ahmed 2018년 4월 24일
yes IEEE 754 half precision

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 4월 24일
편집: Ameer Hamza 2018년 4월 24일
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.
  댓글 수: 5
Guillaume
Guillaume 2018년 4월 24일
편집: Guillaume 2018년 4월 24일

Use dec2hex instead of dec2bin in Ameer's answer. Do not use num2hex which is not all suited for what you want.

halfPrecisionNumber = halfprecision(1.2);  % halfprecision() is provided in the file exchange package.
halfPrecisionHex = dec2hex(halfPrecisionNumber, 4);
Tousif Ahmed
Tousif Ahmed 2018년 4월 24일
cool that works perfectly. Thanks to Mr. Guillaume and Mr. Ameer Hamza

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by