edward0429 / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

Way to specify number of digits / places when printing hex, octal, and binary numbers. #549

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Serial.print(n, HEX, 3); // for example

Original issue reported on code.google.com by dmel...@gmail.com on 24 May 2011 at 7:34

GoogleCodeExporter commented 9 years ago
This needs a lot more specification in order to be implementable.

For instance, when formatting the value 0xff and "3 places", printf can give " 
ff", "ff ", and "0ff" (formats %3x, % 3x and %.3x respectively).

Similarly, when formatting 0x1234 to 3 places, do you want "1234", "123" or 
"234"? (incidentally, from printf you can only get 1234, at least as far as my 
testing went)

With decimal numbers you also have the sign of the number to contend with, 
increasing the number of things someone could possibly want.  printf provides 
at least 7 functionally different ways to format decimal numbers with 3 places:

%3d    : 99:    :999:    :9999:   : -9:    :-99:    :-999:   :-9999: 
%03d   :099:    :999:    :9999:   :-09:    :-99:    :-999:   :-9999: 
%-3d   :99 :    :999:    :9999:   :-9 :    :-99:    :-999:   :-9999: 
%+3d   :+99:    :+999:   :+9999:  : -9:    :-99:    :-999:   :-9999: 
% 3d   : 99:    : 999:   : 9999:  : -9:    :-99:    :-999:   :-9999: 
%- 3d  : 99:    : 999:   : 9999:  :-9 :    :-99:    :-999:   :-9999: 
%.3d   :099:    :999:    :9999:   :-009:   :-099:   :-999:   :-9999: 

…then there are even modes you can't specify with just a single number.  For 
instance, "%3.2d" gives " 09", " 99" and "999".

I'm not suggesting that arduino must necessarily be as complex as printf.  
Simply that more specification is required for this issue to be implementable.  
Part of that specification would include deciding which one or more of these 
behaviors (or behaviors inspired by other number-formatting facilities such as 
C++ iostreams) to actually add to arduino.

Original comment by jep...@gmail.com on 25 Jun 2011 at 2:21

GoogleCodeExporter commented 9 years ago
Wow, thanks for the comprehensive reference!

This is primarily intended for hex, binary, and octal numbers, and I think the 
most useful format is: 0ff.  Let's not truncate, so write(0x1234, HEX, 3)  
should give 1234.  

Original comment by dmel...@gmail.com on 18 Aug 2011 at 3:37

GoogleCodeExporter commented 9 years ago
Is there a reason not to just have a Serial.printf() method that takes C89 
syntax format strings?

Original comment by ads...@gmail.com on 19 Nov 2011 at 6:02

GoogleCodeExporter commented 9 years ago

Original comment by dmel...@gmail.com on 16 Dec 2011 at 10:08

GoogleCodeExporter commented 9 years ago
Issue 767 has been merged into this issue.

Original comment by dmel...@gmail.com on 2 Jan 2012 at 6:12