BitBuffer
BitBuffer object.
Functions
is
GeneralBitBuffer.
is
(
obj:
any
) →
boolean
Returns whether the passed object is a BitBuffer.
print(BitBuffer.is(BitBuffer.new())) --> true
print(BitBuffer.is(true)) --> false
new
ConstructorBitBuffer.
new
(
sizeInBits:
number?
--
Initial size of the buffer in bits (defaults to 0)
) →
BitBuffer
Creates a new BitBuffer with an initial size of sizeInBits
.
local buffer = BitBuffer.new(128)
print(buffer:GetSize()) --> 128
FromString
ConstructorCreates a new BitBuffer from a binary string, starting with a size corresponding to number of bits in the input string (8 bits per character), and it's cursor positioned at 0.
local buffer = BitBuffer.FromString("\89")
print(buffer:ReadUInt(8)) --> 89
print(buffer:GetSize()) --> 8
FromBase64
ConstructorCreates a new BitBuffer from a Base64 string, starting with a size corresponding to the number of bits stored in the input string (6 bits per character), and it's cursor positioned at 0.
local str = base64("\45\180")
local buffer = BitBuffer.FromBase64(str)
print(buffer:ReadUInt(8)) --> 45
print(buffer:ReadUInt(8)) --> 180
FromBase91
ConstructorCreates a new BitBuffer from a Base91 string, starting with a size corresponding to the number of bits stored in the input string, and it's cursor positioned at 0. This is the recommended function to use for DataStores.
local initialBuffer = BitBuffer.new()
initialBuffer:WriteUInt(32, 78)
initialBuffer:WriteString("Hi")
local b91 = initialBuffer:ToBase91()
local newBuffer = BitBuffer.FromBase91(b91)
print(newBuffer:ReadUInt(32)) --> 78
print(newBuffer:ReadString()) --> Hi
What is Base91?
Base91 is a way to pack binary data into text, similar to Base64. It is, on average, about 10% more efficient than Base64. Check this page to learn more.
FromBase128
ConstructorCreates a new BitBuffer from a Base128 string, starting with a size corresponding to the number of bits stored in the input string (7 bits per character), and it's cursor positioned at 0.
local str = base128("\255\12")
local buffer = BitBuffer.FromBase128(str)
print(buffer:ReadUInt(8)) --> 255
print(buffer:ReadUInt(8)) --> 12
ResetCursor
GeneralBitBuffer:
ResetCursor
(
) →
(
)
Resets the position of the cursor.
local buffer = BitBuffer.new()
buffer:WriteUInt(32, 890)
buffer:ResetCursor()
print(buffer:GetCursor()) --> 0
SetCursor
GeneralBitBuffer:
SetCursor
(
position:
number
) →
(
)
Sets the position of the cursor to the given position.
local buffer = BitBuffer.new()
buffer:WriteUInt(32, 67)
buffer:WriteUInt(32, 44)
buffer:SetCursor(32)
print(buffer:ReadUInt(32)) --> 44
GetCursor
GeneralBitBuffer:
GetCursor
(
) →
number
Returns the position of the cursor.
local buffer = BitBuffer.new()
buffer:WriteUInt(17, 901)
buffer:WriteUInt(4, 2)
print(buffer:GetCursor()) --> 21
ResetBuffer
GeneralBitBuffer:
ResetBuffer
(
) →
(
)
Clears the buffer, setting its size to zero, and sets its position to 0.
local buffer = BitBuffer.new()
buffer:WriteUInt(32, math.pow(2, 32) - 1)
buffer:ResetBuffer()
print(buffer:GetCursor()) --> 0
print(buffer:ReadUInt(32)) --> 0
GetSize
GeneralBitBuffer:
GetSize
(
) →
number
Returns the size of the buffer.
local buffer = BitBuffer.new()
buffer:WriteUInt(18, 618)
print(buffer:GetSize()) --> 18
ToString
SerializationBitBuffer:
ToString
(
) →
string
Serializes the buffer into a binary string. You can retrieve the buffer from this string using BitBuffer.FromString.
local buffer = BitBuffer.new()
buffer:WriteUInt(8, 65)
buffer:WriteUInt(8, 66)
buffer:WriteUInt(8, 67)
print(buffer:ToString()) --> ABC
ToBase64
SerializationBitBuffer:
ToBase64
(
) →
string
Serializes the buffer into a Base64 string. You can retrieve the buffer from this string using BitBuffer.FromBase64.
local initialBuffer = BitBuffer.new()
initialBuffer:WriteUInt(15, 919)
initialBuffer:WriteString("Hello!")
local b64 = initialBuffer:ToBase64()
local newBuffer = BitBuffer.FromBase64(b64)
print(newBuffer:ReadUInt(15)) --> 919
print(newBuffer:ReadString()) --> Hello!
ToBase91
SerializationBitBuffer:
ToBase91
(
) →
string
Serializes the buffer into a Base91 string. You can retrieve the buffer from this string using BitBuffer.FromBase91. This is the recommended function to use for DataStores.
local buffer = BitBuffer.new()
buffer:WriteString(playerData.CustomName)
buffer:WriteUInt(8, playerData.Level)
buffer:WriteUInt(16, playerData.Money)
SaveToDataStore(buffer:ToBase91())
local b91 = RetrieveFromDataStore()
local buffer = BitBuffer.FromBase91(b91)
local playerData = {
CustomName = buffer:ReadString();
Level = buffer:ReadUInt(8);
Money = buffer:ReadUInt(16);
}
ToBase128
SerializationBitBuffer:
ToBase128
(
) →
string
Serializes the buffer into a Base128 string. You can retrieve the buffer from this string using BitBuffer.FromBase128.
return string
WriteUInt
WriteBitBuffer:
WriteUInt
(
bitWidth:
number
,
uint:
number
) →
(
)
Writes an unsigned integer of bitWidth
bits to the buffer.
bitWidth
must be an integer between 1 and 32.
If the input integer uses more bits than bitWidth
, it will overflow as expected.
buffer:WriteUInt(32, 560) -- Writes 560 to the buffer
buffer:WriteUInt(3, 9) -- Writes 0b101 (5) because 9 is 0b1101, but `bitWidth` is only 3!
ReadUInt
ReadBitBuffer:
ReadUInt
(
bitWidth:
number
) →
number
Reads bitWidth
bits from the buffer as an unsigned integer.
bitWidth
must be an integer between 1 and 32.
buffer:WriteUInt(12, 89)
buffer:ResetCursor()
print(buffer:ReadUInt(12)) --> 89
WriteInt
WriteBitBuffer:
WriteInt
(
bitWidth:
number
,
int:
number
) →
(
)
Writes a signed integer of bitWidth
bits using two's complement.
bitWidth
must be an integer between 1 and 32.
Overflow is untested, use at your own risk.
local buffer = BitBuffer.new()
buffer:WriteInt(22, -901) --> Writes -901 to the buffer
ReadInt
ReadBitBuffer:
ReadInt
(
bitWidth:
number
) →
number
Reads bitWidth
bits as a signed integer stored using two's complement.
bitWidth
must be an integer between 1 and 32.
local buffer = BitBuffer.new()
buffer:WriteInt(15, -78)
buffer:ResetCursor()
print(buffer:ReadInt(15)) --> -78
WriteBool
WriteBitBuffer:
WriteBool
(
value:
any
) →
(
)
Writes one bit the buffer: 1 if value
is truthy, 0 otherwise.
local buffer = BitBuffer.new()
buffer:WriteBool(true) --> Writes 1
buffer:WriteBool("A") --> Also writes 1
buffer:WriteBool(nil) --> Writes 0
ReadBool
ReadBitBuffer:
ReadBool
(
) →
boolean
Reads one bit from the buffer and returns a boolean: true if the bit is 1, false if the bit is 0.
local buffer = BitBuffer.new()
buffer:WriteUInt(4, 0b1011)
buffer:ResetCursor()
print(buffer:ReadBool()) --> true
print(buffer:ReadBool()) --> true
print(buffer:ReadBool()) --> false
print(buffer:ReadBool()) --> true
WriteChar
WriteBitBuffer:
WriteChar
(
char:
string
) →
(
)
Writes one ASCII character (one byte) to the buffer.
char
cannot be an empty string.
local buffer = BitBuffer.new()
buffer:WriteChar("k")
buffer:ResetCursor()
print(buffer:ReadChar()) --> k
ReadChar
ReadBitBuffer:
ReadChar
(
) →
string
Reads one byte as an ASCII character from the buffer.
local buffer = BitBuffer.new()
buffer:WriteUInt(8, 65)
buffer:ResetCursor()
print(buffer:ReadChar()) --> A
WriteBytes
WriteBitBuffer:
WriteBytes
(
bytes:
string
) →
(
)
Writes a stream of bytes to the buffer.
if bytes
is an empty string, nothing will be written.
local buffer = BitBuffer.new()
buffer:WriteBytes("AD")
buffer:ResetCursor()
print(buffer:ReadUInt(8), buffer:ReadUInt(8)) --> 65 68
ReadBytes
ReadBitBuffer:
ReadBytes
(
length:
number
) →
string
Reads length
bytes as a string from the buffer.
if length
is 0, nothing will be read and an empty string will be returned.
local buffer = BitBuffer.new()
buffer:WriteUInt(8, 65)
buffer:WriteUInt(8, 67)
print(buffer:ReadBytes(2)) --> AC
WriteString
WriteBitBuffer:
WriteString
(
str:
string
) →
(
)
Writes a string to the buffer.
WriteString will write the length of the string as a 24-bit unsigned integer first, then write the bytes in the string.
The length of the string cannot be greater than 2^24 - 1 (16777215)
.
local buffer = BitBuffer.new()
buffer:WriteString("AB")
buffer:ResetCursor()
print(buffer:ReadUInt(24), buffer:ReadBytes(2)) --> 2 AB
ReadString
ReadBitBuffer:
ReadString
(
) →
string
Reads a string from the buffer (see BitBuffer::WriteString).
local buffer = BitBuffer.new()
buffer:WriteString("Hello!")
buffer:ResetCursor()
print(buffer:ReadString()) --> Hello!
WriteFloat32
WriteBitBuffer:
WriteFloat32
(
float:
number
) →
(
)
Writes a single-precision floating point number to the buffer.
local buffer = BitBuffer.new()
buffer:WriteFloat32(892.738)
buffer:ResetCursor()
print(buffer:ReadFloat32()) --> 892.73797607421875
ReadFloat32
ReadBitBuffer:
ReadFloat32
(
) →
number
Reads a single-precision floating point number from the buffer.
local buffer = BitBuffer.new()
buffer:WriteFloat32(892.738)
buffer:ResetCursor()
print(buffer:ReadFloat32()) --> 892.73797607421875
WriteFloat64
WriteBitBuffer:
WriteFloat64
(
double:
number
) →
(
)
Writes a double-precision floating point number to the buffer.
local buffer = BitBuffer.new()
buffer:WriteFloat64(-76358128.888202341)
buffer:ResetCursor()
print(buffer:ReadFloat64()) --> -76358128.888202
ReadFloat64
ReadBitBuffer:
ReadFloat64
(
) →
number
Reads a double-precision floating point number from the buffer.
local buffer = BitBuffer.new()
buffer:WriteFloat64(-76358128.888202341)
buffer:ResetCursor()
print(buffer:ReadFloat64()) --> -76358128.888202