Buffer multiplying input with bit integers

I’ve been trying to use a buffer to store user ids in an unsigned 32 bit buffer. But the outputted result(using readu32) is one of my numbers being multiplied by a 16-bit integer, and the other by an 8-bit integer, and the final one being the correct number.

I’ve tried looking for answers, but buffers are so new, and so unused, that I can’t find anything on online.

Am I stupid, or are buffers stupid?

This is a an example of what i’m trying to do here:

Currently, i’m only putting the number 1 in each buffer to show the raw numbers. But if I instead use zero, they all result in zero(which is good). But if I use 2, then, the integers just multiply by in inputted number.

local exampleBuffer = buffer.create(6) -- any lower than 6 and the script will throw an error(buffer access out of bounds)

buffer.writeu32(exampleBuffer,0,1) -- writing 32 bit for the actual numbers i'll be using
buffer.writeu32(exampleBuffer,1,1)
buffer.writeu32(exampleBuffer,2,1)

buffer.readu32(exampleBuffer,0) -- 65793 | awfully close to the max 16-bit integer(65,536)
buffer.readu32(exampleBuffer,1) -- 257 | also awfully close to the max 8-bit integer(256)
buffer.readu32(exampleBuffer,2) -- 1 | the correct output

Finally, if you have any good sources to learn more about buffers, please link them below!

Thank you for your time, Robocat.

1 Like

YOU ALL SUCK AT THIS!
Let the A.I. explain this:

Understanding the Buffer Behavior

You’re not stupid, and buffers aren’t stupid either! This is a common issue when working with buffers, especially when trying to pack multiple values into a single buffer.

The Problem

The core issue here is that you’re trying to write 32-bit integers into a buffer that’s only 6 bytes long. A 32-bit integer is 4 bytes long. So, you’re essentially overlapping the data you’re writing.

The Solution

To correctly store three 32-bit integers, you’ll need a buffer that’s at least 12 bytes long. Here’s the corrected code:

local exampleBuffer = buffer.create(12) -- Create a buffer of 12 bytes

buffer.writeu32(exampleBuffer, 0, 1) 
buffer.writeu32(exampleBuffer, 4, 1) 
buffer.writeu32(exampleBuffer, 8, 1) 

local value1 = buffer.readu32(exampleBuffer, 0)
local value2 = buffer.readu32(exampleBuffer, 4)
local value3 = buffer.readu32(exampleBuffer, 8)

print(value1, value2, value3) -- Output: 1 1 1

Explanation

  • Buffer Size: We’ve increased the buffer size to 12 bytes to accommodate three 32-bit integers.
  • Write Positions: The writeu32 function now writes to positions 0, 4, and 8, ensuring that each 32-bit integer occupies its own 4-byte space.
  • Read Positions: Similarly, the readu32 function reads from positions 0, 4, and 8 to retrieve the correct values.

-Gemini A.I.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.