About Monkey 2 › Forums › Monkey 2 Programming Help › How to convert a string to character?
This topic contains 7 replies, has 5 voices, and was last updated by
codifies
2 years, 2 months ago.
-
AuthorPosts
-
January 23, 2017 at 10:16 pm #6760
How do you convert a string character into an int character?
I tried casting but it does not work.
Also I tried indexing it as an array “a”[0] also, it works, but it does not seem very good idea.
January 23, 2017 at 11:01 pm #6761you can only convert *one* character to a value
[/crayon]Monkey12345678[crayon-5cba84474fea5334468087 inline="true" ]Function Main()Local s:String="Abc"For Local ss:= Eachin sPrint ss+","+Int(ss)NextEndJanuary 23, 2017 at 11:26 pm #6762“a”[0] is fine!
January 24, 2017 at 12:16 am #6765s[0] is what Asc(s) is in BASIC programming languages.
If you like functions like Asc(), make it a function.Calling an extra function like Asc() has some overhead,
and we don’t have Macro’s in MX2, but it can be helpful
in the process of learning and converting older codes.January 24, 2017 at 6:54 am #6779Yes it would be nice is thoses good old BASIC functions where included in std or any core module..
January 24, 2017 at 8:52 am #6782…yeah macros would be useful…
anyone happen to know how to do the equivalent of chr$(n)
…edit $ ah those were the days…..
January 24, 2017 at 11:18 am #6792> anyone happen to know how to do the equivalent of chr$(n)
It is
Monkey1s := String.FromChar(n)If you like to make it a function for compatibility with some old code:
Monkey123456789101112131415161718192021Function Asc:Int(input:String, index:Int=0)Return input[index]EndFunction Chr:String(character:Int)Return String.FromChar(character)EndFunction Main()Local out:StringFor Local i := 1 To 255out += Chr(i)NextPrint outFor Local i := 0 To 4Print Asc("Hello",i) + " - " + Chr(Asc("Hello",i))NextEndJanuary 24, 2017 at 12:02 pm #6793..ah of course, thanks…
although I probably wouldn;t wrap in in an asc function what you’ve made there makes for a nice little example.
-
AuthorPosts
You must be logged in to reply to this topic.