Pages: [1]   Go Down
Author Topic: Strange behavior with int and Serial.print  (Read 481 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi! Got a short program write ints to the serial port and I get a very strange behavior. My guess is that arduino think test2 and test3 is in OCT for because the 0 in the start. Can I specify that it is DEC like 0x in HEX or 0b BIN? And is there any good way to convert a DEC to OCT in arduino? (Not in the print function)


Code:
void setup() {                
  Serial.begin(57600);

  int test1 = 23;
  int test2 = 023;
  int test3 = 0023;
  
  Serial.println(test1);
  Serial.println(test2);
  Serial.println(test3);

}
Output:
Code:

23
19
19
« Last Edit: June 14, 2013, 04:02:13 pm by Aladdin » Logged

BCC AZ USA
Offline Offline
Edison Member
*
Karma: 34
Posts: 1157
It gets hot so it must be working
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the leading zero (0) is for octal (like 0x for hex).  23 octal is 19 decimal.  (8*2)+3
« Last Edit: June 14, 2013, 04:10:05 pm by groundfungus » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can I specify that it is DEC like 0x in HEX or 0b BIN?
No. Numbers with leading 0s are assumed to be base 10. What is the point of having the leading 0? If it didn't change the base, it would not change the value stored. So, that is the point?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use literal formatting for this. E.g.

  • 22L is of type LONG
  • B001 is of type BYTE
  • 2f is of type FLOAT

More info:
http://arduino.cc/en/Reference/IntegerConstants and Google.. Just search for literal formatting
Logged

Pages: [1]   Go Up
Jump to: