|
|
|
|
- Why use the Buffer type?
- Assigning a Buffer variable
- Using functions
- Operators
- Buffer comparison
- Manipulating 'Buffer of' variables (called fixed buffers)
Buffer (Type de variable) In french: Buffer
The Buffer type corresponds to a binary memory area. This type allows for full portability of code manipulating binary data 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 string variable contains binary data, this data may be corrupted (due to conversion errors, for example). To manipulate 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 of: This type is used to manipulate a memory area whose size (in bytes) is set at compile time. This is an advanced type used to perform specific operations in memory and 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
Syntax
Declaring a Buffer variable Hide the details
<Variable name> is Buffer
<Variable name 1>, <Variable name 2> are Buffers
<Variable name>: Name of the variable to declare. By default, a Buffer variable is empty.
Declaring and initializing a Buffer variable Hide the details
<Variable name> is Buffer = [<Hex value 1>, <Hex value 2>, ..., <Hex value N>]
<Variable name>: Name of the variable to declare. <Hexa value>: Hexadecimal value. Example: b is Buffer = [0x01, 0x02, 0x03]
Declaring a 'Buffer of' variable Hide the details
<Variable name> is Buffer of <Buffer size>
<Variable name 1>, <Variable name 2> are Buffers of <Size of buffers>
<Variable name>: Name of the variable to declare. By default, a Buffer variable is empty. <Buffer size>: Buffer size expressed in bytes. By default, the '0' character is assigned to a 'Buffer of' variable.
Examples: MyBuffer is Buffer of 4
MyBuffer = "ABCDE"
MyBuffer = "ZZ"
Remarks Assigning a Buffer variable 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"
- 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.
- Length is used to find out the real size of the data contained in the Buffer variable (in bytes).
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 ... Manipulating 'Buffer of' variables (called fixed buffers) Fixed buffers are manipulated in the same way as standard buffers. However, some differences should be noted. 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 to an automatic buffer. // Use an API that returns the size of a buffer bFixedBuffer is Buffer of 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…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|