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

String(unsigned long) constructor reverses digits, e.g. 10 becomes "01" #5585

Closed
marcboon opened this issue Aug 24, 2021 · 3 comments
Closed

Comments

@marcboon
Copy link

marcboon commented Aug 24, 2021

Arduino 1.8.15
ESP32 v2.0.0-rc-1
ESP32-S2-WROVER (Saola 1)

String() constructor from unsigned long reverses digits.
Code:

void setup() {
  Serial.begin(115200);
  delay(100);
    
  for(unsigned long ul = 0; ul < 25; ul++) {
    Serial.println(String(ul)); 
  }
}

void loop() {
}

Output:

0
1
2
3
4
5
6
7
8
9
01
11
21
31
41
51
61
71
81
91
02
12
22
32
42

After some more testing I found that the problem is actually with ultoa(), and also ltoa(). The String(unsigned long) constructor uses ultoa() for the conversion, while String(long) uses sprintf("%ld") when base is 10 (which works as expected), and ltoa() for other cases, which fail.
So, both ltoa() and ultoa() reverse digits.

  unsigned long value = 123456789;
  char buf[10];
  ultoa(value, buf, 10);
  Serial.print(buf);

produces "987654321"

@me-no-dev
Copy link
Member

try RC2

@marcboon
Copy link
Author

try RC2

I didn't realize it was released already. Thanks, will do!

@marcboon
Copy link
Author

Fixed in RC2

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

No branches or pull requests

2 participants