-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add conversion between a string hexadecimal value and integer #5569
Add conversion between a string hexadecimal value and integer #5569
Conversation
result, err := strconv.ParseFloat(value, 64) | ||
if err != nil { | ||
return 0, false | ||
if strings.HasPrefix(value, "0x") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this check, just try ParseInt and then fallback to ParseFloat if it fails, this will give us octal support and allow 0XFF
to work as well. Also, can you add a similar conversion for the unsigned type as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @danielnelson , thanks for you advices. That's now done (refactoring + string hexa to uint converter). Unfortunately, there is one issue in the CI. CircleCI is OK but Appveyor failed. I may be wrong but I think it's not because of my code (the error is failed to write dep tree: failed to export golang.org/x/crypto: (1) failed to list versions for https://go.googlesource.com/crypto: fatal: remote error: Internal Server
🤔 ). Am I wrong ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that, just having some issues lately with our builds on appveyor, I'm sure its unrelated.
Required for all PRs:
This commit add a conversion between a string hexadecimal (e.g: '0x2A') and an integer (e.g: 42). It's some change I needed because, for a project, I need to consume metrics from an HTTP endpoint. Those metrics are in hexadecimal.
I did not modified the README yet (not sure I had to mention this new case). I can do it if needed.
If someone sees how to make this commit "cleaner" in term of how it's coded, feel free to give your advices ! 😃