【问题标题】:iPhone: CFReadStreamRead storing bytes as signed instead of unsignediPhone:CFReadStreamRead 将字节存储为有符号而不是无符号
【发布时间】:2011-07-26 05:51:41
【问题描述】:

我正在从我的 PC 应用程序向我的 iPhone 发送套接字数据。当数据到达时,它被存储为有符号值而不是无符号 (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);

【问题讨论】:

    标签: iphone ios4 ios-simulator cfstream


    【解决方案1】:

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

    【讨论】:

    • 据我所知,我并没有搞砸它。如果我在读取后放置一个断点并检查缓冲区,任何超过 0x3f 的值都会设置为“?”。
    • 另外,我对将其解码为字符串不感兴趣。我只想要不变的十六进制值。
    • 看起来我在 Windows 端使用的测试工具在发送之前截断了字节值。尽管 GUI 没有表明这一点。注意 SimpleCom 工具上的这个问题。
    猜你喜欢
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 2017-10-01
    • 2015-01-12
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多