Author Topic: string literal adress question  (Read 3286 times)

0 Members and 1 Guest are viewing this topic.

Frankolinox

  • Guest
string literal adress question
« on: February 08, 2014, 01:25:07 AM »
lalala :)
« Last Edit: October 01, 2014, 08:40:56 AM by Frankolinox »

Charles Pegge

  • Guest
Re: string literal adress question
« Reply #1 on: February 08, 2014, 04:04:38 AM »
In Basic and Asm:

Code: [Select]

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:

Code: [Select]
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"

« Last Edit: February 08, 2014, 04:25:56 AM by Charles Pegge »

Charles Pegge

  • Guest
Re: string literal adress question
« Reply #2 on: February 10, 2014, 02:16:57 AM »
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


« Last Edit: February 10, 2014, 02:30:14 AM by Charles Pegge »

Charles Pegge

  • Guest
Re: string literal adress question
« Reply #3 on: February 10, 2014, 09:26:23 AM »
Will this do?

Code: [Select]
'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


Aurel

  • Guest
Re: string literal adress question
« Reply #4 on: February 10, 2014, 11:36:32 AM »
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

Charles Pegge

  • Guest
Re: string literal adress question
« Reply #5 on: February 10, 2014, 01:54:41 PM »
Recent Oxygen releases also support the equivalent C-like syntax:

long*a=strptr s

Code: [Select]
'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

Aurel

  • Guest
Re: string literal adress question
« Reply #6 on: February 10, 2014, 10:50:46 PM »
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 ?

Charles Pegge

  • Guest
Re: string literal adress question
« Reply #7 on: February 10, 2014, 11:09:11 PM »
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.

Aurel

  • Guest
Re: string literal adress question
« Reply #8 on: February 10, 2014, 11:13:22 PM »
Cast with brackets ?
How?

Charles Pegge

  • Guest
Re: string literal adress question
« Reply #9 on: February 11, 2014, 10:15:04 AM »
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. :)

Code: [Select]
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)+" "
« Last Edit: February 11, 2014, 10:34:03 AM by Charles Pegge »

Emil_halim

  • Guest
Re: string literal adress question
« Reply #10 on: February 12, 2014, 08:58:51 AM »
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.