How do i convert data to 2 bit hexadecimal using num2hex?
Show older comments
I am using num2hex where i am getting 8 bit as the output, i need to convert it to 2 bit. is there any way for that using num2hex? example :ffc00000, this is the output of 8 bit, how do i convert it to 2 bit?
15 Comments
Jan
on 31 Jul 2017
Why is "ffc00000" 8 bits? Do you mean 3 characters? And if so, what is the wanted output for 2 characters?
Tousif Ahmed
on 31 Jul 2017
James Tursa
on 31 Jul 2017
Edited: James Tursa
on 31 Jul 2017
The bit pattern of 'ffc00000' represents a single class NaN. E.g.,
>> num2hex(single(NaN))
ans =
ffc00000
It is unclear what you are trying to do here. If you are converting a 32-bit single class number (NaN or otherwise) to the underlying hex pattern, you will get 8 characters (1 character per 4 bits). Certainly you can simply pick off the first two characters (to get the sign "bit" and part of the exponent "bits") if that is your desire. But it is not at all clear if this is what you need. Can you clarify?
>> x = num2hex(single(NaN))
x =
ffc00000
>> result = x(1:2)
result =
ff
Tousif Ahmed
on 31 Jul 2017
James Tursa
on 31 Jul 2017
Edited: James Tursa
on 31 Jul 2017
"... 72x1 has all floating point values ..."
What are the range of possible values?
"... I need that in 2 bits to give it to FPGA ..."
What exactly does the FPGA need? I.e., what is supposed to be in those "2 bits"? And do you really mean "2 bits" or "2 hex values"? I.e., are you really supposed to be sending 8 bits (2 hex characters) to the FPGA, where the data sent will be some integer value? (In which case a simple uint8( ) conversion might be what you really want to use as long as the original data is in range for a uint8).
Tousif Ahmed
on 31 Jul 2017
Tousif Ahmed
on 31 Jul 2017
Walter Roberson
on 31 Jul 2017
Edited: Walter Roberson
on 31 Jul 2017
"i am asking is there any way to convert that 32 bits to 8 bits?"
Not while retaining any kind of accuracy.
If you have data that is strictly non-negative in the range 0 to 1 (as all of your examples happen to match) then
uint8( floor(HOG_values * 256) )
Tousif Ahmed
on 1 Aug 2017
Walter Roberson
on 1 Aug 2017
If you have fractions between 0 and 1, then 256 times the number gets you the number of 1/256's the number represents. floor() that gives an integer that will be in the range 0 to 256 (256 only if HOG_values can exactly equal 1). uint8() changes data type and also clips the upper limit so the result would be 8 bit bytes with value 0 to 255.
For example all values from 0.06640625 (inclusive) to 0.0703125 (excluded) would work out as 17, as they are approximately 17/256
Jan
on 1 Aug 2017
@Tousif Ahmed: The explained procedure seems to be indirect: 'ffc00000' is a char vector, which can be interpreted as hexadecimal number. The conversion with dec2hex and hex2dec converts it to a floating point number, but there are different ways for the conversion depending on the decision, if teh number represents a single, a part of a double, a [1x4] vector of INT8 values or a bit pattern. If I replace "bits" by "characters" or "bytes", the question might be meaningful in the one or other way. But why do you convert between bits, bytes, hex and floating point at all? It should be possible to work in the wanted class directly. Then the question concerning the (multiple) conversion might vanish automatically.
Tousif Ahmed
on 1 Aug 2017
Walter Roberson
on 1 Aug 2017
Please give a couple of sample inputs and corresponding desired outputs.
Jan
on 1 Aug 2017
@Tousif Ahmed: Are you sure that the conversion from floating point zo 8 bit is useful? Why don't you use the floating point values directly? E.g. integer values from 0 to 255 or even a scalar UINT8 might be easier.
Stephen23
on 3 Aug 2017
See also this almost identical question:
Answers (1)
Walter Roberson
on 31 Jul 2017
LUT{'0'} = {'00' '00'};
LUT{'1'} = {'00', '01'};
LUT{'2'} = {'00', '10'};
LUT{'3'} = {'00', '11'};
LUT{'4'} = {'01', '00'};
LUT{'5'} = {'01', '01'};
LUT{'6'} = {'01', '10'};
LUT{'7'} = {'01', '11'};
LUT{'8'} = {'10' '00'};
LUT{'9'} = {'10', '01'};
LUT{'A'} = {'10', '10'};
LUT{'B'} = {'10', '11'};
LUT{'C'} = {'11', '00'};
LUT{'D'} = {'11', '01'};
LUT{'E'} = {'11', '10'};
LUT{'F'} = {'11', '11'};
LUT{'a'} = {'10', '10'};
LUT{'b'} = {'10', '11'};
LUT{'c'} = {'11', '00'};
LUT{'d'} = {'11', '01'};
LUT{'e'} = {'11', '10'};
LUT{'f'} = {'11', '11'};
hex = num2hex(single(ScalarValue));
output = [LUT{hex}];
The output would be, for example,
{'11' '11' '11' '11' '11' '00' '00' '00' '00' '00' '00' '00' '00' '00' '00' '00'}
You were not clear on what value you wanted for each output. If you want numeric values such as 0, 1, 2, 3 then you can code those instead of the '10' or whatever.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!