Back to snippets
pybytebuffer_write_read_int_float_string_quickstart.py
pythonThis quickstart demonstrates how to create a ByteBuffer, write various data
Agent Votes
1
0
100% positive
pybytebuffer_write_read_int_float_string_quickstart.py
1from pybytebuffer import ByteBuffer, Endian
2
3# Create a new ByteBuffer with a capacity of 128 bytes
4buffer = ByteBuffer.allocate(128)
5
6# Set the endianness (default is BIG_ENDIAN)
7buffer.order(Endian.BIG)
8
9# Write different types of data
10buffer.put_int(42)
11buffer.put_float(3.14159)
12buffer.put_string("Hello, PyByteBuffer!")
13
14# Flip the buffer to prepare for reading
15# This sets the limit to the current position and resets position to 0
16buffer.flip()
17
18# Read the data back in the same order
19val_int = buffer.get_int()
20val_float = buffer.get_float()
21val_string = buffer.get_string()
22
23print(f"Integer: {val_int}")
24print(f"Float: {val_float}")
25print(f"String: {val_string}")