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
Comment on lines +64 to +68
validator=lambda x: (
'format must be "dec", "hex", "oct", or "bin"'
if x not in ("dec", "hex", "oct", "bin")
else None
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the in_set_validator({...}) for this.

)
@noPosargs
@InterpreterObject.method('to_string')
def to_string_method(self, args: T.List[TYPE_var], kwargs: T.Dict[str, T.Any]) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're here, can you add a TypedDict to use as the type for kwargs here? there's plenty of examples, preferably with a Literal[] for the format.

Comment on lines +79 to +101
if format_type == "dec":
result = str(self.held_object)
if fill > 0:
result = result.zfill(fill)
else:
result = format(self.held_object, format_codes[format_type])

if fill > 0:
if result.startswith("-"):
sign = "-"
prefixed = result[1:]
else:
sign = ""
prefixed = result

prefix = prefixed[:2]
digits = prefixed[2:]

total_prefix_len = len(sign) + len(prefix)
if fill > total_prefix_len:
digits = digits.zfill(fill - total_prefix_len)

result = sign + prefix + digits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this all be implemented as:

template = '0=#{}{}'.format(fill, format_codes[format_code))
return format(self.held_object, template)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even easier:

return '{0:#0{fill}{format}}'.format(self.held_object, fill=fill, format=format_codes[format_code])

@bonzini bonzini added this to the 1.11 milestone Nov 19, 2025
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

4 participants