How do i convert data to 2 bit hexadecimal using num2hex?

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 件のコメント

Jan
Jan 2017 年 7 月 31 日
Why is "ffc00000" 8 bits? Do you mean 3 characters? And if so, what is the wanted output for 2 characters?
Tousif Ahmed
Tousif Ahmed 2017 年 7 月 31 日
In hexadecimal "ffc00000" represents 32 bit, is there any way to convert it to 8 bit like example "ff" ?
James Tursa
James Tursa 2017 年 7 月 31 日
編集済み: James Tursa 2017 年 7 月 31 日
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
Tousif Ahmed 2017 年 7 月 31 日
It's like this I have image of size 27x18 and when I am extracting HOG features for that I get 72x1 which are the extracted features. 72x1 has all floating point values so I am using "num2hex" which gives me 8 bits of values but I need that in 2 bits to give it to FPGA. What if the output is "DA123456"? How can I convert it to 2 bits? I hope you are getting what i am telling
James Tursa
James Tursa 2017 年 7 月 31 日
編集済み: James Tursa 2017 年 7 月 31 日
"... 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
Tousif Ahmed 2017 年 7 月 31 日
yes i want to give 2 hex characters to the FPGA
Tousif Ahmed
Tousif Ahmed 2017 年 7 月 31 日
here is the snapshot which i have attached, one is the output of the HOG extracted and the other is when i am using "num2hex" for the HOG extracted outputs. In num2hex as it can be seen it has 8 characters totally having 32 bits, i am asking is there any way to convert that 32 bits to 8 bits?
Walter Roberson
Walter Roberson 2017 年 7 月 31 日
編集済み: Walter Roberson 2017 年 7 月 31 日
"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
Tousif Ahmed 2017 年 8 月 1 日
Sir, can you please explain the above written code
Walter Roberson
Walter Roberson 2017 年 8 月 1 日
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
Jan 2017 年 8 月 1 日
@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
Tousif Ahmed 2017 年 8 月 1 日
@Jan Simon:Sir, i don't want to convert to floating point, i want to convert the floating point to 8 bit i.e 2 hexadecimal characters. When i am using the num2hex command it converts to 32 bit or 64 bit but i need the output in 8 bit not 32 or 64.
Walter Roberson
Walter Roberson 2017 年 8 月 1 日
Please give a couple of sample inputs and corresponding desired outputs.
Jan
Jan 2017 年 8 月 1 日
@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.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 7 月 31 日

1 投票

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.

カテゴリ

タグ

質問済み:

2017 年 7 月 31 日

コメント済み:

2017 年 8 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by