It's UWAweek 48

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

Displaying the 2 articles in this topic
Showing 2 of 919 articles.
Currently 1 other person reading this forum.


 UWA week 32 (2nd semester, week 3) ↓
SVG not supported

Login to reply

👍?
helpful
1:28pm Tue 9th Aug, ANONYMOUS

Hello I am attempting to do lab 2 and I am trying to work out the best ways to convert between different types. I can't seem to find a straight forward way to do it, so I'm wondering what the go-to ways are for the following: 1. String/Char to int (e.g., '1234' to 123 or '9' to 9) 2. int to string/Char (e.g., 1234 to '1234' or 9 to '9') -> I've been trying to use itoa() for this, but it doesn't seem to work. Thank you for your help.


SVG not supported

Login to reply

👍?
helpful
3:29pm Tue 9th Aug, Christopher M.

ANONYMOUS wrote:
> 1. String/Char to int (e.g., '1234' to 123 or '9' to 9)
Hi. Firstly, let's make sure we're using the correct C syntax, else we may be chasing a solution that doesn't exist. Strings, sequences of zero-or-more characters, are represented between double-quotes, such as "1234". Single characters are represented between single-quotes, such as '9'. In the ASCII character set (see man ascii ) the digit/character '9' does not have the integer value of 9, and the digit/character '3' does not have the integer value of 3, BUT the difference between '9' and the integer 9, is the same as the difference between '3' and the integer 3. Have a look at the table from man ascii - look at the decimal values - and you'll see the values of '9', '3', etc. Most importantly, look at the integer value of '0'. Now, you want the difference between the characters '9' and '3' to be the same as the difference between 9 and 3 (and it is!) Got it yet?? Next, observe that the integer 'meaning' of the string "123" is 100 * magicfunction('1') + 10 * magicfunction('2') + 1 * magicfunction('3') by Horners' Rule. Just need to work out that magicfunction() ....
> 2. int to string/Char (e.g., 1234 to '1234' or 9 to '9') -> I've been trying to use itoa() for this, but it doesn't seem to work.
There is no itoa() function in the C library. We didn't quite get there this morning, but this page will help: [CITS2002]

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  1:17AM Sep 14 2022
Privacy policy