Skip to content

Add uint to char functions #15244

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

Merged
merged 9 commits into from
Nov 22, 2021
Merged

Add uint to char functions #15244

merged 9 commits into from
Nov 22, 2021

Conversation

drashna
Copy link
Member

@drashna drashna commented Nov 21, 2021

Description

Adds functions to convert an unsigned int to a char array. Useful for OLEDs and displays, especially on AVR, as things like sprintf take up ~1.5kB of firmware space.

And instead of continually adding the same block of code, add a function into core to do this.

Types of Changes

  • Core
  • Enhancement/optimization
  • Documentation

Checklist

  • My code follows the code style of this project: C, Python
  • I have read the PR Checklist document and have made the appropriate changes.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@drashna drashna requested review from tzarc and a team November 21, 2021 00:28
@github-actions github-actions bot added the core label Nov 21, 2021
Comment on lines 513 to 515
buf[3] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[2] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[1] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
buf[3] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[2] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[1] = (curr_num /= 10) % 10 ? '0' + (curr_num) % 10 : (curr_num / 10) % 10 ? '0' : curr_pad;
buf[3] = (curr_num /= 10) ? '0' + curr_num % 10 : curr_pad;
buf[2] = (curr_num /= 10) ? '0' + curr_num % 10 : curr_pad;
buf[1] = (curr_num /= 10) ? '0' + curr_num % 10 : curr_pad;

The original code would not work properly for a number like 1001 (with at least two consecutive zeros); the whole remaining number needs to be tested when deciding whether the zero needs to be replaced with the padding character.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, went through a number of iterations, the current code should work properly, but please take a look at it.

@tzarc tzarc mentioned this pull request Nov 22, 2021
8 tasks
@tzarc tzarc changed the base branch from master to develop November 22, 2021 16:42
Copy link
Member

@tzarc tzarc left a comment

Choose a reason for hiding this comment

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

The static buffer note needed to be placed on the other APIs. My bad.
We should also put the function declarations into quantum.h.

drashna and others added 2 commits November 22, 2021 08:55
@tzarc tzarc merged commit 32a87d3 into qmk:develop Nov 22, 2021
@drashna drashna deleted the core/strings branch November 23, 2021 00:07
yokada-code added a commit to yokada-code/qmk_firmware that referenced this pull request Aug 18, 2023
* Add uint to char functions

* appease the all mighty lint

* Further appease Lint

* Update functions

* Add doxygen comment

* Update quantum/quantum.c

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Apply suggestions from code review

Co-authored-by: Nick Brassel <nick@tzarc.org>

* Add declaration for get_numeric_string

* fix formatting and bug

Co-authored-by: Nick Brassel <nick@tzarc.org>

(Cherry-pick from: 32a87d3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants