How to convert a string into integers (in MIPS)

Click For Summary
To calculate the time difference between two military format times represented as strings, first ensure that the input strings are valid and contain only numeric characters. The conversion from string to integer can be achieved by iterating through the string from the end to the start. For each character, calculate its integer value by subtracting the character '0' from it, and multiply this value by 10 raised to the power of its position in the string. This method allows for the construction of the integer representation of the time, which can then be used to compute the difference between the two times.
Rat_Rodz
Messages
1
Reaction score
0
Hey,

I'm working on a project for my Computer Organization class and I have to read in 2 separate times (in military format with no ":" ) as strings and then find and print the time difference. I am able to read in and save the strings no problem, but I don't have a clue as to how I can manipulate them into integers. any suggestions? ...I would post the code that I have already, but I'm not sure as to how to go about it.
 
Technology news on Phys.org
Rat_Rodz said:
Hey,

I'm working on a project for my Computer Organization class and I have to read in 2 separate times (in military format with no ":" ) as strings and then find and print the time difference. I am able to read in and save the strings no problem, but I don't have a clue as to how I can manipulate them into integers. any suggestions? ...I would post the code that I have already, but I'm not sure as to how to go about it.

Hey Rat_Rodz and welcome to the forums.

The first thing to do is to make sure that the string is valid. For the moment let's assume that the string is required to be only in base 10 (ie only have characters '0' to '9').

So assuming we string has the correct format, the rest is pretty easy.

Going from end of string to start of string we start off with our temp variable to be zero.
For each character we do temp = temp + 10^(counter) * 'value of digit'.

The digit will be calculated by using the relationship 'value of digit' = 'char value' - '0'. counter will increment in a natural loop from 0 to string_len-1.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
1
Views
2K
  • · Replies 47 ·
2
Replies
47
Views
4K
Replies
5
Views
1K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
55
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K