编码回车
我正在尝试将 ascii 编码的消息发送到服务器。当我尝试将回车符附加到
-(void)button2Pressed
{
NSMutableString *mutableString = [NSMutableString stringWithString:@"h323name get"];
[self sendStringCommand:mutableString];
}
-(void)sendStringCommand:(NSMutableString*)string
{
[string appendString:@"\\r"];
NSLog(@"string %@ wtf",[string dataUsingEncoding:NSASCIIStringEncoding]);
NSData * testData = [[NSData alloc]initWithBytes:[string dataUsingEncoding:NSASCIIStringEncoding] length:sizeof([string dataUsingEncoding:NSASCIIStringEncoding])];
[socket writeData:testData withTimeout:20 tag:1];
}
当前输出的字符串时,我的问题出现了:
字符串<68333233 6e616d65 20676574 5c72>哇哦
应该是
字符串 <68333233 6e616d65 20676574 0d>哇哦
只是普通 /r 换了一行,因此 nslog 中数据后面的 wtf 字符
Im trying to send ascii encoded message to a server. My problem is coming in when I try to append the carriage return to the string
-(void)button2Pressed
{
NSMutableString *mutableString = [NSMutableString stringWithString:@"h323name get"];
[self sendStringCommand:mutableString];
}
-(void)sendStringCommand:(NSMutableString*)string
{
[string appendString:@"\\r"];
NSLog(@"string %@ wtf",[string dataUsingEncoding:NSASCIIStringEncoding]);
NSData * testData = [[NSData alloc]initWithBytes:[string dataUsingEncoding:NSASCIIStringEncoding] length:sizeof([string dataUsingEncoding:NSASCIIStringEncoding])];
[socket writeData:testData withTimeout:20 tag:1];
}
currently this outputs this:
string <68333233 6e616d65 20676574 5c72> wtf
which should be
string <68333233 6e616d65 20676574 0d> wtf
Just plain /r did a new line hence the wtf characters after the data in the nslog
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的反斜杠太多了。试试这个:
此外,您创建的
testData
是完全错误的。创建testData
的方式是将指针传递给NSData
对象作为“bytes”参数,并将指针的大小传递给NSData
code> 作为“长度”参数。你应该这样做:You have too many backslashes. Try this:
Also, your creation of
testData
is completely wrong. The way you are creatingtestData
is passing a pointer to anNSData
object as the "bytes" parameter, and passing the size of a pointer to anNSData
as the "length" parameter. You should just do this: