Collections
Buffer
Buffer.alloc, writeUIntBE/LE và Buffer.compare của Node.js so với []byte (Go), &mut [u8] (Rust), [UInt8] (Swift) và byte[] (Java).
- Phiên bản tối thiểu
- Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 3.0Java ≥ 25
- Đã chạy thử trên
- Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1
Code Node.js là ES module: lưu file .mjs hoặc đặt "type": "module" trong package.json.
Buffer trong Node.js là API cấp cao để làm việc với dữ liệu nhị phân, có sẵn method đọc/ghi big-endian và little-endian. Go không có kiểu Buffer riêng — bạn thao tác trực tiếp trên []byte bằng các package chuẩn encoding/binary, encoding/hex và bytes. Rust, Swift và Java cũng không có sẵn method kiểu writeUIntBE/writeUIntLE cho độ dài byte tuỳ ý — cả ba đều phải tự viết bằng dịch bit (>>, &), y hệt cách làm ở Go.
Ghi số nguyên theo big-endian / little-endian
const buf = Buffer.alloc(6)
const value = 0x1234567890ab
buf.writeUIntBE(value, 0, 6)
console.log(buf.toString('hex')) // 1234567890ab
const buf2 = Buffer.alloc(6)
buf2.writeUIntLE(value, 0, 6)
console.log(buf2.toString('hex')) // ab9078563412package main
import (
"encoding/binary"
"encoding/hex"
"fmt"
)
func writeUIntBE(buffer []byte, value uint64, offset, byteLength int) {
var tmp [8]byte
binary.BigEndian.PutUint64(tmp[:], value)
copy(buffer[offset:], tmp[8-byteLength:])
}
func writeUIntLE(buffer []byte, value uint64, offset, byteLength int) {
var tmp [8]byte
binary.LittleEndian.PutUint64(tmp[:], value)
copy(buffer[offset:], tmp[:byteLength])
}
func main() {
buf := make([]byte, 6)
writeUIntBE(buf, 0x1234567890ab, 0, 6)
fmt.Println(hex.EncodeToString(buf)) // 1234567890ab
buf2 := make([]byte, 6)
writeUIntLE(buf2, 0x1234567890ab, 0, 6)
fmt.Println(hex.EncodeToString(buf2)) // ab9078563412
}fn write_uint_be(buffer: &mut [u8], value: u64, offset: usize, byte_length: usize) {
let tmp = value.to_be_bytes(); // [u8; 8]
buffer[offset..offset + byte_length].copy_from_slice(&tmp[8 - byte_length..]);
}
fn write_uint_le(buffer: &mut [u8], value: u64, offset: usize, byte_length: usize) {
let tmp = value.to_le_bytes();
buffer[offset..offset + byte_length].copy_from_slice(&tmp[..byte_length]);
}
fn to_hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
fn main() {
let mut buf = vec![0u8; 6];
write_uint_be(&mut buf, 0x1234567890ab, 0, 6);
println!("{}", to_hex(&buf)); // 1234567890ab
let mut buf2 = vec![0u8; 6];
write_uint_le(&mut buf2, 0x1234567890ab, 0, 6);
println!("{}", to_hex(&buf2)); // ab9078563412
}func writeUIntBE(_ buffer: inout [UInt8], value: UInt64, offset: Int, byteLength: Int) {
for i in 0..<byteLength {
buffer[offset + i] = UInt8((value >> (8 * (byteLength - 1 - i))) & 0xff)
}
}
func writeUIntLE(_ buffer: inout [UInt8], value: UInt64, offset: Int, byteLength: Int) {
for i in 0..<byteLength {
buffer[offset + i] = UInt8((value >> (8 * i)) & 0xff)
}
}
func toHex(_ bytes: [UInt8]) -> String {
bytes.map { byte -> String in
let hex = String(byte, radix: 16)
return byte < 16 ? "0" + hex : hex
}.joined()
}
var buf = [UInt8](repeating: 0, count: 6)
writeUIntBE(&buf, value: 0x1234567890ab, offset: 0, byteLength: 6)
print(toHex(buf)) // 1234567890ab
var buf2 = [UInt8](repeating: 0, count: 6)
writeUIntLE(&buf2, value: 0x1234567890ab, offset: 0, byteLength: 6)
print(toHex(buf2)) // ab9078563412void writeUIntBE(byte[] buffer, long value, int offset, int byteLength) {
for (int i = 0; i < byteLength; i++) {
buffer[offset + i] = (byte) (value >> (8 * (byteLength - 1 - i)));
}
}
void writeUIntLE(byte[] buffer, long value, int offset, int byteLength) {
for (int i = 0; i < byteLength; i++) {
buffer[offset + i] = (byte) (value >> (8 * i));
}
}
void main() {
byte[] buf = new byte[6];
long value = 0x1234567890abL;
writeUIntBE(buf, value, 0, 6);
IO.println(HexFormat.of().formatHex(buf)); // 1234567890ab
byte[] buf2 = new byte[6];
writeUIntLE(buf2, value, 0, 6);
IO.println(HexFormat.of().formatHex(buf2)); // ab9078563412
}So sánh hai buffer
let isEqual = Buffer.compare(buf, buf2) === 0
console.log(isEqual) // false
isEqual = Buffer.compare(buf, buf) === 0
console.log(isEqual) // trueimport "bytes"
isEqual := bytes.Equal(buf, buf2)
fmt.Println(isEqual) // false
isEqual = bytes.Equal(buf, buf)
fmt.Println(isEqual) // truelet is_equal = buf == buf2;
println!("{is_equal}"); // false
let is_equal = buf == buf;
println!("{is_equal}"); // truevar isEqual = buf == buf2
print(isEqual) // false
isEqual = buf == buf
print(isEqual) // trueboolean isEqual = Arrays.equals(buf, buf2);
IO.println(isEqual); // false
isEqual = Arrays.equals(buf, buf);
IO.println(isEqual); // trueTham khảo: github.com/miguelmota/golang-for-nodejs-developers#buffers