如何将 uint8[] 转换为字符串?
你好 我正在开发 iPhone 应用程序 我在缓冲区中收到一些数据类型为 Uint8 的服务器响应 现在我想将我的缓冲区与字符串匹配,任何人都可以告诉我如何检查它吗?给出了一段代码,
void receiveData(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
CFDataRef df = (CFDataRef) data;
int len = CFDataGetLength(df);
if(len <= 0) return;
CFRange range = CFRangeMake(0,len);
UInt8 buffer[len];
NSLog(@"Received %d bytes from socket %d\n",
len, CFSocketGetNative(s));
CFDataGetBytes(df, range, buffer);
NSLog(@"Client received: %s\n", buffer);
NSLog(@"As UInt8 coding: %@", df);
[mTextViewAlias setText:[NSString stringWithUTF8String: buffer]];
////////////.............////////////
LoginViewController *lvc = [[LoginViewController alloc] init];
NSString *search = @"ACPT";
NSRange match;
match = [buffer rangeOfString: search];
if (match.location == NSNotFound)
NSLog (@"Invalid Username Or Password");
else
{
[lvc goToWatchList];
NSLog (@"match found at index %i", match.location);
}
}
任何人都知道吗? 我会非常感激
Hi
I am doing work on an iPhone application
I am getting some response of server in my buffer with data type Uint8
now i want to Match my buffer with a string, can any body tell me how can i check it? a piece of code is given
void receiveData(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
CFDataRef df = (CFDataRef) data;
int len = CFDataGetLength(df);
if(len <= 0) return;
CFRange range = CFRangeMake(0,len);
UInt8 buffer[len];
NSLog(@"Received %d bytes from socket %d\n",
len, CFSocketGetNative(s));
CFDataGetBytes(df, range, buffer);
NSLog(@"Client received: %s\n", buffer);
NSLog(@"As UInt8 coding: %@", df);
[mTextViewAlias setText:[NSString stringWithUTF8String: buffer]];
////////////.............////////////
LoginViewController *lvc = [[LoginViewController alloc] init];
NSString *search = @"ACPT";
NSRange match;
match = [buffer rangeOfString: search];
if (match.location == NSNotFound)
NSLog (@"Invalid Username Or Password");
else
{
[lvc goToWatchList];
NSLog (@"match found at index %i", match.location);
}
}
any body have some idea?
i'll be very thankful
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的响应可能是
UInt8
但您不需要将其复制到UInt8[]
中,因为您已经在CFDataRef
中拥有它(假设您传入的void *
确实是一个CFDataRef
,如果不是,您还有其他问题...) -CFDataRef
是收费的- 免费桥接NSData
(即您可以从一个转换为另一个),并且您可以直接从NSData
中的字节构造一个NSString
。您的代码,删除日志记录并添加直接 NSString 转换:或者,至少在您的示例代码中:不保存转换后的缓冲区,使用文字字符串进行搜索,并且使用
%s
意味着数据以空终止;您可以直接使用 C 函数完成所有这些操作,而无需创建中间对象:Your response may be
UInt8
but you don't need to copy it into aUInt8[]
as your already have it in aCFDataRef
(assuming thevoid *
you pass in really is aCFDataRef
, if it isn't you have other problems...) - aCFDataRef
is toll-free bridged withNSData
(i.e. you can just cast from one to the other), and you can construct anNSString
directly from the bytes in anNSData
. Your code, removing the logging and adding direct NSString conversion:Alternatively as, at least in your sample code: are not saving the converted buffer, are using a literal string for the search, and your use of
%s
implies the data is null-terminated; you could do all this directly with C functions without creating intermediate objects:您在找这个吗?
Are you looking for this?
出现警告是因为
rangeOfString
是NSString
的方法,而buffer
不是NSString
。我认为如果你能将数据转换成 NSString 会更好。我认为如果您尝试使用stringWithData:
UPDATE
请检查此 链接
The warning appears because
rangeOfString
is a method ofNSString
andbuffer
is not anNSString
. I think it would be better if you could convert the data into NSString. I think it will work if you try usingstringWithData:
UPDATE
Pls check this Link