序列化后字符放到数组

来源:互联网 发布:java script实例 编辑:程序博客网 时间:2024/06/16 12:18

//

//  ViewController.m

//  FileMessage

//各位亲 有时间可以去看看我的  “金骏家居淘宝店” http://jinjun1688.taobao.com/shop/view_shop.htm?tracelog=twddp 买时说明在我的博客看到有优惠哦 还有意外礼品赠送  真正的程序员淘宝店

//  Created by chengen on 13-7-30.

//  Copyright (c) 2013 chengen. All rights reserved.

//


#import "ViewController.h"

#import "File.pb.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

   ptro pb;

    pb.set_id(100);

    pb.set_str("hello");

    pb.add_id_num(101);

    

    

    //提前获取序列化所占空间得大小,从而避免多次分配造成的性能开销

//    int length=pb.ByteSize();

//    NSLog(@"pb_size==%d",length);

//    

//    std::string buf;

//    pb.SerializeToString(&buf);

//    NSLog(@"pb_string==%s",buf.data());

    

   int length=pb.ByteSize();

   NSLog(@"length==%d",length);

   char* buf=newchar[length];

    pb.SerializeToArray(buf, length);

   NSLog(@"buf==%s",buf);

    

//    int buf_size=strlen((const char*)buf.data());

//    NSLog(@"pb_StrSize==%d",buf_size);

//    

//    pb.ParseFromString(buf);

//    NSLog(@"pb=%d,,%s,,%d",pb.id(),pb.str().data(),pb.id_num(0));

   ptro pb1;

    pb1.ParsePartialFromArray(buf, length);

    

   NSLog(@"pb=%d,,%s,,%d",pb.id(),pb.str().data(),pb.id_num(0));

   int length1=pb.ByteSize();

   NSLog(@"pb_size==%d",length1);

   delete [] buf;

    

    

    

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end