Converting hex to binary and displaying which bits are set

Hi, I am currently working on a function that will output what state a system is in at a particular point in time indicated by a hex input. For the purpose of this particular peice of code it is only one byte, however i may require to adapt it to two bytes.
I am looking to be able to enter for e.g. [a b c d e f g h] = flags ('F3') and then see the state for each bit displayed on the screen.
My code i'm working on is:
function [ a b c d e f g h ] = flags( hex_str )
x = dec2bin(hex2dec(hex_str));
if x('bit 0') == 0; % a
disp('not_follow');
else
disp('follow');
if x('bit 1') == 0; % b
disp('not_cleared');
else
disp('cleared');
if x('bit 2') == 0; % c
disp('not_loading');
else
disp('loading');
if x('bit 3') == 0; % d
disp('not_locked');
else
disp('locked');
if x('bit 4') == 0; % e
disp('not_initiated');
else
disp('initiated');
if x('bit 5') == 0; % f
disp('not_open');
else
disp('open');
if x('bit 6') == 0; % g
disp('not_ready');
else
disp('ready');
if x('bit 7') == 0; % h
disp('not_used');
else
disp('used');
end
Could someone tell me where i am going wrong or suggest an easier solution to my problem? Any help is much appreciated. Thanks!

Answers (1)

x will be a vector of values. Index it at a numeric index, not at a string. x(1), x(2), and so on.
Hint: re-read the documentation for dec2bin() and look at the optional parameters.

4 Comments

What do you mean index it at a numeric index? I've read the documentation for dec2bin and i'm getting nowhere.
Is the problem in x = dec2bin(hex2dec(hex_str)); ?
You attempted to use
x('bit 3')
In order for that to work, x needs to be a function that accepts a string as its parameter, or x need to be a vector (or array) with at least 116 elements and your desired purpose needs to be to index x at locations 98, 105, 116, 32, and 51 -- that is, x('bit 3') is x([double('b') double('i') double('t') double(' ') double('3')]). In no case will x('bit 3') refer to bit #3 of anything.
The array you get back from the dec2bin() will be a standard vector, that you need to access such as x(1), x(2), x(3) and so on. The first value, x(1), will correspond to the left-most bit returned by dec2bin(). So for example, for dec2bin(11) is '1011' and x(2) would correspond to the second from the left, the bit that happens to be '0' in this example. The '0' in that position signifies, in this example, 0 * 2^2. But for (say) dec2bin(5) which is '101', x(2) would be '0' again but that '0' would signify 0 * 2^1. Therefore, indexing from the beginning of the array might refer to different numeric values. If only there was a way to get dec2bin() to always output the same number of bits so that you knew ahead of time what numeric value a particular index referred to...
Thanks Walter. Can you give me an example of how you would implement that in code? I have been playing about with it for the past 2 days getting nowhere!

Sign in to comment.

Categories

Asked:

on 24 Feb 2013

Community Treasure Hunt

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

Start Hunting!