NSData到NSString返回Null [英] NSData to NSString returns Null

查看:66
本文介绍了NSData到NSString返回Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过.但是仍然无法得到它.

I have searched. But still couldnt get it.

我正在将NSdata转换为NSString. 当我做[data description]; 它返回我< 00000000 31323334 35363738> 是的,我收到了我的字符串@"12345678".

I'm converting NSdata to NSString. When I do [data description]; it returns me <00000000 31323334 35363738> Yes, Im receiving my string @"12345678".

如何将其正确转换为NSString? 我尝试过

How do I convert it to NSString appropriately? I tried

NSString *b = [NSString stringWithUTF8String:[data bytes]];
NSString *a = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];

两个都返回null. 任何的想法? 谢谢

Both returns me null. Any idea? Thanks

大家好, 感谢您的所有建议. 它似乎是常量,因此始终在前面存在一个空字符. 因此,每当我收到一些东西时,我只需删除第一个< 00000000>,然后它就可以正常工作了

Hi all, Thanks for all suggestion. It appears to be constant whereby theres a null character infront always. So whenever I receive something, i just remove the first <00000000>, then its working fine already

推荐答案

如果编码不正确,就会发生这种情况.

This happens if the encoding is incorrect.

尝试使用ASCII进行测试. ASCII几乎肯定可以检索某种字符串.如果只是数字,它可能会起作用.

Try using ASCII to test out. ASCII almost certainly work to retrive somekind of string. If it's only numbers it will probably work.

NSString *a = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding];

除UTF-8编码外,最常见的是:

Most common except UTF-8 enconding is:

NSASCIIStringEncoding
NSUnicodeStringEncoding
NSISOLatin1StringEncoding
NSISOLatin2StringEncoding
NSSymbolStringEncoding

尝试一下,看看它们是否有效.

try them out and see if they work.

这篇关于NSData到NSString返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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