开发者

How to declare Raw Binary/Hex data in ObjC and how to pass it in an argument?

This is what I have in Python:

ourAddr = Binary('\x4c\x6f\x8c')

I need to declare it in ObjC then pass it to a python script running on another server.

This is how I've been trying:

char *addr = "\x4c\x6f\x8c";

NSArray *hex = [NSArray arrayWithObjects: [NSNumber numberWithChar: addr], nil];
[waitOnEvent setMethod:@"waitOnEvent" withObjects: hex];

But I get the response: "Non-licensed address provided" which leads me to think that it doesnt see the 'addr' variable correctly. Any advice would be greatly appreciated.

Thanks in advance!

EDIT:

I have sent it as as string by doing the following:

NSString *const hex = @"4c6f8c"

NSArray *pass = [NSArray arrayWithObjects:hex, nil];

NSString *server = @"http://192.168.0.x:8080"; 
    XMLRPCRequest *waitOnEvent = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
    [waitOnEvent setMethod:@"waitOnEvent" withObjects:pass];

And this is what I get from the server when I try to pass that string:

16:23:08:448 INFO     http_server waitOnEvent(4c6f8c, )
16:23:08:470 INFO     http_server 192.168.0.x:52982 - -  "POST / HTTP/1.1" 200 458

16:23:08:477 INFO     http_server <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<m开发者_如何学JAVAember>
<name>faultString</name>
<value><string>&lt;type 'exceptions.RuntimeError'&gt;:Non-licensed address provided</string></value>
</member>
</struct></value>
</fault>
</methodResponse>


Are you trying to send your data as ASCII/UTF-8 encoded hex strings or raw binary data?

If it's the latter, you can do the following (I have used your code accordingly):

const char addr[] = {0x4c, 0x6f, 0x8c};
NSData *data = [NSData dataWithBytes:addr length:sizeof(addr)];
NSArray *array = [NSArray arrayWithObject:data];
[waitOnEvent setMethod:@"waitOnEvent" withObjects:array];


Your diagnosis is accurate; per the documentation, +numberWithChar:

Creates and returns an NSNumber object containing a given value, treating it as a signed char.

So the NSNumber you're creating has a value that comes from the address that your C-style string happens to be allocated and is completely unrelated to the value of your string.

I'm not entirely sure what you want, but I think probably:

[NSNumber numberWithInt:0x4c6f8c]

To give an NSNumber object with from the C hexadecimal primitive 0x4c6f8c, which is 5,009,292 in decimal.

If you wanted to put byte values into an array and read just the total value, you'd have to consider endianness issues because C is relatively low level and Objective-C inherits directly from it. You can use the C function strtol if you have a C string of digits in any base from 2 to 36 and want to get an integer out of them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜