Variable Converting
-
OneHitWonder
- New User

- Posts: 9
- Joined: Wed Apr 20, 2011 11:33 pm
Variable Converting
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.
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
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 more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
-
OneHitWonder
- New User

- Posts: 9
- Joined: Wed Apr 20, 2011 11:33 pm
Re: Variable Converting
Very quick response thank you StarGate I must of overlooked that, once again thanks.
-
IdeasVacuum
- Always Here

- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Variable Converting
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: Variable Converting
http://www.xs4all.nl/~bluez/purebasic/p ... _variables
http://www.xs4all.nl/~bluez/purebasic/p ... .htm#chars
http://www.xs4all.nl/~bluez/purebasic/p ... .htm#chars
( 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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Re: Variable Converting
ASCIIZ SignedQuad to SignedQuad from memory: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.
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
EndProcedureCode: 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... 

Re: Variable Converting
Hi 4rester: Nice and fast function.
Is any advantage of using Get_ASCIIZ_as_SQ64() in contrast to using the built-in Val()?
Is any advantage of using Get_ASCIIZ_as_SQ64() in contrast to using the built-in Val()?
Re: Variable Converting
You can easy check speed of executables any procedure (or piece of codes) by using CPU operation: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()?
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... 

Re: Variable Converting
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).You can easy check speed of executables any procedure (or piece of codes) by using CPU operation:
RDTSC
Re: Variable Converting
Yes it is.Trond wrote: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).You can easy check speed of executables any procedure (or piece of codes) by using CPU operation:
RDTSC
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
;=============================================================================
See ya... 

Re: Variable Converting
I wrote this procedure with the ability to explicitly specify the maximum string length (0x00 in a row is premature terminator).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()?
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
...
Code: Select all
...
! CMP AL," "
! JE l_skip
! CMP AL,"+" ;<=- \ if char "+"
! JE l_skip ;<=- / then skip his treatment
...
Last edited by 4RESTER on Mon Apr 25, 2011 10:20 pm, edited 2 times in total.
See ya... 

- netmaestro
- PureBasic Bullfrog

- Posts: 8452
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Variable Converting
Talk about hijacking a thread... 
A beginner asking a basic question about Val() is hardly looking for thirty lines of assembler.
A beginner asking a basic question about Val() is hardly looking for thirty lines of assembler.
BERESHEIT
Re: Variable Converting
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.netmaestro wrote:Talk about hijacking a thread...
Someone asking a basic question about Val() is hardly looking for thirty lines of assembler.
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... 



