How do I access hex values stored in a char in matlab?

9 vues (au cours des 30 derniers jours)
Dhruv
Dhruv le 4 Jan 2018
Commenté : Dhruv le 4 Jan 2018
I used DataHash function from MATLAB File Exchange to generate an SHA-256 Hash code for an input plain audio file. But the resultant Hash code was stored in a 'char' data type. How do I retrieve the hex code from it?

Réponse acceptée

Jan
Jan le 4 Jan 2018
Modifié(e) : Jan le 4 Jan 2018
The output types of DataHash are: hex, HEX, double, uint8, base64. You could specify the wanted output type at calling DataHash. hex (hexadecimal number as char vector and lowercase characters) is the default. hex, HEX and base64 reply char vectors. Hexadecimal values are represented as char vectors in general. Therefore your question is not clear. Please post the char vector you have and an example of what you want to get.
  • If your string is like: '5b302b7b2099a97ba2a276640a192485', sscanf is faster than hex2dec or hex2num:
num = sscanf(S, '%2x', [1, Inf])
  • If your string is like: '+tJN9yeF89h3jOFNN55XLg'
function Out = base64toHex(In)
P = [65:90, 97:122, 48:57, 43, 47] + 1; % [0:9, a:z, A:Z, +, /]
v8 = [128, 64, 32, 16, 8, 4, 2, 1];
v6 = [32; 16; 8; 4; 2; 1];
Table = zeros(1, 256);
Table(P) = 1:64;
Data = Table(In(:).' + 1) - 1;
X = rem(floor(bsxfun(@rdivide, Data, v6)), 2);
Num = v8 * reshape(X(1:fix(numel(X) / 8) * 8), 8, []);
Out = sprintf('%.2x', Num);
end
This is a simplified version of a base64 decoder only: It does not consider linebreaks and padding characters. Use it for the output of DataHash only.
  • If you want DataHash to reply the byte values, use:
Opt.Format = 'double'; % Or 'uint8': same values, different type
  1 commentaire
Dhruv
Dhruv le 4 Jan 2018
Thank you sir. Your response made things regarding DataHash more clear to me. My hashcode is like '3717c95fb3a7548f7e3fd5bcfe514171'. I ran the sscanf pseudocode i.e. num = sscanf(S, '%2x', [1, Inf]). I got the following result - num =
55 23 201 95 179 167 84 143 126 63 213 188 254 81 65 113
The 256-bit hash value can be expressed as hexadecimal number array H = [h1, h2, ..., h64]. I wish to get this array as output. How should I proceed?

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 4 Jan 2018
help hex2dec
help hex2num

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by