进制数转化

# 进制数转化

  1. 整除法:

整除到没有余数

以十进制100转换为二进制为例:

计算 结果 余数
100 / 2 50 0
50 / 2 25 0
25 / 2 12 1
12 / 2 6 0
6 / 2 3 0
3 / 2 1 1

结果: 100 => 1100100

  1. 比表法

... 256 128 64 32 16 8 4 2 1

... 2^8 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0

例如: 173.8125

先算整数部分

- - - - - - - - -
128 64 32 16 8 4 2 1
173 45 45 13 13 5 1 1
1 0 1 0 1 1 0 1

小数部分: 0.8125

计算 结果 余数
0.8125 x 2 1.625 1
0.625 x 2 1.25 1
0.25 x 2 0.5 0
0.5 x 2 1 1

所以计算结果得到: 10101101.1101

# 程序实现

借助 Number 内置函数 toString 实现:

Number.prototype.toString([radix]) 并没有继承 Object.prototype.toString(), radix 为转化基数(2~36), 默认为10

(173.8125).toString(2)  // '10101101.1101'
(173.8125).toString(4)  // '2231.31'
(173.8125).toString(8)  // '255.64'
(173.8125).toString(16) // 'ad.d'
(173.8125).toString(32) // '5d.q'
1
2
3
4
5

手动实现10进制转化为10进制以下的数(10进制以上需要特殊的实现):

function radixConvert(num, radix = 2) {
  if (radix >= 10) return
  let [res, decimalRes] = ['', '']
  let [n, decimal] = [num, num % 1]
  // 整数部分
  while (n) {
    res = n % radix + res
    n = Math.floor(n / radix)
  }

  // 小数部分
  if (decimal > 0) {
    while(decimal > 0 && decimal < 1) {
      decimalRes = decimalRes + String(Math.floor(decimal * radix))
      decimal = decimal * radix % 1
    }
  }

  return [res, decimalRes].join('.')
}

console.log(radixConvert(173.8125)) // '10101101.1101'
console.log(radixConvert(173.8125, 4))  // '2231.31'
console.log(radixConvert(173.8125, 8))  // '255.64'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

简单实现转化为16进制

function radix16Convert(num) {
  // 整除法
  if (num === 0) return num

  const HEXS = '0123456789abcdef'
  let [n, decimal] = [num, num % 1]
  let [hex, decimalHex] = ['', '']
  // 整数部分
  while (n) {
    hex = HEXS.charAt(n % 16) + hex
    n = Math.floor(n / 16)
  }
  // 小数部分
  if (decimal > 0) {
    while(decimal < 1) {
      decimalHex = HEXS.charAt(decimal * 16) + decimalHex
      decimal = Math.floor(decimal * 16)
    }
  }
  return [hex, decimalHex].join('.')
}

console.log(radix16Convert(10022321)) // 98edb1
console.log(radixConvert(173))  // ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

使用栈实现进制转化

点击查看代码
class Stack {
  constructor() {
    this.dataStore = []
    this.top = 0
  }

  // 入栈
  push(value) {
    return this.dataStore[this.top++] = value
  }

  // 删除栈顶元素
  pop() {
    return this.dataStore[--this.top]
  }

  // 返回栈顶元素
  peek() {
    return this.dataStore[this.top - 1]
  }

  // 判断栈是否为空
  isEmpty() {
    return this.top === 0
  }

  // 清空栈
  clear() {
    this.top = 0
  }

  // 返回栈大小
  size() {
    return this.top
  }

  // 打印栈
  print() {
    console.log('print -> ', this.dataStore.toString())
  }
}

/** */
function hex2Decimal(decNumber, base = 10) {
  let remStack = new Stack(),
    rem,
    binaryStr = '',
    digits = '0123456789ABCDEF'

  while (decNumber > 0) {
    rem = Math.floor(decNumber % base)
    remStack.push(rem)
    decNumber = Math.floor(decNumber / base)
  }

  console.log('remStack -> ', remStack)

  while (!remStack.isEmpty()) {
    binaryStr += digits[remStack.pop()]
  }

  return binaryStr
}


hex2Decimal(8, 2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

# 参考资料