Skip to content

Commit 650a064

Browse files
sharkdpDavid Peter
authored and
David Peter
committed
Add 'bin' and 'oct', closes #289
1 parent 3cf4a5e commit 650a064

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

book/src/number-notation.md

+13
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,16 @@ Numbers in Numbat can be written in the following forms:
1717
* `0x2A` — Hexadecimal
1818
* `0o52` — Octal
1919
* `0b101010` — Binary
20+
21+
## Convert numbers to other bases
22+
23+
You can use the `bin`, `oct`, and `hex` functions to convert numbers to binary, octal, and hexadecimal bases, respectively.
24+
As with any other function, you can call those using `hex(2^16 - 1)`, but it's often more convenient to use the `… // hex`
25+
convention to convert a number.
26+
27+
Examples:
28+
```nbt
29+
0xffee // bin
30+
42 // oct
31+
2^16 - 1 // hex
32+
```

examples/prelude_tests.nbt

+15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ assert(str_replace("xxx", "x", "yY") == "yYyYyY")
7373

7474
assert(str_repeat("xy", 3) == "xyxyxy")
7575

76+
assert(bin(0b0) == "0b0")
77+
assert(bin(0b1) == "0b1")
78+
assert(bin(0b10) == "0b10")
79+
assert(bin(0b11) == "0b11")
80+
assert(bin(0b10101010101010101010101010101010) == "0b10101010101010101010101010101010")
81+
assert(bin(-0b11110000) == "-0b11110000")
82+
83+
assert(oct(0o0) == "0o0")
84+
assert(oct(0o1) == "0o1")
85+
assert(oct(0o7) == "0o7")
86+
assert(oct(0o10) == "0o10")
87+
assert(oct(0o77) == "0o77")
88+
assert(oct(0o12345670) == "0o12345670")
89+
assert(oct(-0o12345670) == "-0o12345670")
90+
7691
assert(hex(0x0) == "0x0")
7792
assert(hex(0x1) == "0x1")
7893
assert(hex(0x9) == "0x9")

numbat/modules/core/strings.nbt

+26-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,32 @@ fn str_repeat(a: String, n: Scalar) -> String =
3030
then str_append(a, str_repeat(a, n - 1))
3131
else ""
3232

33-
fn hex_digit(x: Scalar) -> String =
33+
fn _bin_digit(x: Scalar) -> String =
34+
chr(48 + mod(x, 2))
35+
36+
fn _oct_digit(x: Scalar) -> String =
37+
chr(48 + mod(x, 8))
38+
39+
fn _hex_digit(x: Scalar) -> String =
3440
if mod(x, 16) < 10 then chr(48 + mod(x, 16)) else chr(97 + mod(x, 16) - 10)
3541

42+
fn bin(x: Scalar) -> String =
43+
if x < 0
44+
then "-{bin(-x)}"
45+
else if x < 2
46+
then "0b{chr(48 + x)}"
47+
else str_append(bin(floor(x / 2)), _bin_digit(mod(x, 2)))
48+
49+
fn oct(x: Scalar) -> String =
50+
if x < 0
51+
then "-{oct(-x)}"
52+
else if x < 8
53+
then "0o{chr(48 + x)}"
54+
else str_append(oct(floor(x / 8)), _oct_digit(x))
55+
3656
fn hex(x: Scalar) -> String =
37-
if x < 0
38-
then "-{hex(-x)}"
39-
else if floor(x / 16) == 0
40-
then str_append("0x", hex_digit(x))
41-
else str_append(hex(floor(x / 16)), hex_digit(x))
57+
if x < 0
58+
then "-{hex(-x)}"
59+
else if floor(x / 16) == 0
60+
then str_append("0x", _hex_digit(x))
61+
else str_append(hex(floor(x / 16)), _hex_digit(x))

0 commit comments

Comments
 (0)