sir,i want to convert string into a hexadecimal value...is there any direct command?
27 ビュー (過去 30 日間)
古いコメントを表示
sir,i want to convert string into a hexadecimal value...is there any direct command?suppose my string is abc.i want that abc in the hexadecimal value.plz help me sir
6 件のコメント
Patrik Ek
2014 年 4 月 2 日
You have it already. Just notice that concate is a variable and must be treated as such and not as 'concate' which is a string with value 'concate' and not the variable concate. also remove the 8 in dec2str the input is given as an array of hexadecimal strings, which only is 2 digit each.
However, walter roberson give a nice code that can replace
decString = unicode2native('hi','utf-8');
hexString = dec2hex(decString);
If you want the hexadecimal code as a row vector, instead of a 2xN columnvector. Your choice. Both have pros and cons.
採用された回答
Patrik Ek
2014 年 3 月 21 日
編集済み: Patrik Ek
2014 年 3 月 21 日
Have you tried unicode2native ?
decString = unicode2native('hi','utf-8');
hexString = dec2hex(decString);
You can really select another encoding if you would like and the hexString vector may need to be modified a bit. Otherwise this should work fine. Please comment if this does not solve your problem. If you want the return vector in some special way, then add the wanted output to the question.
Good luck!
0 件のコメント
その他の回答 (3 件)
Walter Roberson
2014 年 4 月 1 日
hexstring = sprintf('%02x', YourString')
Note that if characters beyond 255 are present then you need to take other steps such as using UTF-8 encoding as Patrik suggested.
1 件のコメント
Mischa Kim
2014 年 3 月 20 日
編集済み: Mischa Kim
2014 年 3 月 20 日
Satya, hex values are interpreted as strings, so no need for a transformation. E.g.
a = hex2dec('abc') % converting hex number to decimal
a =
2748
or
a = hex2dec('f')
a =
15
4 件のコメント
Mischa Kim
2014 年 3 月 21 日
OK. There is only limited built-in functionality to deal with hex numbers. So typically, you would read and store hexadecimals as strings. For any kind of hex manipulations you'd have to convert to a different number format (decimals, most likely). You might find some useful code in the File Exchange .
Jos (10584)
2014 年 3 月 21 日
Do you want to convert the string to a value?
STR = input('Enter a hexadecimal number:','s')
% the user types, for instance, 1F
value = hex2dec(STR)
% or
value2 = sscanf('1F','%x')
% both gives 31
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!