Int to Str

Monkey Forums/Monkey Beginners/Int to Str

MOBii(Posted 2014) [#1]
I am little confused I can't find char(type) = 0 to 255
I try Google: "monkey x" "int to str"
No results found for "monkey x" "int to str".

I manage to print a Int:
var:Int = 8
Print "text" + var


I like to:
text:String = "abc"
var = text[1].ToInt()

ASC and CHR not in the function list


ImmutableOctet(SKNG)(Posted 2014) [#2]
You can get an integer array containing the string's character codes by calling the 'ToChars' method, and you can make a string from one or more character codes (Integers) by using String.FromChars(CharacterCodes:Int[]) or String.FromChar(CharacterCode:Int).

You can also access a string's character codes by using it like an array.

Here's a full example of some of the things you can do with strings in Monkey X:


That should give you a good enough idea about how strings work, but for more information, please visit the documentation either through an IDE (Like Ted, Monkey's default editor), or here on the website. You can quickly access the documentation for what you're working on by pressing F1.

If for some reason you need the old BlitzBasic styled 'Asc' and 'Chr' commands, I wrote my 'RetroStrings' module a while ago.

EDIT: Updated the sample to have String.FromChar(CharacterCode:Int), and String.FromChars(CharacterCodes:Int[]).


MOBii(Posted 2014) [#3]
SORRY SORRY
Somehow World[n] become the int
Print World[2] ' How can I Print "c"

Global World:String = "abcdefghijklmnop"


Function Main()
Local n:Int

	For n = 0 To 3
		Print n + ": " + World[n]
	Next
	
End



ImmutableOctet(SKNG)(Posted 2014) [#4]
You can use String.FromChar(CharacterCode:Int):

Global World:String = "abcdefghijklmnop"


Function Main()
Local n:Int

	For n = 0 To 3
		Print n + ": " + String.FromChar(World[n])
	Next
	
End



ImmutableOctet(SKNG)(Posted 2014) [#5]
Just for the record, if you want to convert an integer to a string, then back into an integer, you'd do this:

Local I:Int = 10
Local S:String = String(I) ' Will become "10".
Local N:Int = Int(S) ' Will become 10.


I hope what I've posted helps.


MOBii(Posted 2014) [#6]
Global World:String = "abcdefghijklmnop"

Function Main:Int()
Local n:Int

For n = 0 To 3
Print "%" + n + ": " + World[n]
Next
For n = 0 To 3
Print "$" + World[n .. n + 1]
Next

' Thank thee this was what I was looking for
For n = 0 To 3
Print n + ": " + String.FromChar(World[n])
Next

Return 1 ' Why it return 0?
' Process ended with return code 0 at 6/29/2014 9:43:26 PM
End


THis was the ASC/CHR replacement
Local I:Int = 10
Local S:String = String(I) ' Will become "10".
Local N:Int = Int(S) ' Will become 10.



I bow my head
I sitting and testing Monkey X to see if this language is what I like to invest my life?
The helpfile is very good (a Joy to read). The problem for a totally beginner like me is I don't really know what I am looking for.

For example:
Local n:Int = 8

' I notice I can write a var to the String
Print "N=" + n

' OR is this more preferable to write?
Print "N=" + String(n)
' What is most political correct to write (Strict)?


ziggy(Posted 2014) [#7]
Automatic casing from Int to String when you're concatenating is very common in the language and should be considered standard Monkey code.

so this:
Print "Score: " + Score
Is as good as:
Print "Score: " + String(Score)


ImmutableOctet(SKNG)(Posted 2014) [#8]
'Strict' mode effectively makes it so the compiler has very little leeway about type discipline, structure, (And to some extent) ambiguity. Basically, it makes sure your code is rarely ever messy without good reason, otherwise it'll error. For the sake of learning the language, people are on the fence about this. I'd say using Strict from the start is a good thing, as you won't be ashamed at how messy and unreadable your code will look in the long run. The thing is, this topic is a long running one, and both strict and non-strict styles have benefits, and they'll both compile with Monkey. That being said, I've used 'Strict' the entire time I've had Monkey, and it's made me a better programmer because of its ideals. However, it should be noted that 'Trans' (Monkey's compiler) was not written with 'Strict', so no matter what, your code will always compile. One of the issues with non-strict programs in the past was long compile times, but these days, Monkey's actually pretty good about this. And that being said, I think 'Trans' code-wise is the messiest piece of Monkey code out there, but some might not agree, or at the very least defend it for an arbitrary reason.

To make a long story short, I'd recommend using 'Strict' because Monkey's own philosophies are to keep things modular, and re-use code. So if you end up being unable to understand your own code, or your code was poorly written (And likely to be horribly unoptimized), you'll be less likely to use it again. This becomes a problem later on as a programmer, and it technically never stops, but starting with stricter notation would help mitigate this. If you're not planning to write anything long-lasting right now, I'd say keep away from 'Strict', and move to it later. However, if you're planning on making a large(r) game, I'd stick to 'Strict'. Personally, the one thing I look for in a game is how well it was programmed (Which leads to great gameplay a lot of the time). It's good in my book if it's well-structured, multi-purpose, systematically written, (In the case of Monkey, modular), unified code which doesn't halt the experience for those making assets, levels, etc. I personally applaud programmers who go the extra mile, even if they didn't need to.

At the end of the day, it's your decision, I personally just prefer 'Strict'. It's something I initially hated when coming from the original Blitz languages, but I ended up loving it in BlitzMax, and the rest was history.

You should also note that (Especially without 'Strict') you're far more likely to come across some odd errors when you're unsure what type you're actually using. I can't begin to tell you the number of times this has happened to me with the old Blitz languages (Monkey is a decent bit better about this). Not to mention a few people coming to me about this. In the end non-strict code could seriously screw up your program's design if you're really lax about structure, but Monkey's better about this than the Blitz languages. Excluding Max of course, but BlitzMax is basically Monkey with desktop-only functionality and without several features.

And just for the record, the return code of 'Main' is a response code to the operating system. By default, 0 means no error.


MOBii(Posted 2014) [#9]
I use write strict code but always in None Strict Mode. So I try use Strict when I learn Monkey X now.
Life getting little complicate when I looking for an other platform when I am still trying finish my project.
But I am going to change. Today I notice that 1 picture in my animation be visible thru walls and it's nothing I can do to fix that

I believe if the code is poorly documented it's hard to finish little bigger projects
The biggest reason for me to chose Monkey X now is for the online/offline Reference guide, it's almost as good as php.net