biketrack-app/node_modules/@ethersphere/bee-js/dist/mjs/utils/uint64.js

23 lines
845 B
JavaScript

import { makeBytes } from "./bytes.js"; // TODO handle bigger values than 32 bit
// For now it's good enough because we only use these functions
// sequential feed indexes.
export function writeUint64LittleEndian(value, bytes = makeBytes(8)) {
const dataView = new DataView(bytes.buffer);
const valueLower32 = value & 0xffffffff;
const littleEndian = true;
dataView.setUint32(0, valueLower32, littleEndian);
dataView.setUint32(4, 0, littleEndian);
return bytes;
}
export function writeUint64BigEndian(value, bytes = makeBytes(8)) {
const dataView = new DataView(bytes.buffer);
const valueLower32 = value & 0xffffffff;
dataView.setUint32(0, 0);
dataView.setUint32(4, valueLower32);
return bytes;
}
export function readUint64BigEndian(bytes) {
const dataView = new DataView(bytes.buffer);
return dataView.getUint32(4);
}