Skip to content

Commit 55a71f5

Browse files
committed
feat: add Strings.toBinary()
1 parent 4a789a3 commit 55a71f5

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

CHANGELOG.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- `Strings.toBinary()`
14+
1115

1216
## [7.0.7] - 2025-10-18
1317

1418
### Fixed
1519
- Improve compatibility with Haxe 5
16-
- Wrong AnyAsString type path in String8Generator
17-
- StringBuilder.OutputWrapper.flush does not null buffer as expected
18-
- Rename Char.TRHEE to THREE
19-
- Swapped line/column stored in CharIterator's prev buffer
20-
- Pattern does not auto-apply UTF-8 'u' flag to underlying EReg
21-
- Strings.splitLines(null) does not return null
22-
- internal RingBuffer.get() can return wrong or uninitialized items due to bad indexing
23-
- StringBuilder length not updated when adding null values
20+
- Wrong `AnyAsString` type path in `String8Generator`
21+
- `StringBuilder.OutputWrapper.flush` does not null buffer as expected
22+
- Rename `Char.TRHEE` to `THREE`
23+
- Swapped line/column stored in `CharIterator`'s prev buffer
24+
- Pattern does not auto-apply UTF-8 'u' flag to underlying `EReg`
25+
- `Strings.splitLines(null)` does not return `null`
26+
- internal `RingBuffer.get()` can return wrong or uninitialized items due to bad indexing
27+
- `StringBuilder` length not updated when adding null values
2428
- UTF8 handling in some Strings functions is broken
2529

2630

src/hx/strings/Strings.hx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3578,6 +3578,65 @@ class Strings {
35783578
}
35793579

35803580

3581+
/**
3582+
* Static extension for <code>Int</code>.
3583+
*
3584+
* <pre><code>
3585+
* >>> Strings.toBinary(1) == "1"
3586+
* >>> Strings.toBinary(2) == "10"
3587+
* >>> Strings.toBinary(10) == "1010"
3588+
* >>> Strings.toBinary(10, 8) == "00001010"
3589+
* >>> Strings.toBinary(255) == "11111111"
3590+
* >>> Strings.toBinary(-1) == "11111111111111111111111111111111"
3591+
* >>> Strings.toBinary(-10, 8) == "11110110"
3592+
* </code></pre>
3593+
*
3594+
* @param minDigits the resulting string is left padded with <code>0</code> until it length equals <code>minDigits</code>
3595+
* @return the binary representation of <b>num</b>
3596+
*/
3597+
public static function toBinary(num:Int, ?minDigits:Int):String {
3598+
var result:String;
3599+
3600+
#if cs
3601+
result = cs.system.Convert.ToString(num, 2);
3602+
#elseif java
3603+
result = java.lang.Integer.toBinaryString(num);
3604+
#elseif js
3605+
result = untyped (num >>> 0).toString(2);
3606+
#elseif php
3607+
// Force 32-bit two's complement for negatives, then decbin
3608+
result = (php.Syntax.code("decbin({0} & 0xFFFFFFFF)", num) : String);
3609+
#elseif python
3610+
// Keep 32-bit two's complement for negatives (match other targets)
3611+
result = python.Syntax.code("format({0} & 0xFFFFFFFF, 'b')", num);
3612+
#else
3613+
if (num == 0) {
3614+
result = "0";
3615+
} else {
3616+
var bits = new Array<String>();
3617+
var v = num;
3618+
// Logical shift to shrink even negative ints to 0 in 32 steps
3619+
while (v != 0) {
3620+
bits.push(((v & 1) != 0) ? "1" : "0");
3621+
v >>>= 1;
3622+
}
3623+
bits.reverse();
3624+
result = bits.join("");
3625+
}
3626+
#end
3627+
3628+
if (minDigits != null) {
3629+
// For negatives we want the rightmost minDigits bits (trim leading ones from 32-bit form)
3630+
if (num < 0 && result.length > minDigits) {
3631+
result = result.substr(result.length - minDigits);
3632+
}
3633+
// Then pad with zeros on the left if still shorter
3634+
result = StringTools.lpad(result, "0", minDigits);
3635+
}
3636+
return result;
3637+
}
3638+
3639+
35813640
/**
35823641
* Static extension for <code>Int</code>.
35833642
*

tools/_test-prepare.cmd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ for %%i in (haxe-doctest) do (
3030
)
3131
)
3232

33-
goto :eof
34-
3533
REM install additional libs
3634
:iterate
3735

0 commit comments

Comments
 (0)