【问题标题】:How to Convert float to Char * in Objective c如何在Objective c中将float转换为Char *
【发布时间】:2012-08-07 08:35:06
【问题描述】:

我正在尝试将 float 转换为 int

float flot=2.1456;
char*  c [] = {(char *)flot};
ObjLen = sizeof(c)/sizeof(*c);
NSLog(@"ObjLen %d",ObjLen);  

但它显示错误'float'类型的操作数不能转换为指针类型

将 Char* 再次转换为浮动

【问题讨论】:

    标签: objective-c floating-point char type-conversion


    【解决方案1】:

    首先将float转换为NSString

    float flot=2.1456;
    NSString *str = [NSString stringWithFormat@"%f",flot];
    

    使用 -[NSString UTF8String]: 转换为 char *

    const char *c = [str UTF8String]
    

    【讨论】:

      【解决方案2】:

      我认为更好的方法是使用来自stdio.hsprintfsnprintf 方法。 sprintf/snprintf 比 @Prince 方法快 2 倍。

      【讨论】:

        猜你喜欢
        • 2015-08-30
        • 2012-08-16
        • 1970-01-01
        • 2011-07-05
        • 2011-12-22
        • 1970-01-01
        • 2022-08-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多