Oxygen Basic
Programming => Problems & Solutions => Topic started by: Frankolinox on February 08, 2014, 01:25:07 AM
-
lalala :)
-
In Basic and Asm:
function Reverse(string s) as string
string t=s
sys le=len t
for i=1 to le
mid t,i,mid s,le-i+1
next
return t
end function
print reverse "abcdefghijklmnopqrstuvwxyz"
function Reverse(string s) as string
string t=s
sys le=len t
addr edi,t
addr esi,s
mov ecx,le
dec ecx
(
mov al,[esi]
mov [edi+ecx],al
inc esi
dec ecx
jge repeat
)
return t
end function
print reverse "abcdefghijklmnopqrstuvwxyz"
And another using byte pointers:
function Reverse(string s) as string
string t=s
sys le=len t
byte a at (strptr s)
byte b at (le-1+strptr t)
for i=1 to le
b=a : @b-- : @a++
next
return t
end function
print reverse "abcdefghijklmnopqrstuvwxyz"
-
Hi Frank, If you mean translation from string to integer, and integer to string:
string to sys
string s="abcd"
sys a
a="0x"+s 'auto-conversion val()
print hex a
and back to string again:
s=hex a
print s
-
Will this do?
'Reading String as array of integers
string s="Oxygen"
long a at (strptr s)
print hex(a[1],8)+" "+hex(a[2],8)
'Reading array of integers as string
zstring z at (@a)
print z
-
Situation will be much better if we can use cast
something like this:
string s="promo"
sys p = *<string>s
print p
ahhh whishes ::)
Hmm Charles
I still don't understand real purpose of this at
-
Recent Oxygen releases also support the equivalent C-like syntax:
long*a=strptr s
'Reading String as array of integers
string s="Oxygen"
long*a=strptr s
print hex(a[1],8)+" "+hex(a[2],8)
'Reading array of integers as string
zstring*z=@a
print z
-
Hmm yes it looks that only solution is zstring *z
Charles
What Free Basic have ...i think something like reinterpret_cast or
something like that?
Is C way *<type>p correct ?
because i am not sure , and i can agree that syntax is little bit strange ...
what you think ?
-
You can cast with round '()' brackets, but it is not necessary to cast here. Oxygen accepts any expression for a pointer in this kind of definition.
-
Cast with brackets ?
How?
-
Integers reverse the order of the 4 ascii codes: thus you will see a reversal in their hex values:
This is due to the x86 processor taking the 1st byte of an integer as being the lowest. This behaviour of the Processor is often termed: little-endian
"abcdefgh" --->
int 1 = 0x68676665 'dcba'
int 2 = 0x6C6B6A69 'hgfe'
You could look at the string as an array of bytes, instead of an array of 32bit integers. It is less confusing, and you will not be troubled by these order effects. :)
s="Oxygen"
byte a at (strptr s)
print hex(a[1],2)+" "+hex(a[2],2)+" "+
+hex(a[3],2)+" "+hex(a[4],2)+" "+
+hex(a[5],2)+" "+hex(a[6],2)+" "
-
Hi Charles,
i see that
long*a=strptr s
is the same as
long a at (strptr s)
am i wright ?
if so we need some doc that shade some light on Oxygen basic , it has may goodies but need to be clear in help file.