2012-04-23 26 views
0

我想发送struct给GameCenter中的其他玩家。 我已阅读关于此的其他问题,但是,我无法让他们中的任何人工作。NSString * to Char [](GameCenter Send Struct)

我需要得到@"1234"char[4](例如炭[0] = '1',炭[1] = '2',等等)

我试图[NSString UTF8String],但它不似乎不做我想做的事。

它指定罚款,但是当我把它放回NSString *[NSString stringWithUTF8String:],它返回空白。

如果有人可以告诉我转换和转换,它将不胜感激。

谢谢。

编辑:

我无法得到它的工作:/这是我的代码(删节版):

Matchmaker.h

enum { NChars = 4 }; 

typedef struct { 
    MessageType messageType; 
} Message; 

typedef struct { 
    Message message; 
    char code[NChars]; 
} MessageGameCode; 

@interface Matchmaker : CCLayer <GameCenterMasterDelegate>{ 

    NSString *_code; 
} 
@property (nonatomic,retain) NSString *_code; 

红娘.m

@synthesize _code; 
-(void)viewDidLoad{ 
    self._code = @"1234"; 
} 

- (void)sendCode { 
    NSLog(@"Sending Code...."); 
    MessageGameCode message; 
    message.message.messageType = kMessageTypeGameCode; 
    NSString * const source = self._code; 

    const char* const sourceAsUTF8 = source.UTF8String; 

    for (size_t idx = 0; idx < NChars; ++idx) { 
     message.code[idx] = sourceAsUTF8[idx]; 
    } 
    NSData *data = [NSData dataWithBytes:&message length:NChars];  
    [self sendData:data]; 
} 

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID { 
    Message *message = (Message *) [data bytes]; 
    if (message->messageType == kMessageTypeGameCode) {   

     MessageGameCode *codeMessage = (MessageGameCode *)[data bytes]; 
     self._code = [[NSString alloc] initWithBytes:codeMessage->code length:NChars encoding:NSUTF8StringEncoding]; 
     [self setGameState:kGameStateWaitingForStart]; 
     NSLog(@"Game Code Recieved"); 
     NSLog(@"Recieved Code: %@",self._code); //This always shows self._code as blank 

    } 
} 

回答

7

您的尝试将失败,因为您传递给+ [NSString stringWithUTF8String:]的cstring未终止。

试试这个:

NSString * result = [[NSString alloc] initWithBytes:bytes 
              length:4 
              encoding:NSUTF8StringEncoding]; 

编辑

更完整的演示:

enum { NChars = 4 }; 
/* requires error checking */ 
void transmit() { 
    NSString * const source = @"1234"; 

    char tmp[NChars] = { 0 }; 
    const char* const sourceAsUTF8 = source.UTF8String; 

    for (size_t idx = 0; idx < NChars; ++idx) { 
     tmp[idx] = sourceAsUTF8[idx]; 
    } 
    /* .. */ 
} 

/* requires error checking */ 
void receive(const char bytes[NChars]) { 
    NSString * result = [[NSString alloc] initWithBytes:bytes length:NChars encoding:NSUTF8StringEncoding]; 
    /* ... */ 
} 
+1

好吧,'-UTF8String'的结果被终止了,但是如果他将它复制到一个缓冲区而不允许终止空间的话,那将会是一个问题。 – 2012-04-23 15:23:37

+1

@DavidGelhar完全(+1)。尽管那*是我在原始答案中指定的缓冲区,但我已经添加了一个更完整的说明来澄清。 – justin 2012-04-23 16:09:43

+0

我仍然无法完成它的工作。我想我错过了一些超级明显的东西。我用我的代码编辑了我的帖子。谢谢您的帮助。 – 2012-04-23 22:31:54

1

一种方法是

char bytes[4]; 

NSData* data = [@"1234" dataUsingEncoding: NSUTF8StringEncoding]; 
if ([data length] <= 4) 
{ 
    memcpy(bytes, [data bytes], [data length]); 
} 

和走另一条路:

NSString* recodedString = [[NSString alloc] initWithBytes: bytes 
                length: savedLengthFromBefore 
               encoding: NSUTF8StringEncoding]; 
1

有几个可能的陷阱在这里。

一个是与[NSString UTF8String]"The returned C string is automatically freed just as a returned object would be released; you should copy the C string if it needs to store it outside of the autorelease context in which the C string is created."。所以,这取决于你多久期待值留下来,你可能需要复制它(例如,使用strcpy

的另一个问题是,[NSString UTF8String][NSString stringWithUTF8String:]都期待NULL终止的C字符串,所以你需要一个char [5],而不是char [4]来保存@“1234”。