Repository navigation
Conversation
`to_base` returned the raw input string whenever the input base equaled the output base, so `cast to-hex --base-in 16 ff` printed `ff` without a prefix, `cast to-hex 0x00ff` kept leading zeros and the `0X`/`+` forms, and `cast to-hex -0x1` printed `-0x1` instead of the two's complement that `cast to-hex -1` prints. Always format the parsed value instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ara-stock
requested review from
0xrusowsky,
DaniPopes,
figtracer,
grandizzy,
mablr,
mattsse and
stevencartavia
as code owners
October 7, 2026 02:01
stevencartavia
left a comment
Member
There was a problem hiding this comment.
should we keep same-base output unchanged? @mattsse @grandizzy
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
cast to-base/to-hex/to-decreturn the input string unchanged whenever the input base equals the output base, becauseto_baseshort-circuits withreturn Ok(value.to_string()). The input is parsed (and validated) but the parsed value is thrown away, so the output is not normalized and is inconsistent with every other path:(reproduced with cast 1.8.3 and on master a57bf18)
The negative case is the most surprising: the same number gives a two's-complement hex word when written in decimal, but a signed hex string when written in hex.
Solution
Drop the
base_in == base_outearly return into_base, so the parsedNumberWithBaseis always formatted with{:#?}. The input is already parsed before the early return, so this only changes the output, which is now the same as converting from any other base:Added
to_base_same_base_normalizesincrates/cast/tests/cli/conversions.rs. It fails with theargs.rschange reverted and passes with it.Note: leading zeros in a same-base input are no longer kept (
0x00ff->0xff), matching what happens for a different-base input (cast to-hex 255->0xff).cast to-uint256/cast padremain the way to get fixed-width output.Tests run locally:
cargo test -p cast --test cli -- conversions::(46 passed),cargo clippy -p cast --all-targets(no warnings in cast), rustfmt check on the changed files.PR Checklist
I used an AI assistant while preparing this; I reproduced the bug and ran the tests myself.
🤖 Generated with Claude Code