2012-03-12 54 views
0

使用擴展的十六進制代碼,我都非常新的這個,我試圖使用擴展的十六進制代碼(過去的127 ASCII字符),它們通過將一個十進制數浮動,其隨後被通過發送號碼生成將命令發佈到設備以更改設定點。在Xcode

我簡單地做這創造了十六進制代碼,

- (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]; 

BU噸而不是「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]; 
} 

三江源。

+0

你是如何發送字符串的設備?這似乎是問題。另外,在調用'stringWithFormat:'的格式字符串後,會有一個逗號丟失。 – user1118321 2012-03-13 00:07:00

+0

我已經使用POST命令編輯了上述帖子。希望它會有所幫助。 – retstil 2012-03-23 16:01:58

回答

0

好像你想創建一個字符串的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)]; 
+0

得到它的工作,抱歉的重播後期,但謝謝你的幫助。 – retstil 2012-04-06 12:43:23