Skip to content
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

Merged
merged 2 commits into from
Mar 11, 2019
Merged

Add conversion between a string hexadecimal value and integer #5569

merged 2 commits into from
Mar 11, 2019

Conversation

dsaingre
Copy link
Contributor

Required for all PRs:

  • Signed CLA.
  • Associated README.md updated.
  • Has appropriate unit tests.

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 ! 😃

result, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, false
if strings.HasPrefix(value, "0x") {
Copy link
Contributor

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?

Copy link
Contributor Author

@dsaingre dsaingre Mar 11, 2019

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 ?

Copy link
Contributor

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.

@danielnelson danielnelson added this to the 1.11.0 milestone Mar 11, 2019
@danielnelson danielnelson added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label Mar 11, 2019
@danielnelson danielnelson merged commit 88e0cb1 into influxdata:master Mar 11, 2019
otherpirate pushed a commit to otherpirate/telegraf that referenced this pull request Mar 15, 2019
otherpirate pushed a commit to otherpirate/telegraf that referenced this pull request Mar 15, 2019
dupondje pushed a commit to dupondje/telegraf that referenced this pull request Apr 22, 2019
hwaastad pushed a commit to hwaastad/telegraf that referenced this pull request Jun 13, 2019
athoune pushed a commit to bearstech/telegraf that referenced this pull request Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants