Tutorial: String Variables 10

From GECK
Jump to: navigation, search

This is the final article in a tutorial seriess on string variables.

Switching Between Strings and Other Representations: Character Functions

I'm not spending too much time on these, this has gotten pretty lengthy already ;) I'm just pooling them together here because they're all over the place in obse docs.

Just a few basics: you know the ToString($) function by now, and it's turned out to be pretty handy. It has a ToNumber counterpart (short: #), which will try to pass a number that's in a string to a float/int:

let sv_somestringvar := "34.5"
let fFloat := ToNumber sv_somestringvar
let fFloat := #sv_somestringvar  
  
    --> fFloat will be 34.5

It can also do this with hex numbers kept in a string if you add the hex bool parameter, or if the string started with "0x", which can catch a dynamically created ref.

let sv_somestringvar := "00000ADE"
let fFloat := ToNumber sv_somestringvar, 1

NumToHex, in turn, converts an int into a hex string, with a default width of 8 characters, although you can specify that as a parameter:

let iInt := 2782
let sv_somestringvar := NumToHex iInt         --> "00000ADE"
let sv_somestringvar := NumToHex iInt, 4        --> "0ADE"

To get some operations done at the level of single characters, you need to pass a character from a string to its ascii code, which you can do with Sv_GetChar:

let iSomeInt := sv_GetChar "somestring" somePosInt
let iSomeInt := sv_GetChar "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ.,:" 4  

    --> will return 53, which is the ascii code for 5, at pos 4 in our string

If our string is a single character, we can just use CharToAscii

let sv_somestringvar := "5"
let iSomeInt := CharToAsci sv_somestringvar 
    --> again, 53

Once passed to its ASCII code, you can check the type of the character with the boolean returning IsDigit, IsLetter, IsPunctuation and IsPrintable functions. You can also switch between upper and lower case with ToUpper and ToLower. And then return that ASCII code int back to a string, with AsciiToChar:

let sv_SomeStringVar := AsciiTochar 53 
       
    --> "5"

let iSomeInt := ToLower 65 ; 65 is A   
    --> iSomeINt = 141

let sv_SomeStringVar := AsciiToChar iSomeInt  
    --> "a"

If you want to perform such functions and checks on each character in a string, rather than one you retrieve from its position with Sv_GetChar, you can use a foreach loop, which passes each character to a string var, the iterator, that holds it for the duration of the loop body:

foreach sv_iterator <- "SomeString" ; or: foreach sv_iterator <- somestringvar
    ; do stuff to sv_iterator, which will contain one character for each loop run going from position 0 to end
loop

The "Break" and "Continue" commands, which you might remember from While loops, apply here too.


And now that we're talking int codes anyway, GetKeyName (aka GetKey) will return the string for a keyboard key if you pass the directx scan code as an int parameter:

let sv_somestringvar := GetKeyName 1   

    --> "Escape"

which I suppose can be handy for different localizations, or for situations where the %k format specifier doesn't suffice.


And that's it. Trust me, you'll rarely need to use all of these functions, just be sure to be clear on the basics, and know what your options are.


External Links