当前位置:网站首页>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));
边栏推荐
- Find all possible recipes from given supplies
- QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
- Logstash+jdbc data synchronization +head display problems
- Hudi quick experience (including detailed operation steps and screenshots)
- How MySQL modifies null to not null
- The server denied password root remote connection access
- Process communication - semaphore
- Flask+supervisor installation realizes background process resident
- Common formulas of probability theory
- Construction and test of TFTP server under unbuntu (Debian)
猜你喜欢

Hudi 数据管理和存储概述

Idea uses the MVN command to package and report an error, which is not available

Vscode Arduino installation Library

Solve editor MD uploads pictures and cannot get the picture address

PolyWorks script development learning notes (II) -treeview basic operations
![[CSDN] C1 training problem analysis_ Part III_ JS Foundation](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN] C1 training problem analysis_ Part III_ JS Foundation

Spark overview

Analysis of the implementation principle of an open source markdown to rich text editor

MySQL Data Definition Language DDL common commands

全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
随机推荐
The number of weak characters in the game (1996)
从0开始使用pnpm构建一个Monorepo方式管理的demo
Please tell me how to set vscode
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
Integrated use of interlij idea and sonarqube
Getting started with shell programming
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
[CSDN]C1训练题解析_第四部分_Web进阶
Spark 概述
Serializer rewrite: update and create methods
Shell logic case
Spark cluster installation and deployment
1922. Count Good Numbers
[CSDN]C1訓練題解析_第三部分_JS基礎
Epollet lessons
Flink learning notes (10) Flink fault tolerance mechanism
Leetcode daily question (931. minimum falling path sum)
制作jetson nano最基本的根文件系统、服务器挂载NFS文件系统
QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
Logstash+jdbc data synchronization +head display problems