How to make a program that conver a numer to binary, decimal, hexadecimal and octal.
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi guys, can someone help me with my project please?
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 ☺
1 Kommentar
Stephen23
am 10 Sep. 2019
Duplicate questions:
and other identical ones that have already been closed.
Antworten (2)
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
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.
Ted Shultz
am 9 Sep. 2019
There are built in matlab funtions that will do this conversion for you.
Check out:
etc.
Good luck!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!