Skip to content

Commit 5afa9b6

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

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

stdlib/int.jk

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
if i < 0 {
16+
concat("-", result)
17+
} else {
18+
result
19+
}
20+
}
21+
22+
func hex(i: int) -> string {
23+
"0xff"
24+
}

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)