当前位置:网站首页>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));
边栏推荐
- Hudi learning notes (III) analysis of core concepts
- Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
- Filter comments to filter out uncommented and default values
- LeetCode每日一题(968. Binary Tree Cameras)
- 1300. sum of varied array closed to target
- Leetcode daily question (985. sum of even numbers after queries)
- [csdn] C1 analyse des questions de formation Partie III Bar _ JS Foundation
- The number of weak characters in the game (1996)
- Win10 quick screenshot
- LeetCode每日一题(1162. As Far from Land as Possible)
猜你喜欢
Flink learning notes (XI) table API and SQL
CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers
Construction of simple database learning environment
Trial of the combination of RDS and crawler
[CSDN]C1训练题解析_第二部分_Web基础
Flink learning notes (VIII) multi stream conversion
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
Spark structured stream writing Hudi practice
Shell logic case
数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
随机推荐
QT qstring:: number apply base conversion
Jetson nano custom boot icon kernel logo CBOOT logo
PolyWorks script development learning notes (III) -treeview advanced operation
PolyWorks script development learning notes (I) - script development environment
Construction and test of TFTP server under unbuntu (Debian)
Usage of pandas to obtain MySQL data
Leetcode daily question (2109. adding spaces to a string)
顺利毕业[2]-学生健康管理系统 功能开发中。。。
Alibaba cloud notes for the first time
Win10 install elk
LeetCode每日一题(1024. Video Stitching)
Hudi quick experience (including detailed operation steps and screenshots)
Spark cluster installation and deployment
QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
[csdn] C1 analyse des questions de formation Partie III Bar _ JS Foundation
LeetCode每日一题(745. Prefix and Suffix Search)
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
Filter comments to filter out uncommented and default values
Flink learning notes (10) Flink fault tolerance mechanism
解决Editor.md上传图片获取不到图片地址问题