Skip to content

fix(conversions): reject non-hex digits in HexToOct.hexToDecimal - #7748

Merged
alxkm merged 1 commit into
TheAlgorithms:masterfrom
kevin9327:fix/hextooct-invalid-digit
Nov 11, 2026
Merged

alxkm merged 1 commit into
TheAlgorithms:masterfrom
kevin9327:fix/hextooct-invalid-digit

Conversation

@kevin9327

Copy link
Copy Markdown
Contributor

HexToOct.hexToDecimal looks each character up with indexOf and uses the result directly, so a character that is not a hex digit adds -1 instead of failing. The method then returns a wrong number with no error:

  • hexToDecimal("1G") returns 15 (16 * 1 + -1)
  • hexToDecimal("0x1A") returns -230
  • hexToOctal("Z") returns 0 (the -1 makes decimalToOctal skip its loop)

This change throws IllegalArgumentException for any character outside 0-9A-F (after the existing upper-casing), matching what OctalToHexadecimal.octalToDecimal already does for bad octal digits. Valid inputs give the same results as before.

HexToOctTest.testInvalidHexDigitThrows covers the three inputs above. It fails on master and passes with this change; the existing tests still pass.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations. (no new algorithm)
  • All new algorithms include a corresponding test class that validates their functionality. (test added to the existing class)
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

I used an AI assistant while preparing this change; I reviewed the code and ran the tests myself.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.34%. Comparing base (b2af1db) to head (91ad22a).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7748   +/-   ##
=========================================
  Coverage     82.33%   82.34%           
- Complexity     8444     8445    +1     
=========================================
  Files           843      843           
  Lines         26154    26156    +2     
  Branches       5109     5110    +1     
=========================================
+ Hits          21535    21537    +2     
  Misses         3830     3830           
  Partials        789      789           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kevin9327
kevin9327 marked this pull request as ready for review November 11, 2026 20:22

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, thanks for the contribution.

@alxkm
alxkm merged commit c3ce690 into TheAlgorithms:master Nov 11, 2026
7 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.

3 participants