TypedArr (TA00)
1: export const binaryString = (arr) => Array.from(arr)
2: .map(byte => byte.toString(2).padStart(8, '0'))
3: .join(' ');
4:
5: export const intToHexArray = (number, byteLength = 4) => {
6: const hexArray = [];
7: for (let i = 0; i < byteLength; i++) {
8: const byte = (number >> (8 * i)) & 0xff; // Extract each byte
9: hexArray.push(byte.toString(16).padStart(2, '0')); // Convert to hex
10: }
11: return hexArray;
12: }
13:
14: export function hexArrayToInt(hexArray) {
15: let number = 0;
16: for (let i = 0; i < hexArray.length; i++) {
17: const byte = parseInt(hexArray[i], 16); // Convert hex to byte
18: number = (number << 8) | byte; // Shift and combine bytes
19: }
20: return number;
21: }
22:
23: export const byteArrayAsHex = (buffer) => {
24: if (!(buffer instanceof ArrayBuffer)) {
25: throw new TypeError("Input must be an ArrayBuffer");
26: } else {
27: const byteArray = new Uint8Array(buffer);
28: return Array.from(byteArray).map(byte => (byte & 0xff).toString(16).padStart(2, '0'));
29: }
30: }
31:
32: export function numberToBinary(number) {
33: let binaryString = number.toString(2);
34: const paddingLength = (8 - (binaryString.length % 8)) % 8;
35: binaryString = binaryString.padStart(binaryString.length + paddingLength, '0');
36: return binaryString.match(/.{1,8}/g).join(' ');
37: }
The order in which bytes are stored in memory is called endianness. JavaScript uses little-endian by default, which means the least significant byte is stored first (at the lower memory address). In little-endian systems (like most modern CPUs, including x86 and ARM), the least significant byte is stored at the lowest memory address. This is why the bytes appear to "fill from the left." so, in Javascript 12345 stored as The value 12345 is a 32-bit integer. In binary, it is: 00000000 00000000 00110000 00111001 and stored in In little-endian format, the bytes are stored in reverse order: 00111001 00110000 00000000 00000000 Java uses big-endian by default for most operations. For example, the 32-bit integer 12345 (0x00003039 in hex) is stored as: 00000000 00000000 00110000 00111001 Java provides a ByteBuffer class that allows you to control endianness explicitly. You can set it to little-endian if needed: ByteBuffer buffer = ByteBuffer.allocate(4); buffer.order(ByteOrder.LITTLE_ENDIAN); // Switch to little-endian buffer.putInt(12345) If Java (big-endian) sends data to .NET or JavaScript (little-endian), you’ll need to swap the byte order. If .NET or JavaScript (little-endian) sends data to Java (big-endian), you’ll need to swap the byte order. Dim value As Integer = 12345 '0x00, 0x00, 0x30, 0x39 Dim bytes As Byte() = BitConverter.GetBytes(value) Array.Reverse(bytes) ' Convert to big-endian Module Module1 Sub Main() Dim value As Integer = 12345 Dim bytes As Byte() = BitConverter.GetBytes(value) Array.Reverse(bytes) ' Convert to big-endian ' Print the byte array Console.WriteLine("Byte Array (Big-Endian):") For Each b As Byte In bytes Console.Write(b.ToString("X2") & " ") ' Print in hexadecimal format Next End Sub End Module Byte Array (Big-Endian): 00 00 30 39 Bit order is hardware-defined and consistent across all systems. Example: 32-bit Integer 0x12345678 Big-Endian: Address: 0x00 0x01 0x02 0x03 Value: 0x12 0x34 0x56 0x78 Little-Endian: Address: 0x00 0x01 0x02 0x03 Value: 0x78 0x56 0x34 0x12 Regardless of endianness, the bits within each byte are always ordered from MSB to LSB. For example: 0x12 is always 00010010 in binary. 0x78 is always 01111000 in binary.
TypedArr context:
ES6 context:
- (2024) Notes about JS Closures. #ES6
- (2024) Notes about Javascript asynchronous programming. #ES6
- (2022) Modern Javascript books #ES6 #Doc
- (2021) JS learning start point #ES6
- (2021) Maximilian Schwarzmüller Javascript lecture #ES6
- (2021) Javascript interview question from Happy Rawat #ES6
- (2021) Javascript tests #ES6
- (2016) New unique features of Javascript (updated). #ES6
Binary context:
- (2019) How to change image Exif metadata #Binary
- (2018) З'ясування алгоритмів оновлення Documat-CD SEB. #Binary
- (2016) JS6 Typed Array #ES6 #Binary
- (2015) Сховище графіки на SQL FileStream та канал браузеру multipart/form-data. #Sql #WebServiceClient #Binary
- (2012) Робота з байтами у VB.NET - ChrW, BitConverter.ToInt32, Convert.ToInt32, Byte.Parse, ToString("X2") / AllowHexSpecifier, GetBytes/GetString, New Byte(N) {}, UInt32 = &H33 #Binary #NetCommon
- (2005) DumpExe - утилитка дампирования структуры NET-сборок с открытым исходным текстом. #Binary #WinDesktop
- (2003) DVD - формат и обзор техник работы с ним. #Binary
- (2002) PE - формат исполняемого файла. #Binary
- (2002) FRX-парсер - моя утилитка для выкусывания рисунков из шестерочного FRX-файла. #WinDesktop #Vb6 #Binary
Comments (
)

Link to this page:
http://www.vb-net.com/JavascriptES6/TA00.htm
|