iPhone:CFReadStreamRead将字节存储为已签名而非无符号

时间:2011-07-26 05:51:42

标签: iphone ios4 ios-simulator cfstream

从我的PC应用程序中,我将套接字数据发送到我的iPhone。当数据到达时,它将被存储为有符号值而不是unsigned(uint8)。所以我不能在127以上得到任何值。

当我发送0xFF时,它默认返回到0x3f。

CFReadStreamRead仅适用于ASCII字符吗?如果是这样的话会有什么替代品?

我的代码:

CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFIndex bytes;
UInt8 buffer[128];
UInt8 recv_len = 0;
UInt8 send_len = 0;

//
//Get the native socket that was used
CFSocketNativeHandle sock = *(CFSocketNativeHandle*)data;


//
//Create the read and write streams for this socket
CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, &readStream, &writeStream);

//
//Check for errors
if (!readStream || !writeStream)
{
    close(sock);
    fprintf(stderr, "CFStreamCreatePairWithSocket failed.");
    return;
}

//
//Open the streams
CFReadStreamOpen(readStream);
CFWriteStreamOpen(writeStream);

//
//Read the command bytes
short command = 0;

memset(buffer, 0, sizeof(buffer));
bytes = CFReadStreamRead(readStream, buffer, 12);

1 个答案:

答案 0 :(得分:1)

0x3f是ASCII '?'。您是否尝试将字节缓冲区解码为UTF-8字符串?如果是这样,那就是你的问题。 CFReadStream API不太可能弄乱数据;你的可能性更大。