Page 1 of 1

Variable Converting

Posted: Thu Apr 21, 2011 12:45 am
by OneHitWonder
Hi,

I have just started using Purebasic and am finding it really fun and seems like a really nice language. However I am having trouble finding functions to convert strings to integers and other conversion functions.

Can somebody point me in the right direction please?

Thanks alot.

Re: Variable Converting

Posted: Thu Apr 21, 2011 12:47 am
by STARGĂ…TE
PureBasic - String

Val()
ValF()
...
Str()
StrF()
...

Re: Variable Converting

Posted: Thu Apr 21, 2011 12:51 am
by OneHitWonder
Very quick response thank you StarGate I must of overlooked that, once again thanks.

Re: Variable Converting

Posted: Thu Apr 21, 2011 5:21 am
by IdeasVacuum

Re: Variable Converting

Posted: Fri Apr 22, 2011 9:17 am
by blueznl

Re: Variable Converting

Posted: Mon Apr 25, 2011 8:26 am
by 4RESTER
OneHitWonder wrote:Hi,

I have just started using Purebasic and am finding it really fun and seems like a really nice language. However I am having trouble finding functions to convert strings to integers and other conversion functions.

Can somebody point me in the right direction please?

Thanks alot.
ASCIIZ SignedQuad to SignedQuad from memory:

Code: Select all

Procedure.Q	Get_ASCIIZ_as_SQ64(mem_ADDR.L, strsize.L)
	Protected	SQ64.Q		= 0
	Protected	Minus.B	= 0
!Local		l_next
!Local		l_skip
!Local		l_next1
!Local		l_quit
!Local		l_plus

!           XOR     EDI,EDI     ;LSB
!           XOR     EBX,EBX     ;MSB

!			MOV		ESI,[p.v_mem_ADDR]
!l_next:	MOVZX	EAX,byte [ESI]
!			CMP		AL," "
!			JE		l_skip

!			TEST	AL,AL
!			JE		l_quit

!			CMP		AL,$2D      ;"-"
!			JNE		l_next1
!			MOV		byte [p.v_Minus],1
!			JMP		l_skip

!l_next1:	SUB		EAX,"0"
!           ADD     EDI,EDI
!           ADC     EBX,EBX
!           MOV     ECX,EDI
!           MOV     EDX,EBX
!           ADD     EDI,EDI
!           ADC     EBX,EBX
!           ADD     EDI,EDI
!           ADC     EBX,EBX
!           ADD     EDI,ECX
!           ADC     EBX,EDX
!           ADD     EDI,EAX
!           ADC     EBX,0
!l_skip:	INC     ESI
!           DEC		dword [p.v_strsize]
!			JNZ		l_next

!l_quit:	TEST	byte [p.v_Minus],$FF
!			JZ		l_plus
!			NEG		EDI
!           ADC     EBX,0
!			NEG		EBX

!l_plus:    MOV     dword [p.v_SQ64+0],EDI
!           MOV     dword [p.v_SQ64+4],EBX
ProcedureReturn SQ64.Q
EndProcedure
Examples of using:

Code: Select all

*mem_ADDR = AllocateMemory(1024)

PokeS(*mem_ADDR+000, "123456789012345", 16, #PB_Ascii)
PokeS(*mem_ADDR+100, "-123456789012345", 16, #PB_Ascii)

Debug Str(Get_ASCIIZ_as_SQ64(*mem_ADDR+000, 16))
Debug Str(Get_ASCIIZ_as_SQ64(*mem_ADDR+100, 16))

Re: Variable Converting

Posted: Mon Apr 25, 2011 11:43 am
by einander
Hi 4rester: Nice and fast function.

Is any advantage of using Get_ASCIIZ_as_SQ64() in contrast to using the built-in Val()?

Re: Variable Converting

Posted: Mon Apr 25, 2011 8:48 pm
by 4RESTER
einander wrote:Hi 4rester: Nice and fast function.

Is any advantage of using Get_ASCIIZ_as_SQ64() in contrast to using the built-in Val()?
You can easy check speed of executables any procedure (or piece of codes) by using CPU operation:
RDTSC
that return to EDX:EAX containt of Time-Stamp Counter (TSC) of CPU

Example:

Code: Select all

;=============================================================================
Declare.Q     Get_TSC()
;=============================================================================

Procedure   TESTED_PROC()
!   MOV     CX,$1234
!   LOOP    $
EndProcedure

TSC.Q = Get_TSC()
TESTED_PROC()   ;overhere measured procedure or piece of code
TSC.Q = Get_TSC() - TSC.Q:  Debug "TSC = " + StrU(TSC.Q)



;=============================================================================
Procedure.Q   Get_TSC()
Protected   TSC.Q
!    RDTSC
!    MOV     dword [p.v_TSC+0],EAX
!    MOV     dword [p.v_TSC+4],EDX
ProcedureReturn TSC.Q
EndProcedure
;=============================================================================

Re: Variable Converting

Posted: Mon Apr 25, 2011 8:56 pm
by Trond
You can easy check speed of executables any procedure (or piece of codes) by using CPU operation:
RDTSC
Actually, this instruction can't be expected to give sane values in multitasking operating systems, and especially not in multiprocessor systems (which include most pc's nowadays).

Re: Variable Converting

Posted: Mon Apr 25, 2011 9:25 pm
by 4RESTER
Trond wrote:
You can easy check speed of executables any procedure (or piece of codes) by using CPU operation:
RDTSC
Actually, this instruction can't be expected to give sane values in multitasking operating systems, and especially not in multiprocessor systems (which include most pc's nowadays).
Yes it is.
You can enter values into an array, for example, 100 values, and on the basis of the results obtained by averaging the value.

Somelike this:

Code: Select all

;=============================================================================
Dim TSC.Q(100)
Declare.Q     Get_TSC()
;=============================================================================
Procedure   TESTED_PROC()
!   MOV     CX,$1234
!   LOOP    $
EndProcedure




For i=0 To 99
    TSC.Q(i) = Get_TSC()
    TESTED_PROC()   ;overhere measured procedure or piece of code
    TSC.Q(i) = Get_TSC() - TSC.Q(i)
Next

EXECTICKS.Q = 0
For i=0 To 99
    EXECTICKS.Q = EXECTICKS.Q + TSC.Q(i)
Next
    EXECTICKS.Q = EXECTICKS.Q / 100
Debug "TSC ~ " + StrU(EXECTICKS)
;=============================================================================
Procedure.Q   Get_TSC()
Protected   TSC.Q
!    RDTSC
!    MOV     dword [p.v_TSC+0],EAX
!    MOV     dword [p.v_TSC+4],EDX
ProcedureReturn TSC.Q
EndProcedure
;=============================================================================
Greater accuracy than by RDTSC can not be obtained.

Re: Variable Converting

Posted: Mon Apr 25, 2011 10:08 pm
by 4RESTER
einander wrote:Hi 4rester: Nice and fast function.

Is any advantage of using Get_ASCIIZ_as_SQ64() in contrast to using the built-in Val()?
I wrote this procedure with the ability to explicitly specify the maximum string length (0x00 in a row is premature terminator).
Also characteristic is that the symbols " " (space) right ignored.

This procedure I used to work with records into the files in DBF-format.

This can be used as a template for adapting to other requirements.
For example, if they can be lines starting with a "+ " (type "+12345"), you can easily add to and from these lines:

Was so:

Code: Select all

...
!         CMP      AL," "
!         JE      l_skip
...
Now this:

Code: Select all

...
!         CMP      AL," "
!         JE      l_skip
!         CMP      AL,"+"           ;<=- \ if char "+"
!         JE      l_skip              ;<=- / then skip his treatment
...
Furthermore, the procedure is optimized for P+ processors and running fairly quickly.

Re: Variable Converting

Posted: Mon Apr 25, 2011 10:18 pm
by netmaestro
Talk about hijacking a thread... :?

A beginner asking a basic question about Val() is hardly looking for thirty lines of assembler.

Re: Variable Converting

Posted: Mon Apr 25, 2011 10:26 pm
by 4RESTER
netmaestro wrote:Talk about hijacking a thread... :?

Someone asking a basic question about Val() is hardly looking for thirty lines of assembler.
Questions on converting data has always been and always will be. This is one of the most important in programming. Including adaptation trivial procedures under certain specific goals.

Well write many lines of assembler when compiling to executable machine code is often smaller (and much quicker) than a single line at a high level language :) .