How can I write a program that prompts a positive integer and converts it from decimal to hexidecimal?

1 visualizzazione (ultimi 30 giorni)
Develop a console-based (text-based) interactive MATLAB program named dc2hx (i.e. a function M-file named dc2hx with no input arguments and no output) that prompts its user for a positive integer and writes out the hexadecimal representation of this integer, with a 0x prefix

Risposte (1)

Roger Stafford
Roger Stafford il 16 Nov 2017
If you are allowed to use 'dec2hex', your task would be very simple.
Otherwise, here's a hint. Suppose your number is 1579. Doing floor(1579/16) gives you 98 and 1579-16*98 = 11. Then 11 is your least hexadecimal digit for which the symbol is 'B' with 98 remaining. Applying the same procedure to 98 gives the next higher hex digit as 2 with 6 remaining. The same procedure with 6 will give 6 as the next higher hex digit with 0 remaining, which indicates you should stop. Now reverse the order of these hex digits and you get 62B as your hex number. Does that help you?

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by