Variable Converting

Just starting out? Need help? Post your questions and find answers here.
OneHitWonder
New User
New User
Posts: 9
Joined: Wed Apr 20, 2011 11:33 pm

Variable Converting

Post 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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2265
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Variable Converting

Post by STARGÅTE »

PureBasic - String

Val()
ValF()
...
Str()
StrF()
...
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
OneHitWonder
New User
New User
Posts: 9
Joined: Wed Apr 20, 2011 11:33 pm

Re: Variable Converting

Post by OneHitWonder »

Very quick response thank you StarGate I must of overlooked that, once again thanks.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Variable Converting

Post by IdeasVacuum »

IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Variable Converting

Post by blueznl »

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: Variable Converting

Post 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))
See ya... Image
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Re: Variable Converting

Post 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()?
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: Variable Converting

Post 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
;=============================================================================
See ya... Image
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Variable Converting

Post 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).
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: Variable Converting

Post 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.
See ya... Image
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: Variable Converting

Post 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.
Last edited by 4RESTER on Mon Apr 25, 2011 10:20 pm, edited 2 times in total.
See ya... Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Variable Converting

Post by netmaestro »

Talk about hijacking a thread... :?

A beginner asking a basic question about Val() is hardly looking for thirty lines of assembler.
BERESHEIT
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: Variable Converting

Post 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 :) .
See ya... Image
Post Reply