|
|
|
|
- Overview
- Why use the Buffer type?
- Buffer type: Syntax and use
- Declaring a Buffer variable
- Declaring and initializing a Buffer variable
- Operations on a Buffer variable
- 'Buffer on' type: Syntax and advanced use
- Declaring a Buffer On variable
- Operations on 'Buffer on' variables (called fixed buffers)
Buffer (Type de variable)
The buffer type corresponds to a binary memory area. Via this type, a code that handles the binary format can be shared between a standard WINDEV application and a WINDEV Mobile application. In WINDEV, a string variable can contain both characters and binary data (an image for example). In WINDEV Mobile, if a character string variable contains binary data, this data may be corrupted (improper conversion for example). To handle binary data, it is recommended to use Buffer variables. The buffer type does not have a specific end marker and allows you to store binary zeros. Two types of variables are available: - Buffer: This type allows you to manage a memory area with a dynamic size: it is automatically adapted to the buffer content.
- Buffer on: This type is used to handle a memory area whose size (in bytes) is defined during the compilation. This is an advanced type used to perform specific operations in memory, to use some Windows APIs.
Remark: Buffer variables are encoded in base64 in JSON and XML. In WINDEV, the following code can be used to handle bytes as binary values:
S is string = Charact(1) + Charact(0)
The corresponding memory area is:
The same code has a different behavior if executed in Ansi or Unicode. To have the same code in ANSI and in UNICODE, the string must be replaced with a buffer. The code becomes:
S is Buffer S[[1]] = 1 S[[2]] = 0
Buffer type: Syntax and use Declaring a Buffer variable - <Variable name> is Buffer
OR- <Variable name 1>, <Variable name 2> are Buffers
Details of syntax | | <Variable name>: | Name of the variable to declare. |
Remark: By default, a Buffer variable is empty. Declaring and initializing a Buffer variable To declare and initialize a Buffer variable, use the following syntax:
<Variable name> is Buffer = [<Hexa value 1>, <Hexa value 2>, ..., <Hexa value N>]
Details of syntax | | <Variable name>: | Name of the variable to declare. | Hexa value | Hexadecimal value. |
Example:
b is Buffer = [0x01, 0x02, 0x03]
Operations on a Buffer variable Buffer variables can be handled in the same way as String variables. To assign a Buffer variable, use one of the following syntaxes: - assigning a string to a Buffer variable:
<Buffer variable name> = <My String>. For example:
MyBuffer is Buffer MyBuffer = "WINDEV is great" // In ANSI, MyBuffer contains: WINDEV is great // In UNICODE, MyBuffer contains: // W0i0n0D0e0v0 0i0s0 0gr0e0a0t0
- assigning a byte of the Buffer variable:
<Buffer variable name> [[<Byte index>]] = <Byte ASCII code>. For example:
MyBuffer is Buffer MyBuffer[[1]] = 65 // MyBuffer contains "A"
Using functions: - Left, Right and Middle can be used on Buffer variables. For more details, see the help about these functions.
Not available. - Length is used to find out the real size of the data contained in the Buffer variable (in bytes).
Using operators: The [[ ]] operator is used to access: - one byte of the Buffer variable. For example:
MyBuffer is Buffer MyBuffer = "WINDEV is great" MyBuffer[[1]] = "W"
- one part of the Buffer variable. For example:
MyBuffer is Buffer MyBuffer = "WINDEV is great" Info(MyBuffer[[8 TO 15]])
Buffer comparison:You can compare a buffer or a portion of a buffer to specific values. For example, you can write:
IF MyBuffer = [1,2,3] THEN ...
IF MyBuffer[1 ON 2] = [1,2] THEN ...
'Buffer on' type: Syntax and advanced use Declaring a Buffer On variable
<Variable name> is Buffer on <Buffer size>
OR
<Variable name 1>, <Variable name 2> are Buffers on <Size of buffers>
Details of syntax | | <Variable name>: | Name of the variable to declare. | <Buffer size>: | Size of buffer in bytes. |
Remarks: - The a and an keywords are optional: they provide better readability.
- By default, the '0' character is assigned to a 'Buffer on' variable.
The 'Buffer on' type is not available.
Examples:
MyBuffer is Buffer on 4 MyBuffer = "ABCDE" // MyBuffer contains "ABCD" MyBuffer = "ZZ" // MyBuffer contains "ZZCD"
Operations on 'Buffer on' variables (called fixed buffers) Fixed buffers are handled like simple buffers. However, some differences must be taken into account. At runtime, the actual size of the data contained in the Buffer variable is unknown: - The data written is truncated if it exceeds the size of the Buffer variable.
- When writing data that is smaller than the size of the Buffer variable, the non-written section of the buffer keeps its previous value.
To handle this type of Buffer variable, it is recommended to always store the actual buffer size in an Integer variable. Reminder: the Buffer type automatically manages its own size. When using a fixed buffer, we advise you to quickly copy its value into an automatic buffer.
// Use an API that returns the size of a buffer bFixedBuffer is Buffer on 200 nSize is int // Call the API nSize = API(<API name>, <Parameters>, bFixedBuffer, 200) // Copy the buffer bBuffer is Buffer bBuffer = Left(bFixedBuffer, nSize)
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|