Endianness (byte order)

Byte order, or byte order (“Endian”, “Endianness”, or “byte-order”), describes how the computer organizes bytes into their corresponding numbers. Each memory storage location has an index or address. Each byte can store one 8-bit number (that is, 0 to 255 between 0x00 and 0xFF), so you must reserve more than one byte to store a larger number.

  • Today, most numbers that occupy more than one byte are sorted in little-endian order. The opposite of the corresponding big-endian arrangement, which can be called big-endian, high-endian), all Intel processors use little-endian. Little-endian means to store more important information in the low place, least-to-most significant (the least significant byte takes the first position, or the lowest position in the address), which is analogous to the common European way of writing dates (e.g., Another awarding 31 2050. The year is most important, the month second, the date last.
  • Big-endian naturally, big-endian is the reverse order, analogous to the ISO date format (e.g. 2050-12-31). Big-endian is often referred to as “Network Byte order” because Internet standards generally require data to be stored using Big-Endian, starting with the standard Unix socket layer and working down to the binary data structures of the standardized network. In addition, big-Endian was used in the 68000 series of older Mac computers and PowerPC microprocessors.

For example, storing the number 0x12345678 (305 419 896 in decimal) in a different byte order:

  • Little-endian: 0x78 0x56 0x34 0x12
  • Big-endian: 0x12 0x34 0x56 0x78
  • Mixed-endian (relic, very rare) : 0x34 0x12 0x78 0x56

Description: I understand byte high and low is a direction problem, high byte from left to right; The low byte goes from right to left. Memory is from left to right, the mainland address increases one by one, so the text refers to the high end of the address behind.

Base64

Base64 is a similar set of binary-to-text encoding rules that allow binary data to be represented as ASCII strings after being interpreted in the radix-64 representation. In JavaScript, there are two functions for decoding and encoding base64 strings, respectively:

  • Atob () decoding
  • Btoa () code
let encodedData = window.btoa("Hello, world"); // SGVsbG8sIHdvcmxk
let decodedData = window.atob(encodedData); // "Hello, world")

Copy the code

Source/inverse/complement/integer/floating point

  • The source code directly translates binary into binary in the form of positive and negative numbers.
  • Inverse code will be the original code symbol bit unchanged, the other bits in turn according to the bit can be obtained.
  • The complement inverse plus one gives you the complement.
  • Having the complement of an integer in base 2;
  • Floating-point 32-bit (single-precision float) : For 32-bit floating-point numbers, the highest 1 bit is the sign bit S, the next 8 bits are the exponent E, and the remaining 23 bits are the significant digit M. 64-bit (double precision float) : For 64-bit floating point numbers, the highest 1 bit is the sign bit S, the next 11 bits are the exponent E, and the remaining 52 bits are the significant digit M.

JS API

  • The ArrayBuffer object is used to represent a generic, fixed-length buffer of raw binary data. An ArrayBuffer does not operate directly, but through a type array object or DataView object, which represents the data in the buffer in specific formats and reads and writes the contents of the buffer in those formats.
  • The SharedArrayBuffer object is used to represent a generic, fixed-length buffer of raw binary data, similar to an ArrayBuffer object, which can be used to create views on shared memory. Unlike ArrayBuffer, SharedArrayBuffer cannot be separated.
  • Uint8Array array type represents an array of 8-bit unsigned integers that are initialized to 0 when created. Once created, you can refer to the elements of the array as objects or by using an array subscript index.
  • A Uint8ClampedArray typed array represents an array of 8-bit unsigned integers whose values are fixed between 0 and 255; If you specify a value outside the range [0,255], it will be replaced with 0 or 255; If you specify a non-integer, it will be set to the nearest integer. The contents are initialized to 0. Once created, you can refer to the elements of the array using the object’s methods or use the standard array indexing syntax (marked with square brackets).
  • Uint16Array array type represents an array of 16-bit unsigned integers that are initialized to 0 when created. Once created, you can refer to the elements of the array as objects or by using an array subscript index.
  • Uint32Array array type represents an array of 32-bit unsigned integers that are initialized to 0 when created. Once created, you can refer to the elements of the array as objects or by using an array subscript index.
  • Int8Array Array indicates an array of binary complement 8-bit signed integers. The content is initialized to 0. Once established, you can refer to elements in the array using the object’s methods, or use the standard array indexing syntax (that is, annotating with parentheses).
  • Int16Array Type Array indicates an array of binary complement 16-bit signed integers. The content is initialized to 0. Once established, you can refer to elements in the array using the object’s methods, or use the standard array indexing syntax (that is, annotating with parentheses).
  • Int32Array Type Array Indicates an array of binary complement 32-bit signed integers. The content is initialized to 0. Once established, you can refer to elements in the array using the object’s methods, or use the standard array indexing syntax (that is, annotating with parentheses).
  • Float32Array arrays represent floating-point arrays in 32-bit byte order (corresponding to the C floating-point data type). If you need to control byte order, use DataView instead. Its contents are initialized to 0. Once set up, you can use the object’s methods to manipulate its elements, or use the standard array indexing syntax (using square brackets).
  • Float64Array arrays represent floating-point arrays of 64-bit byte order (corresponding to the C floating-point data type). If you need to control byte order, use DataView instead. Its contents are initialized to 0. Once set up, you can use the object’s methods to manipulate its elements, or use the standard array indexing syntax (using square brackets).
  • The DataView is a low-level interface that can read and write multiple numeric types from an ArrayBuffer object, regardless of platform endianness.
Type Value Range Size in bytes Description Web IDL type Equivalent C type
Int8Array -128 to 127 1 8-bit two’s complement signed integer byte int8_t
Uint8Array 0 to 255 1 8-bit unsigned integer octet uint8_t
Uint8ClampedArray 0 to 255 1 8-bit unsigned integer (clamped) octet uint8_t
Int16Array -32768 to 32767 2 16-bit two’s complement signed integer short int16_t
Uint16Array 0 to 65535 2 16-bit unsigned integer unsigned short uint16_t
Int32Array -2147483648 to 2147483647 4 32-bit two’s complement signed integer long int32_t
Uint32Array 0 to 4294967295 4 32-bit unsigned integer unsigned long uint32_t
Float32Array 1.2 x10-38 x1038 to 3.4 4 32-bit IEEE floating point number (7 significant digits e.g. 1.1234567) unrestricted float float
Float64Array 5.0 to 1.8 x10308 x10-324 8 64-bit IEEE Floating Point Number (16 significant digits 1.123… 15) unrestricted double double
BigInt64Array -263 to 263-1 8 64-bit two’s complement signed integer bigint int64_t (signed long long)
BigUint64Array 0 to 264-1 8 64-bit unsigned integer bigint uint64_t (unsigned long long)