LEN: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The LEN function returns the number of bytes used by a variable value and the number of characters in a STRING. {{PageSyntax}} : {{Parameter|length%}} = LEN({{Parameter|literalTextOrVariable}}) * Literal or variable STRING values return the number of string bytes which is the same as the number of string characters. * A numerical ''variable'' will return the number of bytes used by a numerical variable type. ** _BYTE variable types return 1 byte....")
 
No edit summary
Line 15: Line 15:
** [[_FLOAT]] variable types return 32 bytes.
** [[_FLOAT]] variable types return 32 bytes.
** [[_OFFSET]] and [[_MEM]] variable types return varying byte sizes.
** [[_OFFSET]] and [[_MEM]] variable types return varying byte sizes.
** ''Note:'' [[_BIT]] variable types and bit multiples '''cannot be measured in bytes'''.  
** ''Note:'' [[_BIT]] variable types and bit multiples '''cannot be measured in bytes'''.
* '''LEN cannot return lengths of literal numerical values and will create a "variable required" status error in the IDE.'''
* '''LEN cannot return lengths of literal numerical values and will create a "variable required" status error in the IDE.'''
* '''LEN =''' can be used with a user defined [[TYPE]] variable to determine the number of bytes used in [[RANDOM]] file records:
* '''LEN =''' can be used with a user defined [[TYPE]] variable to determine the number of bytes used in [[RANDOM]] file records:
:::: {{InlineCode}}[[OPEN]] file$ FOR [[RANDOM]] AS #n LEN <nowiki>=</nowiki> LEN(recordTypeVariable){{InlineCodeEnd}}'''  
:::: {{InlineCode}}[[OPEN]] file$ FOR [[RANDOM]] AS #n LEN <nowiki>=</nowiki> LEN(recordTypeVariable){{InlineCodeEnd}}'''
:* If a LEN = statement is not used, [[RANDOM]] default record length is 128 or sequencial is 512 up to a maximum of 32767 bytes.  
:* If a LEN = statement is not used, [[RANDOM]] default record length is 128 or sequencial is 512 up to a maximum of 32767 bytes.
:* [[BINARY]] OPEN statements will ignore LEN = statements. The byte size of a [[GET|read]] or [[PUT|write]] is determined by the [[Variable Types|variable type]].
:* [[BINARY]] OPEN statements will ignore LEN = statements. The byte size of a [[GET|read]] or [[PUT|write]] is determined by the [[Variable Types|variable type]].


Line 27: Line 27:
{{CodeStart}}
{{CodeStart}}
LastName$ = "Williams"
LastName$ = "Williams"
PRINT {{Cl|LEN}}(LastName$); "bytes" '' ''
PRINT {{Cl|LEN}}(LastName$); "bytes"
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}} 8 bytes
{{OutputStart}} 8 bytes
Line 33: Line 33:




''Example 2:'' Testing [[INPUT]] for numerical [[STRING]] entries from a user.  
''Example 2:'' Testing [[INPUT]] for numerical [[STRING]] entries from a user.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|INPUT}} "number: ", num$
{{Cl|INPUT}} "number: ", num$


Line 40: Line 40:
L = {{Cl|LEN}}(value$)
L = {{Cl|LEN}}(value$)


{{Cl|PRINT}} {{Cl|LEN}}(num$), L '' ''
{{Cl|PRINT}} {{Cl|LEN}}(num$), L
{{CodeEnd}}
{{CodeEnd}}
: ''Note:'' [[&H]], [[&O]], D and E will also be accepted as numerical type data in a [[VAL]] conversion, but will add to the entry length.
: ''Note:'' [[&H]], [[&O]], D and E will also be accepted as numerical type data in a [[VAL]] conversion, but will add to the entry length.
Line 46: Line 46:


''Example 3:'' With numerical value types you MUST use a variable to find the inherent byte length when using LEN.
''Example 3:'' With numerical value types you MUST use a variable to find the inherent byte length when using LEN.
{{CodeStart}} '' ''
{{CodeStart}}
DIM I AS INTEGER
DIM I AS INTEGER
PRINT "INTEGER ="; LEN(I); "bytes"
PRINT "INTEGER ="; LEN(I); "bytes"
Line 58: Line 58:
PRINT "DOUBLE ="; LEN(D); "bytes"
PRINT "DOUBLE ="; LEN(D); "bytes"
DIM F AS _FLOAT
DIM F AS _FLOAT
PRINT "_FLOAT ="; LEN(F); "bytes" '' ''
PRINT "_FLOAT ="; LEN(F); "bytes"
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}INTEGER = 2 bytes
{{OutputStart}}INTEGER = 2 bytes
Line 70: Line 70:


''Example 4:'' Opening a RANDOM file using LEN to calculate and LEN = to designate the file record size.
''Example 4:'' Opening a RANDOM file using LEN to calculate and LEN = to designate the file record size.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|TYPE}} variabletype
{{Cl|TYPE}} variabletype
   x {{Cl|AS}} {{Cl|INTEGER}}'      '2 bytes
   x {{Cl|AS}} {{Cl|INTEGER}}'      '2 bytes
Line 100: Line 100:
{{Cl|PRINT}} newrec.x, newrec.y, newrec.z
{{Cl|PRINT}} newrec.x, newrec.y, newrec.z


{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}} 16 bytes
{{OutputStart}} 16 bytes

Revision as of 01:56, 23 January 2023

The LEN function returns the number of bytes used by a variable value and the number of characters in a STRING.


Syntax

length% = LEN(literalTextOrVariable)


  • Literal or variable STRING values return the number of string bytes which is the same as the number of string characters.
  • A numerical variable will return the number of bytes used by a numerical variable type.
    • _BYTE variable types return 1 byte.
    • INTEGER variable types return 2 bytes.
    • SINGLE and LONG integer variable types return 4 bytes.
    • DOUBLE and _INTEGER64 variable types return 8 bytes.
    • _FLOAT variable types return 32 bytes.
    • _OFFSET and _MEM variable types return varying byte sizes.
    • Note: _BIT variable types and bit multiples cannot be measured in bytes.
  • LEN cannot return lengths of literal numerical values and will create a "variable required" status error in the IDE.
  • LEN = can be used with a user defined TYPE variable to determine the number of bytes used in RANDOM file records:
OPEN file$ FOR RANDOM AS #n LEN = LEN(recordTypeVariable)
  • If a LEN = statement is not used, RANDOM default record length is 128 or sequencial is 512 up to a maximum of 32767 bytes.
  • BINARY OPEN statements will ignore LEN = statements. The byte size of a read or write is determined by the variable type.


Examples

Example 1: With a string variable the byte size is the same as the number of characters.

LastName$ = "Williams"
PRINT LEN(LastName$); "bytes"
 8 bytes


Example 2: Testing INPUT for numerical STRING entries from a user.

INPUT "number: ", num$

value$ = LTRIM$(STR$(VAL(num$)))
L = LEN(value$)

PRINT LEN(num$), L
Note: &H, &O, D and E will also be accepted as numerical type data in a VAL conversion, but will add to the entry length.


Example 3: With numerical value types you MUST use a variable to find the inherent byte length when using LEN.

DIM I AS INTEGER
PRINT "INTEGER ="; LEN(I); "bytes"
DIM L AS LONG
PRINT "LONG ="; LEN(L); "bytes"
DIM I64 AS _INTEGER64
PRINT "_INTEGER64 ="; LEN(I64); "bytes"
DIM S AS SINGLE
PRINT "SINGLE ="; LEN(S); "bytes"
DIM D AS DOUBLE
PRINT "DOUBLE ="; LEN(D); "bytes"
DIM F AS _FLOAT
PRINT "_FLOAT ="; LEN(F); "bytes"
INTEGER = 2 bytes
LONG = 4 bytes
_INTEGER64 = 8 bytes
SINGLE = 4 bytes
DOUBLE = 8 bytes
_FLOAT = 32 bytes


Example 4: Opening a RANDOM file using LEN to calculate and LEN = to designate the file record size.

TYPE variabletype
  x AS INTEGER'       '2 bytes
  y AS STRING * 10'  '10 bytes
  z AS LONG'          '4 bytes
END TYPE'            '16 bytes total
DIM record AS variabletype
DIM newrec AS variabletype

file$ = "testrand.inf" '<<<< filename may overwrite existing file
number% = 1 '<<<<<<<<<< record number to write cannot be zero
RecordLEN% = LEN(record)
PRINT RecordLEN%; "bytes"
record.x = 255
record.y = "Hello world!"
record.z = 65535
PRINT record.x, record.y, record.z

OPEN file$ FOR RANDOM AS #1 LEN = RecordLEN%
PUT #1, number% , record 'change record position number to add records
CLOSE #1

OPEN file$ FOR RANDOM AS #2 LEN = RecordLEN%
NumRecords% = LOF(2) \ RecordLEN%
PRINT NumRecords%; "records"

GET #2, NumRecords% , newrec 'GET last record available
CLOSE #2
PRINT newrec.x, newrec.y, newrec.z

END
 16 bytes
 255        Hello worl       65535
 1 records
 255        Hello worl       65535
Explanation: The byte size of the record TYPE determines the LOF byte size of the file and can determine the number of records.
To read the last record GET the number of records. To add a record, use the number of records + 1 to PUT new record data.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link