iOS数据类型转换
// String str = "你好啊,aBc!"; // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // DataOutputStream outputstream = new DataOutputStream(baos); // outputstream.writeChars(str); // // byte[] contents = baos.toByteArray(); // for (int i = 0; i < contents.length; i++) { // System.out.println("contents:"+contents[i]); // } // // NSString *str = @"你好啊,aBc!"; // NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(NSUTF16BigEndianStringEncoding); // NSData *data = [str dataUsingEncoding:enc]; // // Byte *byte = (Byte *)[data bytes]; // for (int i=0 ; i<[data length]; i++) { // NSLog(@"byte = %d",byte[i]); // }
iOS中,将NSData转NSString的一般方法为[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];但是当data中包含00时,所获得的新字符就会为nil,这时我们应该这样转
[NSString stringWithUTF8String:[data bytes]];狗日的骗老子
NSString *testString = @"妈妈"; NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding]; Byte *testByte = (Byte *)[testData bytes]; NSMutableArray *byte = [NSMutableArray array]; for(int i=0;i<[testData length];i++) //printf("testByte = %d\n",testByte[i]) // Byte *byte[i] = testByte[i]; [byte addObject:@(testByte[i])]; NSLog(@"array=%@",byte);
正解