Skip to content

Commit bd3bbe4

Browse files
committed
int: Add conversion to bin/hex string representation
1 parent c370d1b commit bd3bbe4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

stdlib/int.jk

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func bin(i: int) -> string {
2+
mut result = "";
3+
mut i_copy = i;
4+
5+
while i_copy > 0 {
6+
if i_copy % 2 == 0 {
7+
result = concat("0", result);
8+
} else {
9+
result = concat("1", result);
10+
}
11+
12+
i_copy = i_copy / 2;
13+
}
14+
15+
// FIXME: We want proper 2's complement representation
16+
if i < 0 {
17+
concat("-", result)
18+
} else {
19+
result
20+
}
21+
}
22+
23+
func hex(i: int) -> string {
24+
"0xff"
25+
}

stdlib/lib.jk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
incl pair
2+
incl int
23
incl string
34
incl maybe
45
incl range

0 commit comments

Comments
 (0)