Objective-C中的字符串格式化输出

uint64的格式化请用%llu   , int64的格式化请用%lld

格式定义

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

定义说明

%@                         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 long)

%qu                         Unsigned 64-bit integer (unsigned long 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 long), printed in hexadecimal using the digits 0–9 and lowercase a–f

%qX                         Unsigned 64-bit integer (unsigned long 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 orthe 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

%a                           64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit

                               before the decimalpoint using a lowercase p to introduce the exponent

%A                          64-bit floating-point number (double), printed in scientificnotation 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

平台依赖

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

类型定义建议

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 long   %llu or %ll          xunsigned long long is 64-bit on both 32- and 64-bit platforms


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,732评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,496评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,264评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,807评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,806评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,675评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,029评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,683评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,704评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,666评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,773评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,413评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,016评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,204评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,083评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,503评论 2 343

推荐阅读更多精彩内容