dim as string n2 = "101100"
print n2
swap mid( n2 , 5 , 2 ) , mid( n2 , 1 , 2 )
print n2
The swap errors , the compiler can't swap mid sequences.. I don't know if its a compiler error or just bad code??
Maybe it would be a "Feature Request"?
Swapping bits
Re: Swapping bits
Technically speaking, you want to swap bytes in a string, not bits. It can be done, of course:
Output:
Code: Select all
dim as string n2 = "101100011111111abc"
print n2
asm
mov eax, [n2] ' get string address
mov edx, eax ' make a copy
lbl1:
mov cl, [eax]
push ecx
inc eax
test cl, cl
jne lbl1
pop ecx ' discard nullbyte
lbl2:
pop ecx ' get last valid byte
mov [edx], cl ' move to start of string
inc edx
cmp eax, edx
ja lbl2
push ecx ' correct the stack
end asm
'swap mid( n2 , 5 , 2 ) , mid( n2 , 1 , 2 )
print n2
Sleep
Code: Select all
101100011111111abc
cba111111110001101
Re: Swapping bits
Of course it errs. What exactly are you swapping there? The results of two functions? How is that meaningful in any way?newby12 wrote:...
The swap errors , the compiler can't swap mid sequences.. I don't know if its a compiler error or just bad code??
...
Re: Swapping bits
That does not work because the MID (Function) returns by value and not by reference.
Instead, you can swap byte elements inside the string characters by using the Operator [] (String index) which returns by reference:
Instead, you can swap byte elements inside the string characters by using the Operator [] (String index) which returns by reference:
Code: Select all
dim as string n2 = "101100"
print n2
'swap mid( n2 , 5 , 2 ) , mid( n2 , 1 , 2 )
swap n2[4], n2[0]
swap n2[5], n2[1]
print n2