From int to string and then string back to int

BlitzMax Forums/BlitzMax Programming/From int to string and then string back to int

AvestheFox(Posted 2011) [#1]
Blitz3D didn't seem to give me this problem. I'm trying to create a custom numeric counter. Here's the function I used in Blitz 3D:



the problem after pasting this function into Blitz Max is that its having a hard time converting the number in the string back into an int (which would read the frame number of the number image)

So what would be the BMax alternative for this type of code?


AvestheFox(Posted 2011) [#2]
in other words.. if the string was "1" then I'd want to convert it from a string to an integer variable that = 1


BlitzSupport(Posted 2011) [#3]
I'm not totally following the code, but this seems to produce the same result in BlitzMax as I get in Blitz3D, by making var_str a string variable. The contents of "variable" are then converted to a string automatically...

Function counter(variable,pos_x,pos_y)
	var_str:String=variable
	For a=1 To Len(var_str)
		DrawImage gfx_numbers,a*8+pos_x-8,pos_y,Mid$(var_str,a,1)
		Print a
	Next
End Function



BlitzSupport(Posted 2011) [#4]
... or is this the result you want?


Function counter(variable,pos_x,pos_y)
	var_str:String=variable
	Print Int (var_str)
End Function

counter 100, 0, 0



AvestheFox(Posted 2011) [#5]
I never thought to actually put the string variable into an int() bracket. That did the trick!

Thanks! :D

(also, forgive my lack of using ':string' or :anything' I'm still new to the Blitz Max scene and I'm not sure what those are for or exactly what they do :P )


BlitzSupport(Posted 2011) [#6]
Well, :String is the same as $ -- it just states the type of variable.

Putting a string (starting with a number) into Int (string) converts the numerical part of the string into an integer.

I was a little surprised that var_str=Str variable worked, but I guess there's some automatic string-to-int conversion going on there...


Czar Flavius(Posted 2011) [#7]
Make sure you code in Strict mode, so that each variable strictly follows its proper type, by putting Strict as the first command of your program.

You use :type when you first declare a variable, but it's optional from every subsequent usage (I prefer not to). If you leave it out, it defaults to :Int (as they're the most common) You can put :Int if you want.

Strict
Function counter(variable:Int, pos_x:Int, pos_y:Int)
	Local var_str:String = "1"
	For Local a:Int = 1 To var_str.Length
		DrawImage gfx_numbers, a * 8 + pos_x - 8, pos_y, Int(var_str)
	Next
End Function


I'm not entirely sure what you wanted the code to do, so this might not be what you want.


AvestheFox(Posted 2011) [#8]
@blitzsupport

actually the var_str=Str variable bit was edited removing the str function after I copied it to Blitz Max since str doesn't seem to be apart of BMax's language :P sorry for the confusion!

@czar

thanks for the explanation and tip :) I kinda understand now


Htbaa(Posted 2011) [#9]
Print "10".ToInt()



Czar Flavius(Posted 2011) [#10]
In that case, it is converting the string to an int and then back to a string again!


Htbaa(Posted 2011) [#11]
Yes OK, but you can assign it to a variable:
Local i:Int = "10".ToInt()



Warpy(Posted 2011) [#12]
Or, let's be clever! When you do str[n], it returns the ASCII code of the nth character in the string. The digits 0-9 have ASCII codes 48-57. Subtracting 48, therefore, gets the original digit back. So, you could do the following:

Function counter(variable,pos_x,pos_y)
	var_str:String=variable
	For a=0 To Len(var_str)-1
		DrawImage gfx_numbers,a*8+pos_x,pos_y,var_str[a]-49
	Next
End Function


Or, if you're willing to be REALLY clever, do it without any strings at all:
Function counter(variable,pos_x,pos_y)
	'if variable is 0, can just draw the digit zero straight away
	If variable=0
		DrawText "0",pos_x,pos_y
		Return
	EndIf

	'if variable is negative, draw a minus sign in front
	If variable<0
		DrawText "-",pos_x,pos_y
		variable=-variable
		pos_x:+8
	EndIf

	'for any number n,
	'   n Mod 10
	'is equal to the last digit of n
	'so we're going to draw the digits from right to left
	'by finding out the last digit, drawing it, subtracting it from the number, and dividing by 10 so the second-last digit becomes the last digit
	'and so on until there are no more digits
	
	'work out number of digits in variable by taking a copy of it, and dividing by 10 until it won't go any more
	'each time you can divide by 10, that corresponds to a digit
	'integer division always rounds down, so eventually var2 = 0
	length=-1
	var2=variable
	While var2
		length:+1
		var2 :/10
	Wend
	
	'now that we know how many digits the number has, we know where to draw the last digit
	pos_x:+length*8
	
	'draw each digit, following the method described above
	While variable
		m = variable Mod 10
		DrawText m,pos_x,pos_y
		variable = (variable-m)/10
		pos_x:-8
	Wend
End Function


Last edited 2011


Czar Flavius(Posted 2011) [#13]
I think I will stick with strings! :P