NSData字节转换为字符串 [英] NSData bytes to string

查看:58
本文介绍了NSData字节转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的NSData字节看起来像这样:

I get NSData of bytes that look like this:

    2d2d2d2d 2d2d2d2d 2d2d2d2d 2d2d2d2d 2d2d2d2d 2d2d2d2d 2d2d2d2d 2d353731 35343039 37373139
    34383437 34303832 30333533 30383232 380d0a43 6f6e7465 6e742d44 6973706f 73697469 6f6e3a20 
    666f726d 2d646174 613b206e 616d653d 2266696c 65223b20 66696c65 6e616d65 
    3d224265 61636820 426f7973 202d2047 6f6f6420 56696272 6174696f 6e732e6d 

并且我想将其转换为NSString,我尝试了此方法,但是它给了我零的字符串:

and i want to convert it to NSString, i tried this method but it give me a nil to the string:

NSString* postInfo = [[NSString alloc] initWithBytes:[postDataChunk bytes] length:[postDataChunk length] encoding:NSUTF8StringEncoding];

推荐答案

您可以使用

NSString* newStr = [[NSString alloc] initWithData:theData
                                         encoding:NSUTF8StringEncoding];

如果数据以空值结尾,则应改用

If the data is null-terminated, you should instead use

NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];

有关更多参考,请参见以下链接:

for further reference see these links:

将UTF-8编码的NSData转换为NSString

NSString类参考

http://homepage.mac.com/mnishikata/objective-c_memo/convert_nsdata_to_nsstring_.html

这篇关于NSData字节转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆