开发者

How can i convert CGFloat to an Integer Value (iPhone SDK)

My problem is: I'm trying to convert my CGFloat Values to an integer value and than add them to an NSMutableArray.

For Example;

CGFloat x=ship.center.x

//convert x to integer ?

P.S.:After that, I will send these values to another iPhone with bluetooth. But first I must solve this problem. İf anyone knows how to send an array data to another iPhone with blu开发者_如何学编程etooth, it also works :)


Firstly, why do you want to convert to int? CGfloat is just a typedef for float, and can be sent as-is, especially if you know that your sending iPhone to iPhone, since they will use the same memory representation for floats. If you still need to do this for some reason...

// Choose constants m and c so that the resulting integers
// span as much of the range (INT_MIN, INT_MAX) as possible,
// thus minimising aliasing effects. Also consider using
// long long (64-bit integers).
int x = (int)(m*ship.center.x+c)

NSMutableArray only stores objects, so you might want to use a simple array instead:

int* arr = malloc(sizeof(x)*ships.length);
for (int i = 0; i < ships.length; i++) {
    arr[i] = (int)ships[i].center.x;
}
send_data(arr, sizeof(x)*ships.length);

I've never programmed with a Bluetooth stack, so I can't help you there. Sorry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜