Skip to content

Commit

Permalink
feat: 增加 八进制转二进制
Browse files Browse the repository at this point in the history
  • Loading branch information
fxss5201 committed Jul 16, 2024
1 parent 8b417ba commit d459a5d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/.vitepress/otherItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const otherItems = [
{
"text": "二进制转十进制",
"link": "/other/binaryToDecimal"
},
{
"text": "八进制转二进制",
"link": "/other/octalToBinary"
}
]

Expand Down
1 change: 1 addition & 0 deletions docs/other/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
- [链表反转](./listNodeReverse)
- [十进制转二进制](./decimalToBinary)
- [二进制转十进制](./binaryToDecimal)
- [八进制转二进制](./octalToBinary)
12 changes: 12 additions & 0 deletions docs/other/octalToBinary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 八进制转二进制

八进制转二进制

## 代码

::: code-group

<<< ../../src/other/octalToBinary/javascript.js{javascript} [javascript]
<<< ../../src/other/octalToBinary/typescript.ts{typescript} [typescript]

:::
13 changes: 13 additions & 0 deletions src/other/octalToBinary/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 八进制转二进制
// 输入:'15'
// 输出:'1101'

import { decimalToBinary } from "../decimalToBinary/javascript"

export function octalToBinary (str) {
let res = ''
for (let i = 0; i < str.length; i++) {
res += decimalToBinary(Number(str[i]))
}
return res
}
13 changes: 13 additions & 0 deletions src/other/octalToBinary/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 八进制转二进制
// 输入:'15'
// 输出:'1101'

import { decimalToBinary } from "../decimalToBinary/typescript"

export function octalToBinary (str: string): string {
let res = ''
for (let i = 0; i < str.length; i++) {
res += decimalToBinary(Number(str[i]))
}
return res
}
6 changes: 6 additions & 0 deletions test/other/octalToBinary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, test } from 'vitest'
import { octalToBinary } from '../../src/other/octalToBinary/typescript.ts'

test(`'15' to '1101'`, () => {
expect(octalToBinary('15')).toBe('1101')
})

0 comments on commit d459a5d

Please sign in to comment.