在Xcode中使用扩展十六进制代码

在Xcode中使用扩展十六进制代码,xcode,post,hex,Xcode,Post,Hex,我对此非常陌生,我尝试使用扩展十六进制代码(超过127的ascii字符),它是通过将十进制数转换为浮点数生成的,然后通过post命令发送到设备以更改设定点 我通过简单的操作来创建十六进制代码 - (void)dec2float:(id)sender { NSString *dec = setPoint.text; float flt = [dec floatValue]; unsigned char *bytes = (unsigned char *)&flt;

我对此非常陌生,我尝试使用扩展十六进制代码(超过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];
}
-(void)setACStatus:(id)发送方
{    
NSString*post=[NSString stringWithFormat:@“\x38\x00\x00\x00\x00\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\x00%@\x00%@\x00%@\x00\x00\x00%@\x00,
unitNumberString、onOffString、modeString、modeString2、sPoint01、sPoint02、sPoint03、sPoint04、,
[3];
postData=[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString*postLength=[NSString stringWithFormat:@“%d”,[postData计数];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];

[请求设置URL:[NSURL URLWithString:@”http://10.38.140.9/cmd/”]];//似乎您正试图从字符串创建一个
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)];

您是如何将字符串发送到设备的?这似乎是个问题。此外,在调用
stringWithFormat:
时,格式字符串后缺少一个逗号。我已使用我正在使用的post命令编辑了上述帖子。希望它会有所帮助。它已正常工作,很抱歉延迟重播,但非常感谢您的帮助。