Slicing strings

BlitzMax Forums/BlitzMax Beginners Area/Slicing strings

eindbaas(Posted 2007) [#1]
I want to make myself a fucntion that sets opengl-drawcolor from a given color in hex, i.e. setGlColor ("#AABBCC")

I read that Int("$AA") gives me a conversion from hex to int, but how do i slice the incoming string up in three parts?

i thought something like string[0]+string[1] would work, but the problem is that string[index] just gives me an ASCII-number, not the character itself


Dreamora(Posted 2007) [#2]
The function you are looking for is called mid$()

What you are doing is critical, as it does not take unicode into account which is what BlitzStrings are.


klepto2(Posted 2007) [#3]
Maybe you need something like this?

Function SetColorfromString:Byte(Color:String)
	Local R:Int,G:Int,B:Int
	If Color.length <> 7 Then Return False
	R = Int("$"+Color[1..3])
	G = Int("$"+Color[3..5])
	B = Int("$"+Color[5..7])
	Print R+";"+G+";"+B
	'SetColor R,G,B
	Return True
End Function

Print SetColorfromString("$AABB22")
Print SetColorfromString("$000000")
Print SetColorfromString("$FFFFFF")



Perturbatio(Posted 2007) [#4]
To slice strings:
Local s:String = "#AABBCC"

Local R:String = s[1..3]
Local G:String = s[3..5]
Local B:String = s[5..7]

Print R
Print G
Print B



eindbaas(Posted 2007) [#5]
Thanks everyone, got it working now and learned some new things. Where do i lookup things like for example the string[2..3] notation? Having a hard time now and then figuring out some Bmax-basics.


tonyg(Posted 2007) [#6]
In the docs it's under Language / Slices