Convert Hexadecimal value to signed integer

26 ビュー (過去 30 日間)
Aresh Dadlani
Aresh Dadlani 2014 年 10 月 22 日
回答済み: Ulrich Moser 2019 年 3 月 19 日
Hi, I have a .bin file in which data is stored in hexadecimal format. I want to do the following:
1. Read the hexadecimal data from the .bin file.
2. Convert the data into signed integer. For instance, if we have FF5C in the file, I want to read it and convert it into its corresponding signed integer representation which is -164.
Could anyone please show me the code to do so. Thank you in advance.

採用された回答

Guillaume
Guillaume 2014 年 10 月 22 日
You haven't given enough information about your file for us to help much with question 1.
If it's a text file, use fopen, fgetl or similar and fclose. If it's a binary file, use fopen, fread and fclose.
For question 2., if it's a binary file, fread can read the data as signed integer as is. Use:
out = fread(fid, numvalues, *int16);
If it's a text file, convert your hexadecimal string with:
out = typecast(uint16(base2dec(hexstring, 16)), 'int16');
  1 件のコメント
Aresh Dadlani
Aresh Dadlani 2014 年 10 月 23 日
The file is binary. Thank you, it works fine now.

サインインしてコメントする。

その他の回答 (1 件)

Ulrich Moser
Ulrich Moser 2019 年 3 月 19 日
The typecast command worked very well for hex-strings. Thank you for providing this code!

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by