Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7825051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:49:03+00:00 2026-06-02T08:49:03+00:00

I want to send a struct to another player in GameCenter. I have read

  • 0

I want to send a struct to another player in GameCenter.
I have read the other questions about this, however, I cannot get any of them to work.

I need to get @"1234" into a char[4] (ex char[0] = ‘1’, char[1] = ‘2’, etc)

I have tried [NSString UTF8String], but it doesn’t seem to do what I want.

It assigns fine, but when I pull it back into NSString * with [NSString stringWithUTF8String:], It returns blank.

If someone could show me the conversion to and from, it would be greatly appreciated.

Thanks.

EDIT:

I can’t get it to work :/ Here is my code (the abridged version):

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;

Matchmaker.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

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:49:07+00:00Added an answer on June 2, 2026 at 8:49 am

    Your attempt will fail because the cstring which you pass to + [NSString stringWithUTF8String:] is not terminated.

    Try this:

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

    Edit

    A more complete demonstration:

    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];
        /* ... */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use windows's message queue facilities to send a struct to another
I have a client and server program where I want to send an entire
I want to send a certain string on my serial port and read the
Possible Duplicate: Send NSString via Game Center i have been working on this for
I want to send a struct in C# to C++ using sockets. For example,
I want to have a message send & receive through 2 uni-direction FIFO Flow
I want to send OSC messages from iphone to another programme (max/msp) by creating
I want to send keyboard input to a window in another process, without bringing
I want to to use MFMailComposeViewController to send an email, and I already have
I want send e-mail with some images in content. I think I must attached

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.