> For the complete documentation index, see [llms.txt](https://easonwang.gitbook.io/crypto/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easonwang.gitbook.io/crypto/big-endian-and-small-endian.md).

# Big Endian & small Endian

```javascript
0x12345678：

Small Endian： 0x78 0x56 0x34 0x12 


Big Endian： 0x12 0x34 0x56 0x78
```

以下為範例程式（BigEndian\_to\_SmallEndian）

```javascript
function BigEndian_to_SmallEndian(hexNum) {
  let SmallEndian_array = [];
  if (hexNum.length % 2 !== 0) {
    hexNum = '0' + hexNum // 數字總數為奇數的話 在開頭補0
  }

  for (let i = 0, len = hexNum.length; i < len; i++) {
    if (i % 2 !== 0) {
      SmallEndian_array.unshift(hexNum.charAt(i - 1) + hexNum.charAt(i))
    }
  }

  return SmallEndian_array.join('');
}
```

如果要把Smail 轉為BIg Endian則為同一個程式，把數字再放入跑一次即可轉回

![](https://1775491709-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAKHJCUBld9DYCi%2F-M0u4Ft9n4JCnniIVQ-O%2F-M0u4_A1Fs5_pWxs0C9U%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-12-17%20%E4%B8%8B%E5%8D%882.20.56.png?generation=1582596283253508\&alt=media)
