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 Kommentare

Patrik Ek
Patrik Ek am 21 Mär. 2014
Bearbeitet: Patrik Ek am 21 Mär. 2014
I am not sure I follow, how are you going to transform 'hi' to hexagonals? Do you mean that for a given coding translate the string to a 2 digit hexagonal number? With digits I mean 0-f of type string.
ya...i exactly mean what u said..,i want to transform the string to a hexdecimal value.the direct command may not be available.but is there any other way that u can suggest?
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.
Thanku sir,i got the answer.but i should take the input from the user.from the code u have given,the string should give directly...i need to take the input from the user...how can i get it? my code is ui=input('enter the user value:','s') pwi=input('enter the password:','s') concate=strcat(ui,pwi) decString = unicode2native('concate','utf-8') hexString = dec2hex(decString,8)
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.
siy,By using 'uint8' command...i got my answer...this will convert the string into integer value..we can convert the decimal value to hex by using dec2hex command...
decstring=uint8(concate)
hexstring = dec2hex(decstring)
check this code....thanku for ur reply..

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Patrik Ek
Patrik Ek am 21 Mär. 2014
Bearbeitet: Patrik Ek am 21 Mär. 2014
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!

Weitere Antworten (3)

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.
Mischa Kim
Mischa Kim am 20 Mär. 2014
Bearbeitet: Mischa Kim am 20 Mär. 2014
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 Kommentare

satya
satya am 20 Mär. 2014
Bearbeitet: satya am 20 Mär. 2014
sir,my input string is abc.and i want to display the hexadecimal value for string abc .can i get it by using this command?
Mischa Kim
Mischa Kim am 20 Mär. 2014
Bearbeitet: Mischa Kim am 20 Mär. 2014
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?
sir,Actually i am designing a SHA-512 hash algorithm.according to my application the user have to give input i.e..,some string or value..and that string must be converted to HEX value for further computations.As i am new to matlab...i am not aware of basics...
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 .

Melden Sie sich an, um zu kommentieren.

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 Kommentare

sir,For the code u have given,user have to give their input in hexadecimal value,but my input is string.that may be a name of a person..
You really lost me … Can you provide a flow-diagram for your problem?

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 20 Mär. 2014

Kommentiert:

am 8 Apr. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by