Converting Decimal to Hexadecimal without the use of the dec2hex function.

6 Ansichten (letzte 30 Tage)
Thomas am 20 Nov. 2011
Hi, I was recently assigned this as a project in my mathlab class, and I'll be honest when I say I really have no idea where to start. I understand that a hexadecimal stems from when we divide the original number by 16 and assign its remainder a numerical value, but I don't know where to start in Matlab. Any help appreciated.
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Alex am 20 Nov. 2011
I would first recommend sitting down with some paper and figuring out a basic idea of the steps you need to do to convert Dec number into Hex number.
Once you have those steps, or the idea of what steps to take, then it becomes much easier to program them in.
You'll want to remember the modulus function http://www.mathworks.com/help/techdoc/ref/mod.html.
Using the division and the Mod function, you'll be able to create each Hex digit (these digits will be in dec values - ex. dec 11 will tell you that you will have a single hex digit of value 11 - so you'll need to eventually convert that to the Hex equivalent- 'B')
edit: the floor function will also come in handy.
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

James Tursa am 20 Nov. 2011
I will get you started with one approach. You could start with a char array like this:
hexdigits = '0123456789abcdef';
Then repeatedly:
- mod your number with 16
- use that result and the hexdigits array to find the corresponding hex digit
- shift the input over 4 bits and go back to the mod
I will leave it to you to figure out the syntax for the above. If you need more help then post your code and ask questions about it.
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Thomas am 20 Nov. 2011
Thanks guys, I'll try my hand at it and post my results here. It's just that all this is really new to me, and pretty obscure as far as programming goes.
1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Alex am 20 Nov. 2011
If you are new to programming, then I recommend starting on paper. If you know how to do it on paper, then it should be easier to transfer it to the computer.
If you can't come up with a process for an arbitrary dec value X, choose a few random dec values, and work through them. That should get you an idea for what you could do for any dec value.

Melden Sie sich an, um zu kommentieren.

Jan am 20 Nov. 2011
I'm sure James and Alex point to the right direction.
But if you do not have any further ideas, fprintf('%x', num) can do the job for you. Of course this will not satisfy the educational part of the question. However, the result is correct.
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming 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!

Translated by