Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Strg_var = Left(Str_var , 0) Problem

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 375
Location: Wuppertal, Germany

germany.gif
PostPosted: Thu Jul 03, 2014 8:38 am    Post subject: Strg_var = Left(Str_var , 0) Problem Reply with quote

Hi,

I expect following to return an empty string:

Strg_var = "123"
Strg_var = Left(Str_var , 0)

But indeed it retutns Strg_var to be "1", just the same as

Strg_var = "123"
Strg_var = Left(Str_var , 1)

Is there a reason for that? The HELP says nothing about that...

Backround:
I shorten a string by receiving BACKSPACE characters, even down to empty the string.
Sure there are workarounds, but I wonder if LEFT works propper here.

(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5096
Location: Holland

blank.gif
PostPosted: Thu Jul 03, 2014 9:05 am    Post subject: Reply with quote

when you use a 0, it is in effect 255.

if you want a 0 check, you can change the code as below in mcs.lib:

[_COPYSTRING]
$EXTERNAL _ADD_NULL
; number of chars to copy in R16
; used by left and right.
; IMPORTANT DO NOT MOVE, should be under CopyMidString code
_CopyString:
TST r16 ; check for 0
BREQ _CopyStringExit ; when 0 bytes to copy exit
_CopyString1:
Ld _temp1,Z+ ; load byte
St X+,_temp1 ; store byte
Dec R16 ; decrease counter
Breq _CopyStringExit ;ready
Tst R24 ; is character zero
Brne _CopyString1 ; no,next byte
Ret ; was zero so null is added, could remove this line
_CopyStringExit:
Rjmp _Add_Null ; add null and exit
[END]

_________________
Mark
Back to top
View user's profile Visit poster's website
Deanus

Bascom Member



Joined: 26 May 2006
Posts: 188
Location: Adelaide

australia.gif
PostPosted: Thu Jul 03, 2014 9:50 am    Post subject: Reply with quote

Hi Arera,
If you are removing characters with back space, wouldn't it be better to

varnum = Len(Strg_var0)

then when you have no characters in the string Varnum would equal zero.

Regards

Dean
Back to top
View user's profile
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 375
Location: Wuppertal, Germany

germany.gif
PostPosted: Thu Jul 03, 2014 1:06 pm    Post subject: Reply with quote

Hi Mark,

thanks for your code, but I will never ever coax in the mcs.lib!

@Deanus: to dermin the lenght does not shorten the string!

The workaround is simple, but still the behavior of LEFT makes no sense to me...
That's how I do it now (watch the workaround-line):

If Asc(chr_read_conf) = &H08 Then 'Backspace received
If Len(config_in) > 0 Then 'only shorten if characters left
N = Len(config_in) - 1 'calculate new lenght
Config_in = Left(config_in , N) 'works, exept for N = 0
If N = 0 Then Config_in = "" 'WORKAROUND
Result = Tcpwrite(terminal_socket , "{008}") 'echo BS to terminal
End If
End If
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5096
Location: Holland

blank.gif
PostPosted: Thu Jul 03, 2014 7:57 pm    Post subject: Reply with quote

if you do not want to change the lib you can do this too :
- create a new file with bascom or notepad , for example mylibcode.lib
- paste the code into this file
- save the file in the bascom LIB folder
- include the file to your code with $lib "mylibcode.lib"

now the code from your lib will be used.

_________________
Mark
Back to top
View user's profile Visit poster's website
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 375
Location: Wuppertal, Germany

germany.gif
PostPosted: Thu Jul 03, 2014 8:10 pm    Post subject: Reply with quote

Thank you Mark for your effort,
But my one-line workround does the job.
And doesn't the msc.lib change on updates of bascom?

Marc
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum