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!