How to take a hex value as user input?

& Is it possible to manipulate hex values(eg Xoring etc.) directly or indirectly?

 Accepted Answer

Stephen23
Stephen23 on 17 Jan 2018
Edited: Stephen23 on 18 Jan 2018
No, MATLAB does not accept HEX literals. You can specify HEX in a string/char array.
You can enter hex data as strings/char, e.g.:
>> str = 'FF2';
>> reshape(dec2bin(hex2dec(str(:)),4).',1,[])
ans = 111111110010

6 Comments

Can I manipulate this hex inputted as string?
Or atleast convert them to binary?
>> str = 'FF2';
>> reshape(dec2bin(hex2dec(str(:)),4).',1,[])
ans = 111111110010

Thanks alot.

I'm a Matlab newbie.
If you are not too busy.
I had few more doubts.
Like what exactly happens here in the reshape fn.
& I was able to pass an user input by
str=input('enter value1','s')
reshape(dec2bin(hex2dec(str(:)),4).',1,[])
I wanted to give 2 inputs so I tried repeating code after replacing str by str1.
But, didn't work.
reshape changes the shape of an array, without changing the number of elements. Try removing it and see what you get:
>> str = 'FF2';
>> dec2bin(hex2dec(str(:)),4)
ans =
1111
1111
0010
The transpose .' is required because MATLAB works down the columns before along the rows.
Carlos Obiglio comments to Adarsh Santhosh:
try after the input write instruccion 'str1 = char(str)' and change in the reshape str by str1.

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!