Going Through Some Changes

BlitzMax Forums/BlitzMax Beginners Area/Going Through Some Changes

dw817(Posted 2016) [#1]
I need to be able to convert some numbers to strings. In most BASICS, you have a MKI$(), CVI(), MKL$, and CVL().

So the code of
a$=mki$(v)
would result in a$ being a 4-character string from the 4-byte integer v.

Code of [code]v=cvl(a$) would result in v containing an 8-byte integer number derived from the 8-character string a$.

Is there a way to do this in BlitzMAX ?


TomToad(Posted 2016) [#2]
A$ = v will automatically convert from int to string. V = A$.ToInt() converts from string to int.


dw817(Posted 2016) [#3]
Hi Tom.

Oh, let me try that.

Yeah, that's what I thought. It's not working. I.E.: A$="1234" the results in v are 1234 whereas it should be something like:

c1=49, c2=50, c3=51, c4=52
c1*$1000000+c2*$10000+c3*$100+c4

a slightly larger number. I suppose I could write a converter. Was just hoping the system had it already built it.


Brucey(Posted 2016) [#4]
Access your string via its character index..

Print a[0] ' 49
Print a[1] ' 50
etc...


Derron(Posted 2016) [#5]
SuperStrict
Framework Brl.StandardIO

local number:int = 1234

for local i:int = 0 until string(number).length
	print string(number)[i]
Next

'output
'49
'50
'51
'52


If you access a string as "array", the char-code is returned.


Edit: Brucey was faster (BUT i prepared a sample code in that time :-))

bye
Ron


dw817(Posted 2016) [#6]
I'm not understanding that, Brucey. Here is the complete code for one:
a$="1234"

v=cvl(a$)

Print v

Function cvl(a$)
  Return fnca(a$,1)*$1000000+fnca(a$,2)*$10000+fnca(a$,3)*$100+fnca(a$,4)
EndFunction

Function fnca(a$,b)
  Return Asc(Mid$(a$,b))
EndFunction


Correct results are 825373492.


Brucey(Posted 2016) [#7]
SuperStrict

Framework brl.standardio

Local a:String = "1234"

Print cvl(a)


Function cvl:Int(a:String)
  Return a[0]*$1000000+a[1]*$10000+a[2]*$100+a[3]
EndFunction


.. uses 12 less function calls than yours.


dw817(Posted 2016) [#8]
Oh, I see what you're doing. Yeah I forgot about that a$[number] retrieves the ASCII of each character, that is a mite faster. Ok, I'll use that, Brucey. Thanks !

Honestly, a great programming language like BlitzMAX not even supporting MKL, CVL, MKI, and CVI. What's the world coming to. :)


Brucey(Posted 2016) [#9]
Well, you can always implement your own.

But it's no longer 1970 :-)


dw817(Posted 2016) [#10]
Heh, it's okay, Brucey. I got 'em now. These will work for me and maybe they'll help some other programmers too.
' >> CONVERT LONG INTEGER A TO 4-CHARACTER STRING
Function mkl$(a)
  Return Chr$(a Shr 24&$ff)+Chr$(a Shr 16&$ff)+Chr$(a Shr 8&$ff)+Chr$(a &$ff)
EndFunction

' >> CONVERT 4-CHARACTER A$ TO LONG INTEGER - THANKS BRUCEY !
Function cvl(a$)
  Return a$[0]*$1000000+a$[1]*$10000+a$[2]*$100+a$[3]
EndFunction



Brucey(Posted 2016) [#11]
btw, that's not a Long integer. It's just an integer.

A Short is a Short (2 bytes).
An Int is an Integer (4 bytes).
A Long is a Long (8 bytes).


dw817(Posted 2016) [#12]
Whoa, that WOULD be a big number ! Well in other earlier languages it's reversed. Normal INT is -32768 to +32767. In any case, a regular 4-bye integer (I'll rename the remark) will definitely cover what I need, thanks ! :)