【问题标题】:Using extended Hex Codes in Xcode在 Xcode 中使用扩展的十六进制代码
【发布时间】:2012-03-12 22:57:01
【问题描述】:

我对此非常陌生,我正在尝试使用扩展的十六进制代码(过去 127 的 ascii 字符),这些代码是通过将十进制数转换为浮点数生成的,然后通过 post 命令发送到设备以更改设定点。

我只是通过这样做来创建十六进制代码,

- (void)dec2float:(id)sender
{
    NSString *dec = setPoint.text;
    float flt = [dec floatValue];
    unsigned char *bytes = (unsigned char *)&flt;
    sPoint01 = [NSString stringWithFormat:@"%x", bytes[0]];
    sPoint02 = [NSString stringWithFormat:@"%x", bytes[1]];
    sPoint03 = [NSString stringWithFormat:@"%x", bytes[2]];
    sPoint04 = [NSString stringWithFormat:@"%x", bytes[3]];
}

如果 setPoint.text 的值例如是 23,这将使

sPoint01 = 00
sPoint02 = 00
sPoint03 = b8
sPoint01 = 41

然后必须将这些值作为十六进制字符串发送到设备,然后将其连接在一起,

NSString *post = [NSString stringWithFormat:@"\\x%@\\x%@\\x%@\\x%@",sPoint01, sPoint02, sPoint03, sPoint04];

但我收到的不是“0000b841”,而是“00005c78 62385c78 3431”。我假设这是因为 Xcode 不允许任何超过 127 的十六进制代码,就好像我尝试输入它一样

@"\xb8"

Xcode 告诉我“输入转换已停止,因为输入字节不属于输入代码集 UTF-8”。

我是在以正确的方式解决这个问题,还是在错误的道路上走了很长一段路?

我们将不胜感激任何帮助,欢迎提出任何建议。

提前谢谢你

编辑:

嗨, 感谢您的回复,这是我用来将数据发送到控制器的命令,希望对您有所帮助,

- (void)setACStatus:(id)sender
{    
NSString *post = [NSString stringWithFormat:@"\x38\x00\x00\x00\x76\x11\x01\x00%@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1b\x00\x00\x00%@\x00%@%@\x00\x10\x00\x10%@%@%@%@%@\x00\x00\x00",
                      unitNumberString, onOffString, modeString, modeString2, sPoint01, sPoint02, sPoint03, sPoint04, 
                      fanSpeedString];

     postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];    

    NSString *postLength = [NSString stringWithFormat:@”%d”, [postData count];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:@”http://10.38.140.9/cmd/”]];//<-- Controller address.
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if( theConnection )
    {
        webData = [NSMutableData data];
    }
    else
    {
        NSLog (@"The Connection is NULL");
    }

    temp.text = [NSString stringWithFormat:@"%@",postData];
}

谢谢。

【问题讨论】:

  • 如何将字符串发送到设备?这似乎是问题所在。此外,在您对stringWithFormat: 的调用中,格式字符串后缺少一个逗号。
  • 我已经用我正在使用的 POST 命令编辑了上面的帖子。希望它会有所帮助。

标签: xcode post hex


【解决方案1】:

您似乎正在尝试从字符串创建NSData 对象,但您已经知道要包含在数据对象中的字节。无需执行创建字符串并尝试将其转换为数据对象的额外步骤,只需从您的字节创建数据对象,如下所示:

typedef struct postDataType = {
    unsigned char someData0[8];
    int unitNumberString;
    unsigned char someData1[33]; // or however many bytes it is
    unsigned char onOffString;
    // etc... rest of fields go here
} postDataType;
postDataType dataBytes = {
    {0x38, 0x00, 0x00 0x00, 0x76, 0x11, 0x01, 0x00},
    [unitNumberString intValue],
    // etc... fill in the rest of the fields here with the data you want
};
postData = [NSData dataWithBytes:dataBytes length:sizeof(dataBytes)];

【讨论】:

  • 搞定了,抱歉重播晚了,但非常感谢您的帮助。
猜你喜欢
  • 2019-01-01
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 2014-03-18
相关资源
最近更新 更多