Apple print input format objective-C

Source: Internet
Author: User
Tags format definition

From http://blog.kungfusoft.com/programming/iphone/topic-281.html

When using functions such as nslog and [nsstring stringwithformat:], they all work based on C/C ++-style string formatting.

Originally, C/C ++ has never been used. In iPhone development and development, the NS series objects are added, and the formatted output is even more messy.
After reading the documentation of programming guide for cocoa, I still have a detailed description.

Format definition
The format specifiers supported by the nsstring formatting methods and cfstring formatting functions follow the IEEE printf Specification; The specifiers are summarized in table 1. note that you can also use the "N $" positional specifiers such as % 1 $ @ % 2 $ S.
For more details, see the IEEE printf specification. You can also use these format specifiers with the nslog function.

Table 1 format specifiers supported by the nsstring formatting methods and cfstring formatting Functions
Definition Description
% @ Objective-C object, printed as the string returned by descriptionwithlocale: If available, or description otherwise. also works with cftyperef objects, returning the result of the cfcopydescription function.
% '%' Character
% D, % d, % I Signed 32-bit integer (INT)
% U, % u Unsigned 32-bit integer (unsigned INT)
% Hi Signed 16-bit integer (short)
% Hu Unsigned 16-bit integer (unsigned short)
% Qi Signed 64-bit integer (long)
% Qu Unsigned 64-bit integer (unsigned long)
% X Unsigned 32-bit integer (unsigned INT), printed in hexadecimal using the digits 0-9 and lowercase A-F
% X Unsigned 32-bit integer (unsigned INT), printed in hexadecimal using the digits 0-9 and uppercase A-F
% QX Unsigned 64-bit integer (unsigned long), printed in hexadecimal using the digits 0-9 and lowercase A-F
% QX Unsigned 64-bit integer (unsigned long), printed in hexadecimal using the digits 0-9 and uppercase A-F
% O, % O Unsigned 32-bit integer (unsigned INT), printed in octal
% F 64-bit floating-point number (double)
% E 64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent
% E 64-bit floating-point number (double), printed in scientific notation using an uppercase e to introduce the exponent
% G 64-bit floating-point number (double), printed in the style of % E if the exponent is less than-4 or greater than or equal to the precision, in the style of % F otherwise
% G 64-bit floating-point number (double), printed in the style of % E if the exponent is less than-4 or greater than or equal to the precision, in the style of % F otherwise
% C 8-bit unsigned character (unsigned char), printed by nslog () as an ASCII character, or, if not an ASCII character, in the octal format \ DDD or the Unicode hexadecimal format \ udddd, where D is a digit
% C 16-bit Unicode character (unichar), printed by nslog () as an ASCII character, or, if not an ASCII character, in the octal format \ DDD or the Unicode hexadecimal format \ udddd, where D is a digit
% S Null-terminated array of 8-bit unsigned characters. % s interprets its input in the system encoding rather than, for example, UTF-8.
% S Null-terminated array of 16-bit Unicode characters
% P Void pointer (void *), printed in hexadecimal with the digits 0-9 and lowercase A-F, with a leading 0x
% L Length modifier specifying that a following a, A, E, E, F, F, G, or G conversion specifier applies to a long double argument
% 64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase P to introduce the exponent
% 64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a uppercase P to introduce the exponent
% F 64-bit floating-point number (double), printed in decimal notation
% Z Length modifier specifying that a following D, I, O, U, X, or x conversion specifier applies to a size_t or the corresponding signed integer type argument
% T Length modifier specifying that a following D, I, O, U, X, or x conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument
% J Length modifier specifying that a following D, I, O, U, X, or x conversion specifier applies to a intmax_t or uintmax_t argument

Platform dependency
Mac OS X uses several data types-nsinteger, nsuinteger, cgfloat, and cfindex-to provide a consistent means of representing values in 32-and 64-bit environments. in a 32-bit environment, nsinteger and nsuinteger are defined as int and unsigned int, respectively.
In 64-bit environments, nsinteger and nsuinteger are defined as long and unsigned long, respectively. to avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in table 2. note that in some
Cases you may have to cast the value.

Table 2 format specifiers for Data Types
Type Definition Suggestions
Nsinteger % LD or % LX Cast the value to long
Nsuinteger % Lu or % LX Cast the value to unsigned long
Cgfloat % F or % G % F works for floats and doubles when formatting; but see below warning when scanning
Cfindex % LD or % LX The same as nsinteger
Pointer % P % P adds 0x to the beginning of the output. If you don't want that, use % lx and cast to long.
Long long % LLD or % LLX Long long is 64-bit on both 32-and 64-bit platforms
Unsigned long % LlU or % LLX Unsigned long is 64-bit on both 32-and 64-bit platforms

The following example extends strates the use of % LD to format an nsinteger and the use of a cast.

1
2
Nsinteger I = 42;
Printf ("% LD \ n", (long) I );

In addition to the considerations mentioned in Table 2, there is one extra case with scanning: You must distinguish the types for float and double. you shoshould use % F for float, % lf for double. if you need to use scanf (or a variant thereof) with cgfloat, switch
To double instead, and copy the double to cgfloat.

1
2
3
4
Cgfloat imagewidth;
Double TMP;
Sscanf (STR, "% lf", & amp; TMP );
Imagewidth = TMP;

It is important to remember that % lf does not represent cgfloat correctly on either 32-or 64-bit platforms. This is unlike % lD, whworks ich for long in all cases.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.