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

@cdown cdown requested a review from jpakkane as a code owner November 16, 2025 11:03
@bonzini bonzini added this to the 1.11 milestone Nov 19, 2025
@cdown cdown force-pushed the cdown/2025-11-16/int_bases branch from ce586d4 to cf4e844 Compare November 28, 2025 09:05
@cdown cdown requested review from dcbaker and dnicolodi November 28, 2025 09:06
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 force-pushed the cdown/2025-11-16/int_bases branch from cf4e844 to d50cfec Compare November 28, 2025 19:46
return str(self.held_object).zfill(kwargs['fill'])
def to_string_method(self, args: T.List[TYPE_var], kwargs: 'ToStringKw') -> str:
format_codes = {"hex": "x", "oct": "o", "bin": "b", "dec": "d"}
return format(self.held_object, f'#0{kwargs["fill"]}{format_codes[kwargs["format"]]}')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return format(self.held_object, f'#0{kwargs["fill"]}{format_codes[kwargs["format"]]}')
return '{:#0{fill}{format}}'.format(self.held_object, fill=kwargs['fill'], format=format_codes[kwargs['format']])

class ToStringKw(TypedDict):

fill: int
format: Literal["dec", "hex", "oct", "bin"]
Copy link
Member

Choose a reason for hiding this comment

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

I think the quoting style in the meson code base is to use single quotes.

Comment on lines +135 to +147
s = self.held_object.strip()
s_unsigned = s.lstrip('+-').lower()

if s_unsigned.startswith('0x'):
base = 16
elif s_unsigned.startswith('0o'):
base = 8
elif s_unsigned.startswith('0b'):
base = 2
else:
base = 10

return int(s, base)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
s = self.held_object.strip()
s_unsigned = s.lstrip('+-').lower()
if s_unsigned.startswith('0x'):
base = 16
elif s_unsigned.startswith('0o'):
base = 8
elif s_unsigned.startswith('0b'):
base = 2
else:
base = 10
return int(s, base)
return int(s, base=0)

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