How to make a program that conver a numer to binary, decimal, hexadecimal and octal.

11 Ansichten (letzte 30 Tage)
Hi guys, can someone help me with my project please?higuys.png
I need a program that convert a number to decimal, binary, hexadecimal or octal value.
I'am working on GUI and i have on my GUIDE the next things:
2 popupmenu
2 edittext
Well, i was thinking i could do a simple conversor (like google conversor).
And the edittext1 i enter the number to convert, then i choosee on the popupmenu1 what value is it(it can be decimal, binary, hexadecimal or octal value)
after that, i choose on the popupmenu2 what value i wanna convert and that value will be set on the edittext2.
I'am a beginner here,guys i hope you can help me with the code, please, help me with a code with the native variables(like edittext, popupmenu, etc).
Thanks ☺

Antworten (2)

Walter Roberson
Walter Roberson am 9 Sep. 2019
If you generate a uicontrol popup or listbox in GUIDE, and you examine the comments for the code it generates for the callbacks, it tells you how to find out which entry was selected:
idx = get(hObject, 'Value');
This will be empty of nothing has been selected, and otherwise will be a positive integer 1 to the number of entries in the String property, indicating which of the entries has been selected.
You can find out which string was selected by using:
all_entries = get(hObject, 'String');
selected_string = all_entries{idx};
This will be a character vector that you can proceed to process with strcmp() or a switch/case expression:
switch selected_string
case 'Binary'
...
case 'Hexadecimal'
...
end
The above presumes that you are within the Callback for either the source or target popup. You can get access to the other of the two popups by referencing it by tag instead of referencing hObject, such as
src_idx = get(handles.source_base, 'Value');
targ_idx = get(handles.target_base, 'Value');
  2 Kommentare
Jhon Rackham
Jhon Rackham am 10 Sep. 2019
Hi Mr Roberson, thank you so much for the help.
But I don't know what I have to do in the "switch case" what I have to put into it? Can you help me with the entire code please.
Sorry if i'am bothering you asking for the entire code, but I can't do it alone, i'am a begginer here, I hope you can help me.
Walter Roberson
Walter Roberson am 10 Sep. 2019
In the switch case for the source base, case 'Binary' should use bin2dec() to convert the input string to decimal. 'Hexadecimal' should use sscanf() with '%lx' format. 'Decimal' should use sscanf() with '%ld' format. 'Octal' should use sscanf() with '%lo' format'. In all four cases, the output should be a decimal number.
Then you have a second switch case, this time for the target base. case 'Binary' should use dec2bin() to convert the decimal to binary. 'Hexadecimal' should use sprintf() with '%lx' format to convert the decimal to hex. 'Decimal' should use sprintf() with '%ld' format. 'Octal' should use sprintf() with '%lo' format.
Take the resulting string and set the String property of the target field to it.
Note: I recommend that you make the target field a uicontrol style 'text' rather than a uicontrol style 'edit' as you do not want the user to be able to edit the output.

Melden Sie sich an, um zu kommentieren.


Ted Shultz
Ted Shultz am 9 Sep. 2019
There are built in matlab funtions that will do this conversion for you.
Check out:
etc.
Good luck!

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by