How to convert AnsiChar (char8_t) into HEX?
古いコメントを表示
Hello. I save raw sensor data to an SD card. When I open such a file, for example, in the HxD program, I get a preview of the data in HEX, Int8, UInt8 and other formats. Can I do the same directly in MATLAB (R2015a)? How can I convert AnsiChar (char8_t) data into other data types? For example, I would like to convert the data string "ę˙°ţiţ{ý" into the EA FF B0 FE 69 FE 7B FD form.
1 件のコメント
John D'Errico
2023 年 5 月 23 日
@Radoslaw Puchalski - please don't use flags to make a comment. Flags are there for moderation purposes.
回答 (2 件)
It isn't completely clear what you want
datastring = "ę˙°ţiţ{ý"
fprintf('%02x ', typecast(swapbytes(uint16(char(datastring))),'uint8'))
The above is in "big endian" order, which is not the internal order for any currently supported architecture. Big-endian as in the 01 19 is to be interpreted as 0x01 * 256 + 0x19 rather than as 0x01 + 0x19 * 256
dec2hex(datastring{1},4)
But maybe you want utf-8 bytes?
fprintf('%02x ', unicode2native(datastring, 'utf8'))
4 件のコメント
Radoslaw Puchalski
2023 年 5 月 22 日
移動済み: Walter Roberson
2023 年 5 月 22 日
hex='EA FF B0 FE 69 FE 7B FD';
bytes = uint8(sscanf(hex, '%02x', [1 inf]))
bytepairs = typecast(bytes, 'uint16')
char(bytepairs)
char(swapbytes(bytepairs))
native2unicode(bytes, 'utf8')
native2unicode(bytes, 'utf16-le')
native2unicode(bytes, 'utf16-be')
for ch = "windows" + (1250:1258)
disp(ch); native2unicode(bytes, ch)
end
So that leads us to:
datastring = "ę˙°ţiţ{ý"
fprintf('%02X ', unicode2native(datastring, 'windows1250'))
Radoslaw Puchalski
2023 年 5 月 23 日
Radoslaw Puchalski
2023 年 5 月 23 日
編集済み: Radoslaw Puchalski
2023 年 5 月 23 日
Radoslaw Puchalski
2023 年 5 月 23 日
編集済み: Radoslaw Puchalski
2023 年 5 月 23 日
4 件のコメント
Walter Roberson
2023 年 5 月 23 日
Don't use textscan() for this purpose. Do not use line oriented operations. Use binary files with fread()
Radoslaw Puchalski
2023 年 5 月 23 日
編集済み: Radoslaw Puchalski
2023 年 5 月 23 日
Walter Roberson
2023 年 5 月 23 日
You can improve slightly:
filename = 'E:\file.csv';
fileID = fopen(filename,'r');
Data_int16 = fread(fileID, '*int16');
fclose(fileID);
Radoslaw Puchalski
2023 年 5 月 24 日
カテゴリ
ヘルプ センター および File Exchange で Data Import and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!