|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +/** |
| 4 | + * Converts a number from big endian to little endian byte order |
| 5 | + */ |
| 6 | + |
| 7 | +function bigEndianToLittleEndian(num: bigint): bigint { |
| 8 | + // Convert number to 64-bit buffer (big endian) |
| 9 | + const buffer = Buffer.allocUnsafe(8); |
| 10 | + buffer.writeBigUInt64BE(num); |
| 11 | + |
| 12 | + // Read as little endian |
| 13 | + const littleEndian = buffer.readBigUInt64LE(); |
| 14 | + |
| 15 | + return littleEndian; |
| 16 | +} |
| 17 | + |
| 18 | +// Main execution |
| 19 | +const inputNumber = process.argv[2] || '360287970189639680'; |
| 20 | +const num = BigInt(inputNumber); |
| 21 | + |
| 22 | +console.log('\nBig Endian to Little Endian Conversion\n'); |
| 23 | +console.log('='.repeat(50)); |
| 24 | + |
| 25 | +// Show original (big endian) |
| 26 | +const beBuf = Buffer.allocUnsafe(8); |
| 27 | +beBuf.writeBigUInt64BE(num); |
| 28 | +console.log('\nOriginal (Big Endian):'); |
| 29 | +console.log(` Decimal: ${num}`); |
| 30 | +console.log(` Hex: 0x${num.toString(16).padStart(16, '0')}`); |
| 31 | +console.log(` Bytes: ${Array.from(beBuf).map(b => b.toString(16).padStart(2, '0')).join(' ')}`); |
| 32 | + |
| 33 | +// Convert to little endian |
| 34 | +const leBuf = Buffer.allocUnsafe(8); |
| 35 | +leBuf.writeBigUInt64LE(num); |
| 36 | +const littleEndianValue = leBuf.readBigUInt64BE(); // Read the LE bytes as if they were BE |
| 37 | + |
| 38 | +console.log('\nConverted (Little Endian):'); |
| 39 | +console.log(` Decimal: ${littleEndianValue}`); |
| 40 | +console.log(` Hex: 0x${littleEndianValue.toString(16).padStart(16, '0')}`); |
| 41 | +console.log(` Bytes: ${Array.from(leBuf).map(b => b.toString(16).padStart(2, '0')).join(' ')}`); |
| 42 | + |
| 43 | +console.log('\n' + '='.repeat(50) + '\n'); |
0 commit comments