function addStrings(a, b) local carry = 0 local result = {} local lenA, lenB = #a, #b local maxLen = math.max(lenA, lenB) a = string.rep("0", maxLen - lenA) .. a b = string.rep("0", maxLen - lenB) .. b for i = maxLen, 1, -1 do local sum = tonumber(a:sub(i, i)) + tonumber(b:sub(i, i)) + carry carry = math.floor(sum / 10) table.insert(result, 1, tostring(sum % 10)) end if carry > 0 then table.insert(result, 1, tostring(carry)) end return table.concat(result) end function subtractStrings(a, b) local aStr = tostring(a) local bStr = tostring(b) local maxLen = math.max(#aStr, #bStr) aStr = string.rep("0", maxLen - #aStr) .. aStr bStr = string.rep("0", maxLen - #bStr) .. bStr local result = {} local borrow = 0 for i = maxLen, 1, -1 do local digitA = tonumber(aStr:sub(i, i)) local digitB = tonumber(bStr:sub(i, i)) local diff = digitA - digitB - borrow if diff < 0 then diff = diff + 10 borrow = 1 else borrow = 0 end table.insert(result, 1, tostring(diff)) end local resultStr = table.concat(result) resultStr = resultStr:gsub("^0+", "") return resultStr == "" and "0" or resultStr end function multiplyStrings(a, b) local lenA, lenB = #a, #b local result = "0" for i = lenB, 1, -1 do local digitB = tonumber(b:sub(i, i)) local carry = 0 local tempResult = {} for j = lenA, 1, -1 do local digitA = tonumber(a:sub(j, j)) local prod = (digitA * digitB) + carry carry = math.floor(prod / 10) table.insert(tempResult, 1, tostring(prod % 10)) end if carry > 0 then table.insert(tempResult, 1, tostring(carry)) end local trailingZeros = string.rep("0", lenB - i) local partialResult = table.concat(tempResult) .. trailingZeros result = addStrings(result, partialResult) end return result end local INT32_MOD = "4294967296" local function toUint32DecimalString(decValue) local value = tostring(decValue or "0") local isNegative = value:sub(1, 1) == "-" if isNegative then -- Convert signed negative decimal to uint32 decimal using two's complement. local absValue = value:sub(2):gsub("^0+", "") if absValue == "" then return "0" end return subtractStrings(INT32_MOD, absValue) end value = value:gsub("^0+", "") return value == "" and "0" or value end local function bytesToU32String(b1, b2, b3, b4) if b1 == nil or b2 == nil or b3 == nil or b4 == nil then return nil end if b1 < 0 then b1 = b1 + 256 end if b2 < 0 then b2 = b2 + 256 end if b3 < 0 then b3 = b3 + 256 end if b4 < 0 then b4 = b4 + 256 end local part1 = tostring(b1) local part2 = multiplyStrings(tostring(b2), "256") local part3 = multiplyStrings(tostring(b3), "65536") local part4 = multiplyStrings(tostring(b4), "16777216") local result = addStrings(part1, addStrings(part2, addStrings(part3, part4))) result = result:gsub("^0+", "") return result == "" and "0" or result, b4 end function bytesToInt32(b1, b2, b3, b4) local unsigned, highByte = bytesToU32String(b1, b2, b3, b4) if unsigned == nil then return nil end if highByte >= 128 then return "-" .. subtractStrings(INT32_MOD, unsigned) end return unsigned end function bytesToUInt32(b1, b2, b3, b4) local unsigned = bytesToU32String(b1, b2, b3, b4) return unsigned end function decToBinaryString(decStr) local result = "" local number = decStr while number ~= "0" do local quotient, remainder = "", 0 for i = 1, #number do local digit = tonumber(number:sub(i,i)) local temp = remainder * 10 + digit local q = math.floor(temp / 2) remainder = temp % 2 quotient = quotient .. q end result = remainder .. result number = quotient:match("^0*(.-)$") -- strip leading zeros if number == "" then number = "0" end end -- Pad to 32 bits while #result < 32 do result = "0" .. result end return result end -- Extract low 28 and high 4 from binary string function extractBits32String(decimalStr) local unsignedDec = toUint32DecimalString(decimalStr) local bin = decToBinaryString(unsignedDec) local high4 = tonumber(bin:sub(1, 4), 2) local low28 = tonumber(bin:sub(5), 2) return high4, low28 end function buildBits32String(high4, low28) -- Convert high4 to binary string, pad to 4 bits local high4Bin = "" local h = tonumber(high4) or 0 for i = 3, 0, -1 do high4Bin = high4Bin .. tostring(math.floor(h / 2^i) % 2) end -- Convert low28 to binary string, pad to 28 bits local low28Bin = "" local l = tonumber(low28) or 0 for i = 27, 0, -1 do low28Bin = low28Bin .. tostring(math.floor(l / 2^i) % 2) end -- Concatenate to get full 32-bit binary string local binStr = high4Bin .. low28Bin -- Convert binary string to decimal string using string math local decStr = "0" for i = 1, #binStr do decStr = multiplyStrings(decStr, "2") if binStr:sub(i, i) == "1" then decStr = addStrings(decStr, "1") end end return decStr end