Skip to content

Conversation

@cdown
Copy link

@cdown cdown commented Nov 16, 2025

Add support for parsing and formatting integers in hexadecimal, octal, and binary representations to complement the existing decimal support.

str.to_int() now accepts strings with 0x/0o/0b prefixes and optional leading signs (+/-), enabling direct parsing of hex, octal, and binary literals while maintaining full backward compatibility with decimal strings including those with leading zeros.

int.to_string() gains a new format keyword argument that accepts 'dec', 'hex', 'oct', or 'bin', allowing integers to be formatted with appropriate prefixes (0x, 0o, 0b). The format parameter works correctly with the existing fill parameter and handles negative numbers properly.

Round-trip conversions are fully supported: values formatted with
int.to_string(format: 'hex') can be parsed back with str.to_int().

Fixes: #2047
Fixes: #15201

Add support for parsing and formatting integers in hexadecimal, octal,
and binary representations to complement the existing decimal support.

str.to_int() now accepts strings with 0x/0o/0b prefixes and optional
leading signs (+/-), enabling direct parsing of hex, octal, and binary
literals while maintaining full backward compatibility with decimal
strings including those with leading zeros.

int.to_string() gains a new format keyword argument that accepts 'dec',
'hex', 'oct', or 'bin', allowing integers to be formatted with
appropriate prefixes (0x, 0o, 0b). The format parameter works correctly
with the existing fill parameter and handles negative numbers properly.

Round-trip conversions are fully supported: values formatted with
int.to_string(format: 'hex') can be parsed back with str.to_int().

Fixes: mesonbuild#2047
Fixes: mesonbuild#15201
@cdown cdown requested a review from jpakkane as a code owner November 16, 2025 11:03
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.

RFE: a way to format integers in hex String '0x0001' cannot be converted to int

1 participant