
-----------------------------------
supaphreek
Fri May 21, 2010 3:06 pm

Variable char to int
-----------------------------------
What is it you are trying to achieve?
Im trying to convert a char variable (which will only be a number) to an integer.


What is the problem you are having?
I cant convert char to an integer :S (I dont know how).


Describe what you have tried to solve this problem
Ive searched up the turing reference to see any commands invovling char hoping there would be one called charint or something :P. Ive tried using strint and (i got it to work using if statements, but its not really efficient... eg if ch = "1" then a := 1).

Ive managed to convert an integer into a char using intstr and then making the variable that was converted to a string into a char so it looked like.... (code below)

And btw, im using char because its being sent thru a network to another PC and it would always messup when i tried to send an int or string :S


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)




var ch : char
var a : int
var b : string

loop
a:=a+1
b := intstr (a,1)
ch := b

end loop



Please specify what version of Turing you are using
4.1.1

-----------------------------------
TerranceN
Fri May 21, 2010 3:16 pm

RE:Variable char to int
-----------------------------------
First convert to a string, then convert to an integer.

loop
    var input : char := getchar()

    var stringVersion : string := input
    var intValue : int := 0
    
    if (strintok(stringVersion)) then
        intValue := strint(stringVersion)
        put "intValue          :", intValue
        put ""
    else
        put "Not an integer, try again"
        put ""
    end if
end loop


-----------------------------------
DemonWasp
Fri May 21, 2010 3:25 pm

RE:Variable char to int
-----------------------------------
Or, remember that the digits 0-9 (which are all you ever expect) are contiguous and sequential in ord and subtract 48, that's the value of the character.


var value : int := ord ( '0' ) - 48
put value


should output 0.

Out of curiosity, what do you mean when you say that "it would always messup" when trying to send integers or strings? What would happen?

-----------------------------------
supaphreek
Fri May 21, 2010 10:46 pm

RE:Variable char to int
-----------------------------------
By messup, i mean it would either freeze or give me an error. Dont really remember what it was :P as i did it this morning an d have tried dozens of things to solve it.

A few hours ago actually before reading this, i looked at one of my codes and realized that to get it to a int from an char:P i used intstr and then converted the string to a char by using char:= string

Then i pretty much figured out how to make it with char :P. I havent thought about the ASCII way though, do you think it would be more efficient using ASCII? since the next part whcih im also confused about..........

I tried sending an integer through the net commands and it would give me an error so my only solution was char which could only allow one digit. So if i were to have an integer above 9, it wouldnt output it. However i managed to get it to cut the string into different parts using string (2) for example which would get the second character.

But since im doing that, 11 would be 1 and 1. How would i add both these numbers to get 11?

LOL, sorry if im overcomplicating everything :P

-----------------------------------
DemonWasp
Sat May 22, 2010 1:05 am

RE:Variable char to int
-----------------------------------
You're probably overcomplicating things. It would be easier to just send integers - this should be pretty possible. Consider posting that problem as a separate thread.

As for the other problem, this is relatively simple, so here's an example to get you on the right track:

123 = 1 * 100 + 2 * 10 + 3
321 = 3 * 100 + 2 * 10 + 1
