The Bytes Package provides a set of functions for creating, inserting data into, and extracting data from instances of the Bytes datatype.
The functions offered here support the creation of a Bytes (Allocate() and NewFromString()), storing values into a Bytes (various Put functions), and retrieving values from a Bytes (various Get functions). The values supported are the following:
Integer InPlace
The flag constant indicating that Put operations should modify the passed Bytes instead of a returned copy.
SwapInteger Swap
The flag constant indicating that Put and Get operations should swap bytes, if necessary, to maintain PC byte-order (little endian) compatibility.
AllocateBytes Allocate( Integer size )
Allocates a new Bytes of the specified size.
size | - | The size, in bytes, |
The Bytes allocated will be filled with zero values. See Bytes.GetBytes() for an example.
Bytes GetBytes( Bytes b, Integer startidx, Integer len )
Returns a subrange of the bytes in b from index startidx of length len as a new Bytes.
b | - | The Bytes from which a subrange is extracted. |
startidx | - | The starting index from which to extract the subrange. |
len | - | The length of the subrange to extract. |
Note that startidx and len are not necessarily bounds-checked, meaning that it is the responsibility of the caller to ensure that a valid subrange is specified. See Bytes.GetLBytes() and Bytes.GetPBytes(). Here is a short example:
Bytes b = Bytes.Allocate( 10 ) Integer i for ( i = 0; i < 10; i += 1 ) Bytes.PutI1( b, i, i, Bytes.InPlace ) end Echo( "b = ", b, "; b[2..4] = ", Bytes.GetBytes( b, 2, 4 ) )
The output of the example is:
b = [00010203040506070809]; b[2..4] = [02030405]
String GetCString( Bytes b, Integer startidx, [Integer flags] )
Extracts a C-style null-terminated String from the Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
- | The index in the Bytes of the value to extract. | |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format (which currently has no effect). |
Integer GetI1( Bytes b, Integer startidx, [Integer flags] )
Returns a single byte signed integer from the specified Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
See Bytes.PutI1() for an example.
Integer GetI2( Bytes b, Integer startidx, [Integer flags] )
Returns a 2-byte signed integer from the specified Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
Integer GetI4( Bytes b, Integer startidx, [Integer flags] )
Returns a 4-byte signed integer from the specified Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
Bytes GetLBytes( Bytes b, Integer startidx, [Integer flags] )
Returns a new Bytes extracted from the specified Bytes at index startidx, which points to a 4-byte signed integer indicating the number of following bytes to extract.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
String GetLString( Bytes b, Integer startidx, [Integer flags] )
Returns a new String extracted from the specified Bytes at index startidx, which points to a 4-byte signed integer indicating the number of following bytes to extract as the String.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
Bytes GetPBytes( Bytes b, Integer startidx, [Integer flags] )
Returns a new Bytes extracted from the specified Bytes as Pascal bytes at index startidx, which points to a 4-byte unsigned integer indicating the number of following bytes to extract.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
String GetPString( Bytes b, Integer startidx, [Integer flags] )
Returns a new String extracted from the specified Bytes as a Pascal String at index startidx, which points to a 4-byte unsigned integer indicating the number of following bytes to extract as the String.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
Point GetPoint( Bytes b, Integer startidx, [Integer flags] )
Returns a new Point extracted from the Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
Real GetR4( Bytes b, Integer startidx, [Integer flags] )
Returns a 4-byte real (floating point value) from the specified Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
Real GetR8( Bytes b, Integer startidx, [Integer flags] )
Returns an 8-byte real (floating point value) from the specified Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
String GetString( Bytes b, Integer startidx, [Integer maxlen], [Integer flags] )
Returns a String extracted from the Bytes at the given index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
maxlen | - | If specified, the maximum String length to return. |
flags | - | Not used. |
Integer GetU1( Bytes b, Integer startidx, [Integer flags] )
Returns a single byte unsigned integer from the specified Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
Integer GetU2( Bytes b, Integer startidx, [Integer flags] )
Returns a 2-byte unsigned integer from the specified Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
Integer GetU4( Bytes b, Integer startidx, [Integer flags] )
Returns a 4-byte unsigned integer from the specified Bytes at the specified index.
b | - | The Bytes from which the value is extracted. |
startidx | - | The index in the Bytes of the value to extract. |
flags | - | If specified, Bytes.Swap to retrieve the value maintaining PC byte-compatible format. |
Note OScript's Integer values are always signed, so on platforms where integer values are 32-bits native, Bytes.GetU4() will return the same value as Bytes.GetI4().
Bytes NewFromString( String s )
Creates and returns a new Bytes containing the given String.
s | - | The String to add to the new Bytes. |
Bytes PutBytes( Bytes b, Integer startidx, Bytes val, [Integer flags] )
Puts the entire, exact contents of the specified Bytes, val, into b.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The Bytes value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
See Bytes.PutLBytes() and Bytes.PutPBytes().
Bytes PutCString( Bytes b, Integer startidx, String val, [Integer flags] )
Puts the specified String into the specified Bytes, terminating the String with a single null byte as is commonly done with C language strings.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutI1( Bytes b, Integer startidx, Integer val, [Integer flags] )
Puts the given Integer into the Bytes as a signed, single-byte value.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Here is a short example:
Bytes b = Bytes.Allocate( 10 ) Bytes.PutI1( b, 3, 2, Bytes.InPlace ) Echo( "b = ", b, "; b[3] = ", Bytes.GetI1( b, 3 ) )
The output of the example is:
b = [00000002000000000000]; b[3] = 2
Bytes PutI2( Bytes b, Integer startidx, Integer val, [Integer flags] )
Puts the given Integer into the Bytes as a signed, 2-byte value.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutI4( Bytes b, Integer startidx, Integer val, [Integer flags] )
Puts the given Integer into the Bytes as a signed, 4-byte value.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutLBytes( Bytes b, Integer startidx, Bytes val, [Integer flags] )
Puts the entire contents of the Bytes, val, into the given Bytes, b, prepending the value inserted with a 4-byte, signed integer length indicator.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
See Bytes.PutPBytes() and Bytes.PutBytes().
Bytes PutLString( Bytes b, Integer startidx, String val, [Integer flags] )
Puts the given String into the Bytes, prepending it with a 4-byte signed integer length indicator.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutPBytes( Bytes b, Integer startidx, Bytes val, [Integer flags] )
Puts the contents of the given Bytes into the Bytes, prepending it with a 4-byte, unsigned integer length indicator (see Bytes.PutPString()).
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutPString( Bytes b, Integer startidx, String val, [Integer flags] )
Puts the given String into the Bytes, prepending the String with a 4-byte, unsigned integer length indicator, as is commonly done with Pascal strings.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutPoint( Bytes b, Integer startidx, Point val, [Integer flags] )
Puts the given Point into the Bytes as two, signed, 4-byte values.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutR4( Bytes b, Integer startidx, Real val, [Integer flags] )
Puts the given Real into the Bytes as a 4-byte value.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutR8( Bytes b, Integer startidx, Real val, [Integer flags] )
Puts the given Real into the Bytes as an 8-byte value.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutString( Bytes b, Integer startidx, String val, [Integer flags] )
Puts the given Integer into the Bytes as a signed, 4-byte value.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutU1( Bytes b, Integer startidx, Integer val, [Integer flags] )
Puts the given Integer into the Bytes as an unsigned, 4-byte value.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutU2( Bytes b, Integer startidx, Integer val, [Integer flags] )
Puts the given Integer into the Bytes as an unsigned, 4-byte value.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |
Bytes PutU4( Bytes b, Integer startidx, Integer val, [Integer flags] )
Puts the given Integer into the Bytes as an unsigned, 4-byte value.
b | - | The Bytes, or a copy of this Bytes, into which the value is put. |
startidx | - | The index in the Bytes of where to put the value. |
val | - | The value to put. |
flags | - | If specified, Bytes.InPlace if the value should be put into the original Bytes instead of a copy, and/or Bytes.Swap if PC-compatible byte-order should be maintained (use bitwise or to combine the two flags). By default, the value is put into a copy, and the byte-order maintained is that native to the platform running the server. |