Wednesday, May 25, 2011

Bascom and AVR, Strings

Strings are a type of variable where space is reserved for a series (string) of character. It is typically used when we want to input/output text or long numbers. A string is dimensioned as:
Dim Usermessage as String*10

Usermessage can hold a text of 10 characters. Each character takes a storage space of one byte. Bascom will add an extra byte with value 'null' to signal the end of the string. The maximum length of a string is 254 characters (exluding the 'null' byte).

Bascom has a large repertoire for string handling:




Bin  Converts a bytevalue to a binary string

Example:
Dim Portstate as String*8
PortD= 152
Portstate = Bin(PortD)
'Portstate = "10011000"





Format  Format Inpstring according to format string

Example:
Dim Inpstring as String*6
Dim Pstring as String*6
Inpstring = "38.869"
Pstring = Format(Inpstring, "+000.00")
'Pstring = "+038.86"





Fusing  Format a single variable according to a format string, do rounding if necessary

Example:
Dim Xval as Single
Dim Pstring as String*4
Xval = 16.379
Pstring = Fusing(Xval, "format")
Pstring = "16.4"





Hex  Convert a hex number to a string

Example:
Dim Hval as Integer
Hval = 12345
Pstring = Hex(Hval)
'Pstring = "3039"





Hexval  Convert a hex string to a number

Example:
Dim Dval as Integer
Dim Instring as String*2
Instring = "10"
Dval = Hexval(Instring)
'Dval = 16





Instr  Determine the position of a substring in a string

Example:
Dim Complstring as String*13
Dim Substring as String*6
Dim Pval as Integer
Complstring = "bascom course"
Substring = "course"
Pval = Instr(Complstring, Substring)
'Pval = 8





Lcase  Convert a string to all lower case

Example:
Dim Instring as String*13
Dim Lowstring as String*13
Instring = "Bascom Course"
Lowstring = Lcase(Instring)
'Lowstring = "bascom course"





Left  Get the left part of a string

Example:
Dim Instring as String*13
Dim Leftstring as String*3
Dim Pval as Integer
Instring = "Bascom Cursus"
Pval = 3
Leftstring = Left(Instring, Pval)
'Leftstring = "Bas"





Len Get the length of the string

Example:
Dim Instring as String*20
Dim Lval as Integer
Instring = "Bascom tasks"
Lval = Len(Instring)
'Lval = 12





Ltrim  Remove leading spaces from a string

Example:
Dim Instring as String*10
Dim Pstring as String*10
Instring = " course"
Pstring = Ltrim(Instring)
Pstring = "course"





Mid  Get the middle part of a string [FUNCTION]

Example:
Dim Instring as String*13
Dim Pstring as String*3
Dim Sval as Integer
Dim Nval as Integer
Instring = "Bascom Course"
Sval = 4
Nval = 3
Pstring = Mid(Instring, Sval, Nval)
'Pstring = "com"





Mid Replace a substring in a string [ROUTINE]

Example:
Dim Instring as String*13
Dim Pstring as String*3
Instring = "Bascom is not easy"
Pstring = "very"
Sval = 10
Pval = 4
Mid(Pstring, Sval, Nval) = Instring
'Instring = "Bascom is very easy"





Right  Get the right part of a string

Example:
Dim Instring as String*13
Dim Rightstring as String*3
Dim Pval as Integer
Instring = "Bascom Course"
Pval = 3
Rightstring = Right(Instring, Nval)
'Rightstring = "rse"





Rtrim  Remove trailing spaces from a string

Example:
Dim Instring as String*10
Dim Pstring as String*10
Instring = " course "
Pstring = Rtrim(Instring)
'Pstring = " course"





Space  Make a string of spaces

Example:
Dim Pstring as String*10
Dim Nval as Integer
Nval = 4
Pstring = Space(Nval)
'Pstring = " "





Str  Convert a number to a string

Example:
Dim Pstring as String*10
Dim Varval as Integer
Varval = 15436
Pstring = Str(Varval)
'Pstring = "15436"





String  Make a string by repeating characters

Example:
Dim Pstring as String*10
Dim Nchar as Integer
Dim Charcode as Byte
Nchar = 6
Charcode = 88
Pstring = String(Nchar, Charcode)
'Pstring = "XXXXX"





Trim Remove leading and trailing spaces from a string

Example:
Dim Instring as String*10
Dim Pstring as String*10
Instring = " course "
Pstring = Trim(Instring)
'Pstring = "course"





Ucase  Convert a string to all upper case

Example:
Dim Instring as String*13
Dim Upstring as String*13
Instring = "Bascom Course"
Upstring = Ucase(Instring)
'Upstring = "BASCOM COURSE"





Val  Convert a string to a number

Example:
Dim Instring as String*10
Dim Varval as Integer
Instring = " 781"
Varval = Val(Instring)
'Varval = 781

No comments:

Post a Comment

Terima kasih atas komentar yang anda sampaikan , sehingga dapat menambah wawasan saya sebagai penulis dan membuat blog ini semakin berguna banyak orang