当前位置:网站首页>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));
边栏推荐
- Win10 install elk
- LeetCode每日一题(2090. K Radius Subarray Averages)
- MySQL environment variable configuration
- Flink learning notes (IX) status programming
- Navicat, MySQL export Er graph, er graph
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
- Implementing distributed lock with redis
- Solve editor MD uploads pictures and cannot get the picture address
- Hudi quick experience (including detailed operation steps and screenshots)
- Leetcode daily question (1362. closest divisors)
猜你喜欢
[CSDN]C1训练题解析_第四部分_Web进阶
Uncle Wang's blog directory [constantly updating]
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
小王叔叔的博客目录【持续更新中】
Flink learning notes (IX) status programming
一款开源的Markdown转富文本编辑器的实现原理剖析
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)
LeetCode每日一题(2090. K Radius Subarray Averages)
Alibaba cloud notes for the first time
Spark structured stream writing Hudi practice
随机推荐
Alibaba cloud notes for the first time
The server denied password root remote connection access
LeetCode每日一题(2305. Fair Distribution of Cookies)
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
LeetCode每日一题(1024. Video Stitching)
LeetCode每日一题(2232. Minimize Result by Adding Parentheses to Expression)
[CSDN]C1训练题解析_第二部分_Web基础
Epoll read / write mode in LT and et modes
Convert IP address to int
Patent inquiry website
顺利毕业[3]-博客系统 更新中。。。
307. Range Sum Query - Mutable
Error output redirection
Run flash demo on ECS
MySQL data manipulation language DML common commands
Solve the problem of disordered code in vscode development, output Chinese and open source code
Send mail using WP mail SMTP plug-in
Word segmentation in full-text indexing
Uncle Wang's blog directory [constantly updating]
Leetcode daily question (1856. maximum subarray min product)