How can I write a program that prompts a positive integer and converts it from decimal to hexidecimal?
1 回表示 (過去 30 日間)
古いコメントを表示
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
回答 (1 件)
Roger Stafford
2017 年 11 月 16 日
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?
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!