How to write a program to transfer any number to hexadecimal?

In summary, the algorithm would be something like this:Take a number as input, call it numStore the remainder when num is divided by 16 in a temporary variable temp. If temp is less than 10, insert (48 + temp) in a character array, otherwise insert (55 + temp) in the character array.Divide num by 16 now.Repeat steps 2 and 3 until num reaches 0.Reverse the character array and print it.
  • #1
hapypy_09
9
4
Summary:: I have a homework " Write a program to input a number from the keyboard, printing the above screen on base 16 (hexadecimal) system". But I don't know how it works. I find it on the Internet but it only have convert from base system 10 to base system 16. And it is written in assembly language on QT spim.
Code:
newline:    .asciiz    "\n"
    .text
    .globl    main
main:
    #print string mgs1
    li    $v0, 4
    la    $a0, mgs1
    syscall
  
    #Get input from user and save
    li $v0, 5
    syscall
    move $t0, $v0
  
    # Math: I didn't how to do !
  
  
    #print mgs2:
    li     $v0, 4
    la     $a0, mgs3
    syscall
  
    #Print answer: I don't know what to do here
  
  
    #Print \n:
    li    $v0,4
    la    $a0, newline
    syscall
  
    li    $v0, 10            #exit
    syscall
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
And I come from Vietnam. May be English of my is not very well. I hope everyone sympathized ! T.T
 
  • #3
Forget the program for a moment. Say I give you a a decimal number (506)10. How do you convert this to hexadecimal?
 
  • Like
Likes hapypy_09
  • #4
I think I will divide it by 16 until the right part is zero then stop.
- Like this:
a3d7f9563564cc3a9575.jpg
 
  • Like
Likes Merlin3189, WWGD and Wrichik Basu
  • #5
hapypy_09 said:
I think I will divide it by 16 until the right part is zero then stop.
Yes, that is correct. Now you have to implement the same in a program. The algorithm will be something like this:
  1. Take a number as input, call it num
  2. Store the remainder when num is divided by 16 in a temporary variable temp. If temp is less than 10, insert (48 + temp) in a character array, otherwise insert (55 + temp) in the character array.
  3. Divide num by 16 now.
  4. Repeat steps 2 and 3 until num reaches 0.
  5. Reverse the character array and print it.
Remember that you won't know the size of the character array beforehand while initialising. So, take a large array, and keep a counter variable, say count, that will denote actually how many letters/digits you have put into the array. Start with count = 0, and increment count by 1 each time you put something into the array.

48 is the ASCII of the number 0.

Can you try it now?
 
  • #7
Wrichik Basu said:
Yes, that is correct. Now you have to implement the same in a program. The algorithm will be something like this:
  1. Take a number as input, call it num
  2. Store the remainder when num is divided by 16 in a temporary variable temp. If temp is less than 10, insert (48 + temp) in a character array, otherwise insert (55 + temp) in the character array.
  3. Divide num by 16 now.
  4. Repeat steps 2 and 3 until num reaches 0.
  5. Reverse the character array and print it.
Remember that you won't know the size of the character array beforehand while initialising. So, take a large array, and keep a counter variable, say count, that will denote actually how many letters/digits you have put into the array. Start with count = 0, and increment count by 1 each time you put something into the array.

48 is the ASCII of the number 0.

Can you try it now?
Ok I will try
 
  • #8
moved to homework from a technical forum. No template.
 
  • #9
Wrichik Basu said:
Yes, that is correct. Now you have to implement the same in a program. The algorithm will be something like this:
  1. Take a number as input, call it num
  2. Store the remainder when num is divided by 16 in a temporary variable temp. If temp is less than 10, insert (48 + temp) in a character array, otherwise insert (55 + temp) in the character array.
  3. Divide num by 16 now.
  4. Repeat steps 2 and 3 until num reaches 0.
  5. Reverse the character array and print it.
Remember that you won't know the size of the character array beforehand while initialising. So, take a large array, and keep a counter variable, say count, that will denote actually how many letters/digits you have put into the array. Start with count = 0, and increment count by 1 each time you put something into the array.

48 is the ASCII of the number 0.

Can you try it now?
I couldn't make it T.T ! Can you make and note the right side for me, please. I would be very grateful !
 
  • #10
hapypy_09 said:
I couldn't make it T.T ! Can you make and note the right side for me, please. I would be very grateful !
A quick google search with the keywords "decimal to hexadecimal in c" would have opened up tons of results using this same logic. For example, check this one:
https://www.sanfoundry.com/c-program-convert-decimal-hex/
If you still can't understand the code, tell where the problem is.
 
  • Like
  • Love
Likes scottdave and hapypy_09
  • #11
Wrichik Basu said:
A quick google search with the keywords "decimal to hexadecimal in c" would have opened up tons of results using this same logic. For example, check this one:
https://www.sanfoundry.com/c-program-convert-decimal-hex/
If you still can't understand the code, tell where the problem is.
Are there any solutions written by assembly language ?. Because my teacher required use assembly language.
 
  • #12
Your task may be simpler than you realize. Take a look at MIPS syscall #34.
 
  • Like
Likes hapypy_09 and Vanadium 50
  • #13
hapypy_09 said:
Are there any solutions written by assembly language ?. Because my teacher required use assembly language.
When you posted your thread (before it was moved), it had the prefix "C/++/#". I thought you need it in C, because you didn't mention explicitly in the OP that you need it in assembly language. My bad, I should have thought about it.

Unfortunately I have 0 knowledge in assembly language. Check out what has been said in post #12.
 
  • #14
Wrichik Basu said:
When you posted your thread (before it was moved), it had the prefix "C/++/#". I thought you need it in C, because you didn't mention explicitly in the OP that you need it in assembly language. My bad, I should have thought about it.

Unfortunately I have 0 knowledge in assembly language. Check out what has been said in post #12.
Oh my god.This is my mistake, I'm so sorry
 
  • #15
gneill said:
Your task may be simpler than you realize. Take a look at MIPS syscall #34.
It's very likely that the OP is not working directly with a MIPS processor, but is instead using a MIPS simulator. Some MIPS simulators support this system call, but some don't. For example, QtSpim (which I believe the OP is using) doesn't support syscall 34, although the MARS simulator does support it.

In any case, I believe the goal of this exercise is that the student needs to "roll his own" routine to display a given decimal number in hexadecimal form.
hapypy_09 said:
I find it on the Internet but it only have convert from base system 10 to base system 16.
Isn't this what you need to do?
 
Last edited:
  • #16
Maybe one point to prove is that the method will converge and stop at some point. Not too hard. Or is it overkill?
 
  • #17
WWGD said:
Maybe one point to prove is that the method will converge and stop at some point. Not too hard. Or is it overkill?
Yes, overkill as well as irrelevant. There is no approximation going on, so convergence isn't an issue. Also, the input number is a 32 bit integer (a word in MIPS), which can be represented in 8 hex digits. The MIPS program in question here can use a counting loop that runs 8 times, one iteration for each hex digit.
 
  • Like
Likes WWGD
  • #18
For someone experienced like yourself clearly there is no point. With beginners likw myself and, I believe the OP, it may be a good technical exercise, as well as for understanding why any number may be repd that way.
 
  • Like
Likes hapypy_09
  • #19
WWGD said:
For someone experienced like yourself clearly there is no point. With beginners likw myself and, I believe the OP, it may be a good technical exercise, as well as for understanding why any number may be repd that way.
No, not at all. The program that OP is trying to write has nothing to do with convergence, and if the program is written with the understanding that an integer in MIPS (a word) is of fixed size, there is no good reason to use a loop that might run forever.
 
  • Like
Likes Merlin3189
  • #20
@hapypy_09, here's an example showing how to convert a decimal number to hexadecimal, with N = 4032. At each step I divide by 16, keeping track of both the quotient and the remainder.
Step 1: 4032/16 = 252 (quotient), and 0 (remainder)
Step 2: 252/16 = 15 (quotient) and 12 (remainder)
Step 3: 15/16 = 0 (quotient), and 15 (remainder)
The process terminates when the quotient becomes 0. The hex representation consists of the remainders, as hex digits, in reverse order. At most, there will be 8 steps for a 32-bit integer.

So the hex representation of 403210 is FC016, usually written as 0xFC0.

The division I did above was integer division - no fractional part.
To get the remainder, all you need to do is to look at the lowest 4 bits, which you can do by anding the number with 0xF, like this: andi $t0, $a0, 0xF.
You can do integer division of a positive number by shifting right by 4 bits; for example as srl $a0, $a0, 4.

To convert from a decimal number in the range of 0 through 15 to a hex digit 0, 1, 2, ..., 9, A, B, C, D, E, F, use a lookup table, a byte array of these characters.

One tricky part to this problem is putting the hex digit character into a string in reverse order.
 
Last edited:
  • Informative
  • Like
Likes hapypy_09, berkeman and sysprog
  • #22
Mark44 said:
It's very likely that the OP is not working directly with a MIPS processor, but is instead using a MIPS simulator. Some MIPS simulators support this system call, but some don't. For example, QtSpim (which I believe the OP is using) doesn't support syscall 34, although the MARS simulator does support it.

In any case, I believe the goal of this exercise is that the student needs to "roll his own" routine to display a given decimal number in hexadecimal form.
Isn't this what you need to do?
Finally, I understand a titles of my homework ^^. Thank you very much.
 

1. What is hexadecimal?

Hexadecimal is a number system that uses 16 symbols (0-9 and A-F) to represent numbers. It is commonly used in computer programming and is often used to represent binary numbers in a more compact and readable form.

2. How do I convert a decimal number to hexadecimal?

To convert a decimal number to hexadecimal, you can use the built-in functions of your programming language or follow a simple algorithm. Divide the decimal number by 16 and note the remainder, then repeat the process with the quotient until the quotient is 0. The remainders, in reverse order, will be the hexadecimal equivalent.

3. How do I write a program to transfer any number to hexadecimal?

To write a program that transfers any number to hexadecimal, you will need to use a loop to iterate through the decimal number and perform the conversion for each digit. You can also use conditional statements to handle numbers greater than 9, which have letter representations in hexadecimal (A-F).

4. Can I manually convert numbers to hexadecimal?

Yes, you can manually convert numbers to hexadecimal by using a conversion chart or following the algorithm mentioned above. However, for larger numbers, it may be more efficient to use a computer program.

5. Are there any built-in functions for converting numbers to hexadecimal?

Yes, most programming languages have built-in functions for converting numbers to hexadecimal. These functions typically take in a decimal number as input and return the hexadecimal equivalent. It is recommended to use these functions instead of writing your own conversion code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top