当前位置:网站首页>Matlab reads hexadecimal numbers and converts them into signed short
Matlab reads hexadecimal numbers and converts them into signed short
2022-07-03 09:38:00 【Communication pawn】
Work needs , Need to read hexadecimal data , And into short Data of type , In fact, there are often such data in fixed-point complex operations , however matlab In data conversion , No, c flexible , Still took some trouble . Tried direct forced conversion int16 The data of , Find out matlab Directly discard the symbol bit , That's why I came up with this method .
% The functionality : This program is to read in a hexadecimal data , Put it 16bits Form a symbolic
%short Type data
clc;
clear;
close all;
fid = fopen('test.txt');% Open file , Hexadecimal data is stored here
fseek(fid,0,'bof');% take fid Point to the beginning of the file
data = fscanf(fid,'%x',[1,20]);% Read 20 Bytes of data
data_unshort = zeros(1,10);% Create a 1*10 The matrix of stores unsigned data
data_short = zeros(1,10);% Create a 1*10 The matrix of stores signed data
data = int32(data);% Force conversion to signed 32bit The data of , Avoid using char Type data operation is out of bounds .
j =1 ;% The array subscript
for i = 1:2:20;
data_unshort(j) = data(i)*256 + data(i+1);% Combine two bytes of data to form a short Type data
sign_flag = bitget(data_unshort(j),16:16);% Read the first 16bit The data of , As a sign bit
if 1 == sign_flag ; % Meet the conditions , The explanation is negative
% Consider this data as a complement , Complement code subtract 1, Then remove the highest position , It's the inverse code .
data_unshort(j) =data_unshort(j) -1 -32768;
% In addition to the sign bit, the inverse code bit Reverse is the original code
data_unshort(j) = bitxor(data_unshort(j),32767);
% Adding symbols to the original code is symbolic short Type data
data_short(j) = -int16(data_unshort(j)) ;
else % If it's a positive number , Then there is no need to transform .
data_short(j) = data_unshort(j) ;
end
j =j+1;
end
data_max = max(data_short);
data_min = min(data_short);
x = 1:10;
y = data_short;
plot(x,y);
title(sprintf('max = %d ,min = %d',data_max,data_min));
边栏推荐
- Spark 集群安装与部署
- The number of weak characters in the game (1996)
- Spark cluster installation and deployment
- Filter comments to filter out uncommented and default values
- Equality judgment of long type
- Jestson nano downloads updated kernel and DTB from TFTP server
- Construction of simple database learning environment
- 【22毕业季】我是毕业生yo~
- Apply for domain name binding IP to open port 80 record
- 1300. sum of varied array closed to target
猜你喜欢

Shell logic case

Hudi quick experience (including detailed operation steps and screenshots)

LeetCode每日一题(2090. K Radius Subarray Averages)

Flask+supervisor installation realizes background process resident

Solve editor MD uploads pictures and cannot get the picture address

Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)

Leetcode daily question (1162. as far from land as possible)

MySQL environment variable configuration

Flink CDC practice (including practical steps and screenshots)

Spark structured stream writing Hudi practice
随机推荐
Failed building wheel for argon2 cffi when installing Jupiter
Flink learning notes (IX) status programming
Epoll read / write mode in LT and et modes
Common software open source protocols
小王叔叔的博客目录【持续更新中】
Leetcode daily question (1162. as far from land as possible)
Leetcode daily question (745. prefix and suffix search)
Serializer rewrite: update and create methods
Modify idea code
ERROR: certificate common name “*.” doesn’t match requested ho
The rise and fall of mobile phones in my perspective these 10 years
Basic knowledge of database design
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
解决Editor.md上传图片获取不到图片地址问题
Install local sources using yum
IDEA 中使用 Hudi
一款开源的Markdown转富文本编辑器的实现原理剖析
Hudi learning notes (III) analysis of core concepts
[solution to the new version of Flink without bat startup file]