Skip to content

feat(js/ts/python/dart/rust/beam): support Convert.ToHexString, ToHexStringLower and FromHexString - #5204

Merged
MangelMaxime merged 6 commits into
fable-compiler:mainfrom
leonitousconforti:support-convert-hex
Oct 30, 2026
Merged

MangelMaxime merged 6 commits into
fable-compiler:mainfrom
leonitousconforti:support-convert-hex

Conversation

@leonitousconforti

Copy link
Copy Markdown
Contributor

Adds Convert.ToHexString, ToHexStringLower and FromHexString to JS/TS, Python, Dart and Beam, where they were compile errors. One commit per target, plus a Rust fix.

  • ToHexString(bytes) and ToHexString(bytes, offset, length) give uppercase digits, ToHexStringLower lowercase ones.
  • FromHexString(s) accepts either case and raises FormatException with .NET's messages for an odd length or a non-hex character. It returns a byte array (a Uint8Array on JS/TS, like FromBase64String).
  • A null input raises ArgumentNullException, and a bad offset or length raises ArgumentOutOfRangeException, checked in .NET's order and with its parameter names.
  • Python gets a new convert.py, since system.py imports string_.py. Beam uses binary:encode_hex/decode_hex (OTP 24).
  • Rust already had the single-argument forms. Its FromHexString now panics on an odd length instead of dropping the last digit.
  • Each suite's raisedExceptionType helper now also reports ArgumentNullException and FormatException.

Differences from .NET: out-of-range messages use Fable's existing wording, and FromHexString counts UTF-8 bytes on Beam and code points on Python, so some non-ASCII strings get the other FormatException message.

Not covered: the Try*, OperationStatus and span overloads, the offset and length overloads on Rust, and null checks on Dart.

leonitousconforti and others added 6 commits October 30, 2026 22:20
…xString

Map them to new String.toHexString, toHexStringLower and fromHexString
library functions, next to the base64 ones. The offset and length
overloads of ToHexString and ToHexStringLower are supported too.

As on .NET, FromHexString accepts either case and raises FormatException
for an odd length or a non-hex character, and returns a Uint8Array like
FromBase64String. Null arguments raise ArgumentNullException and a bad
offset or length raises ArgumentOutOfRangeException. The tests'
raisedExceptionType helper now also reports ArgumentNullException and
FormatException.
…exString

Map them, including the offset and length overloads, to a new
fable_library convert module. It lives outside string_.py because it
raises .NET exceptions from system.py, which imports string_.py.

As on .NET, FromHexString accepts either case and raises FormatException
for an odd length or a non-hex character (bytes.fromhex alone would skip
whitespace), and returns a byte array. Null arguments raise
ArgumentNullException and a bad offset or length raises
ArgumentOutOfRangeException. The tests' raisedExceptionType helper now
also reports ArgumentNullException and FormatException.
…String

Map them, including the offset and length overloads, to new
toHexString, toHexStringLower and fromHexString functions in
String.dart, which raise the .NET exceptions from System.dart like
Async.dart does.

As on .NET, FromHexString accepts either case and raises FormatException
for an odd length or a non-hex character, and a bad offset or length
raises ArgumentOutOfRangeException. The tests' raisedExceptionType
helper now also reports ArgumentNullException and FormatException.
…String

Map them, including the offset and length overloads, to new
fable_convert functions that take byte arrays like to_base64, plus
arrays kept in the process dictionary (List.toArray), and use
binary:encode_hex and binary:decode_hex (OTP 24). The lowercase variant
lowercases the result, since binary:encode_hex/2 needs OTP 26.

As on .NET, FromHexString accepts either case and raises FormatException
for an odd length or a non-hex character, and returns a byte array like
from_base64. Null arguments raise ArgumentNullException and a bad offset
or length raises ArgumentOutOfRangeException. The tests'
raisedExceptionType helper now also reports ArgumentNullException and
FormatException.
fromHexString dropped a trailing digit, so "abc" gave [| 0xABuy |]. It
now panics like it does for a non-hex character, where .NET raises
FormatException.
@MangelMaxime
MangelMaxime merged commit 3a3acf4 into fable-compiler:main Oct 30, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants