cbuf
copy to buffer - allows data to be copied directly to a buffer data type.
syntax
cbuf(argument [, offset])
- argument can be any numeric or alpha-numeric data type
- offset is optional; if specified it sets the offset into the buffer where to store the data
See also clbuf, pbuf, buffer type, record
details
cbuf works with any numeric and alpha-numeric data type. The result must be assigned to a buffer. The function can only be used in assignment statements; it cannot be part of an expression or passed as a parameter. If the optional offset is specified, the converted data will be stored in the buffer starting at the offset position. Offset positions are zero-based. Copying data to a buffer is a low-level operation whereby data types are not preserved. cbuf is useful if you want to place data in a buffer at a specific offset. Otherwise, direct assignment to a buffer is the better choice.
examples
This example clears a buffer with dashes (ascii code 45). It then copies a string to a buffer, storing the string at offset 5. Finally, an immediate string is copied to the buffer and stored at offset 7.
' SharpBASIC copy string to buffer programming example
' ----------------------------------------------------
option strict;
incl "lib/sys.sbi";
' declare a string and a buffer
dim text: str;
dim buffer: byte * 16;
main do
' clear buffer
clbuf(buffer, 45);
' define the string
text = "A new BASIC";
' copy string to buffer, position at offset 5 (0-based)
buffer = cbuf(text, 5);
' buffers can be printed
print(buffer);
' store "fun" at position 7
buffer = cbuf("fun", 7);
print(buffer);
end
Output:
-----A new BASIC
-----A fun BASIC
This example copies a signed 8-bit integer to a 1-byte buffer and then retrieves the data from the buffer, storing it as an unsigned 8-bit integer.
' SharpBASIC numeric-buffer conversion programming example
' --------------------------------------------------------
option strict;
incl "lib/sys.sbi";
dim oneByte: byte * 1;
dim b_int8: int8;
dim b_uint8: uint8;
main do
' assign -1 to signed 8-bit integer
b_int8 = -1;
'copy to 1-byte buffer
oneByte = cbuf(b_int8);
' retrieve the byte from the buffer as unsigned 8-bit integer
b_uint8 = pbuf(oneByte);
print(b_uint8);
end
Output:
255