sir,i want to convert string into a hexadecimal value...is there any direct command?
이전 댓글 표시
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
satya
2014년 3월 30일
Patrik Ek
2014년 4월 1일
You could always try my answer down below. It worked fine for me when I tried. unicode2native transforms it to a decimal value and dec2hex does the rest. Please comment if you have further problems. It is good if there is some interactive exchange here. Also it is hard to say exactly what you want in the question and you may not get a copy-paste code. Also, for those with the same problem it is good if you accept answers that solves the issue. An accept works as a "recipe" that the solution is ok and may help those who needs the same answer.
satya
2014년 4월 2일
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.
satya
2014년 4월 8일
채택된 답변
추가 답변 (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월 20일
편집: Mischa Kim
2014년 3월 20일
Not sure I understand. As mentioned above, hexadecimals numbers are represented as strings. That should answer your question regarding displaying numbers. If you want to do computations using hex, you will have to convert to other number formats, for example, decimal.
What exactly do you need to do?
satya
2014년 3월 21일
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
satya
2014년 3월 21일
Jos (10584)
2014년 3월 21일
You really lost me … Can you provide a flow-diagram for your problem?
카테고리
도움말 센터 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!