How can I represent a hexadecimal string in a 128-bit format in MATLAB?

How can I represent a hexadecimal string as a 128-bit format in MATLAB?
 

 Accepted Answer

You can change the string representation and then use ‘uint8’ so that the two hexadecimal characters fit into one byte.
Please find the sample code below with the necessary comments.
% Get UUID and store as character
uuid=char(java.util.UUID.randomUUID);
val=uuid;
% Remove the '-' to get the hexadecimal values
val(val == '-') = '';
% Turn 'val' into string elements each representing bytes
% reshape(val,2,16) gives a 2x16 char array
% permute(reshapedvalue,[2 1])' gives a 1x16 string array with each element
% of the string representing a byte
byte_hex = string(permute(reshape(val,2,16),[2 1]))';
% Create an array of bytes
bytes_vector = uint8(hex2dec(byte_hex));
% dec2hex is to verify that the byte set is as expected
dec2hex(bytes_vector);

More Answers (0)

Products

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!