1

I searched high and low for the past week, none of the similar questions on SO has solved my problem.

I'm trying to upload a UIImage to a .NET wcf (wsdl?). It needs to be in the form of a byte array.

I tried

NSData *data = [[NSData alloc]initWithData:UIImageJPEGRepresentation(Image, 1)];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);

and byteData returns

ˇÿˇ‡

That doesn't look like a byte array I'm sure. What does a byte array suppose to look like? I also tried called NSData description, and various others with no success.

I also tried

[self.imageFile base64EncodedStringWithOptions:NSDataBase64EncodingEndLineFeed];

and get back

/9j/4AAQSkZJRgABAQEAJAAkAAD/4QCMRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAAAkA

Is that what a byte array suppose to look like? I would guess a byte array is suppose to look like the following.

[244, 22, 244, 4, 32, etc.....]

The byte array needs to be embedded in an XML. Any help is appreciated. Thanks!

khanh.tran.vinh
  • 655
  • 5
  • 10

2 Answers2

1

The code you posted will convert a UIImage to a binary JPEG image in an NSData object, then extract the byte values from the NSData. That IS a byte array. (A block of memory containing bytes of binary data.)

It sounds like what you really want to do is format your binary data in XML format. That is a different question.

I did a Google search on "How do you represent binary data in XML" and it seems that Base64 encoding is the consensus. I suggest you look into Base64 encoding.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • What result is that? Be specific. – Duncan C Dec 05 '13 at 03:36
  • Base64 encode gives me a string "w7/DmMO/w6AAEEpGSUYAAQEBAAAAAAAAw7/DoQD....". When I attach this string into the tags into the XML and send it off, the WCF service returns me nothing (no headers, no data). Based on this -->http://stackoverflow.com/a/2538507/1989101 , WCF wants raw binary data, and not base64. – khanh.tran.vinh Dec 05 '13 at 03:58
  • (cont') The group that maintains this WCF service had originally instructed me to send binary data. So trying that, using initWithBytes on NSStrring and NSASCIIStringEncoding,I get these characters "ÿØÿà" as the image file. I send this in the XML, but the XML terminates after those 4 characters (abruptly ends, without closing tag). Checking the length of this string, is actually len=7000+, not 4. However, I actually get a server response from sending raw binary as string in tags. I get the headers, + "The byte 0x00 is not valid at this location. Line 1, position 413". – khanh.tran.vinh Dec 05 '13 at 03:59
  • NSString *image = [[NSString alloc]initWithBytes:[self.imageFile bytes] length:[self.imageFile length] encoding:NSASCIIStringEncoding]; – khanh.tran.vinh Dec 05 '13 at 04:05
0

I think better approach will be if you will use Base64 Encoding .

ManiaChamp
  • 849
  • 7
  • 22