开发者

How do I convert a file to this hexadecimal notation in objective c?

开发者 https://www.devze.com 2023-04-04 12:13 出处:网络
I\'m trying to urlencode a png for upload using the Tumblr API via iOS / Objective C.This is relatively unbelievable, but the Tumblr API will only accept \'urlencoded binary data\' for multiple photo

I'm trying to urlencode a png for upload using the Tumblr API via iOS / Objective C. This is relatively unbelievable, but the Tumblr API will only accept 'urlencoded binary data' for multiple photo uploads in one photoset, and won't accept the usual simple form data posts. I have a working Python example provided by Tumblr. It takes an image / binary file and reads it to this (I guess hexadecimal) format:

'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00\x1d\x08\x06\x00\x00\x00\xcb\x9en\x87\x00\x00\x01\xc9IDATH\r\xc5\x971K\x03A\x10\x85s\nB \x10\xb0\n\xa4\xb2J\x95* \x08\x82\x95\xbfA\x10\xfc\x01V\xb6\x82\x95?\xc0J\x10\x04[[\xc16U@\xb0\x8d`/(\x8a\x82\x08Z\t\x12\x89\xdf$\xd9p\x9c\xae\xfb&\x9c8\xf0\xd8\xbd\xdb\xb7o^\xe6\x96\xb9K6\x1c\x0e+\xb3D\x96e\xf3\xeck\xb3\xffj\x96\xfda\xcf\\\x98\xa8#\x89\xdb\xe0\x1c\xfe\x00\xf4\x99\x0f@\x17\xec\x80\xba\xaa3\xe5Y\x05\x14\xb0\xa1\x06\x0e\xc0=\xb0\xb2\xfd\x84\x17\xee\xaf(z\x81#%72\xb1\x1bIZ4r\x03\xaf\x16\x12\xa4F\xc9\x00\x82\xf6\xbcM\xb8\x98,v\xbd\x9fJ\x1c\xd6\xd53\xd0 \xf9\x12Pc]%\xaa\x06\x9e\x11\xfcTE\xe1\xd9y\x91B2@\xb9>P{\x90\x14\xc7$\xe3K!\x19\x98(Y\x15\xd4xR\x89\x1e\x03\xef\xaa(\xbc7\x95\xeb1\xd0RE\xe1-\xab\\\x8f\x01\xcf\x19\xb8\xfb\x0b\x03\x9e\x9e\x7f\xad\x1a\x90\x1a\x915\rb\x15\xdc\x82X\xf3\t\xf7\x1f\xe1tB\xa3I\x8d\xb2\x81\x89\x89=\xc1\xc0v*i~\xdds\x06\xc8]QJ\xabpLk\x1cy7\xa99;\x16@\x1f\x84r\x17\xc7K\xe3\xa4t\xf2\xeb\xde\n\xd8;\xe1\xb7\x13n\xcd\xca^\\z\xe4\xdd\xa4\xe6\xa8^\x80\xe2\xaf.^\x9f\xa4t\xf2\xeb\xd2!$i\x13\x1c\n\xc9\x83\x993\xb8\xad|\xa2\xd8<j\x00\x01{\xde\x9b\xa0\x0b\x82\xb0w\xec\xb1w\x0bTe\x03\x90;\xc0~\xad}^y\x13\xc6\xf8\xafh\x1d\x81o\xfdaZ\x01\x167\x80\xf2\x8ccI\xd4\xfb=\xf2\xac\x85\x8a\x8c\x0cp\xe3\x14\xa8\x02e\xf1\x8e\xcd\x84\x85}>\x95%\xea\xd5iX\x1f\xf0|\xeb\x99\xe12\xa3i\x06\xfc\x7f&\xca\xb30\xaa\xc0byzn\xa5\xbaU\xe0?\rT\xbf\x00\x87\x89 \xa8s3+7\x00\x00\x00\x00IEND\xaeB`\x82'

Then it does some url encoding to create this:

89PNG%250D%250A%251A%250A%2500%2500%2500%250DIHDR%2500%2500%2500%2520%2500%2500%2500%251D%2508%2506%2500%2500%2500%25CB%259En%2587%2500%2500%2501%25C9IDATH%250D%25C5%25971K%2503A%2510%2585s%250AB%2520%2510%25B0%250A%25A4%25B2J%2595%252A%2520%2508%2582%2595%25BFA%2510%25FC%2501V%25B6%2582%2595%253F%25C0J%2510%2504%255B%255B%25C16U%2540%25B0%258D%2560%252F%2528%258A%2582%2508Z%2509%2512%2589%25DF%2524%25D9p%259C%25AE%25FB%2526%259C8%25F0%25D8%25BD%25DB%25B7o%255E%25E6%2596%25B9K6%251C%250E%252B%25B3D%2596e%25F3%25ECk%25B3%25FFj%2596%25FDa%25CF%255C%2598%25A8%2523%2589%25DB%25E0%251C%25FE%2500%25F4%2599%250F%2540%2517%25EC%2580%25BA%25AA3%25E5Y%2505%2514%25B0%25A1%2506%250E%25C0%253D%25B0%25B2%25FD%2584%2517%25EE%25AF%2528z%2581%2523%252572%25B1%251BIZ4r%2503%25AF%2516%2512%25A4F%25C9%2500%2582%25F6%25BCM%25B8%2598%252Cv%25BD%259FJ%251C%25D6%25D53%25D0%2520%25F9%2512Pc%255D%2525%25AA%2506%259E%2511%25FCTE%25E1%25D开发者_如何学Python9y%2591B2%2540%25B9%253EP%257B%2590%2514%25C7%2524%25E3K%2521%2519%2598%2528Y%2515%25D4xR%2589%251E%2503%25EF%25AA%2528%25BC7%2595%25EB1%25D0RE%25E1-%25AB%255C%258F%2501%25CF%2519%25B8%25FB%250B%2503%259E%259E%257F%25AD%251A%2590%251A%25915%250Db%2515%25DC%2582X%25F3%2509%25F7%251F%25E1tB%25A3I%258D%25B2%2581%2589%2589%253D%25C1%25C0v%252Ai~%25DDs%2506%25C8%255DQJ%25ABpLk%251Cy7%25A99%253B%2516%2540%251F%2584r%2517%25C7K%25E3%25A4t%25F2%25EB%25DE%250A%25D8%253B%25E1%25B7%2513n%25CD%25CA%255E%255Cz%25E4%25DD%25A4%25E6%25A8%255E%2580%25E2%25AF.%255E%259F%25A4t%25F2%25EB%25D2%2521%2524i%2513%251C%250A%25C9%2583%25993%25B8%25AD%257C%25A2%25D8%253Cj%2500%2501%257B%25DE%259B%25A0%250B%2582%25B0w%25EC%25B1w%250BTe%2503%2590%253B%25C0~%25AD%257D%255Ey%2513%25C6%25F8%25AFh%251D%2581o%25FDaZ%2501%25167%2580%25F2%258CcI%25D4%25FB%253D%25F2%25AC%2585%258A%258C%250Cp%25E3%2514%25A8%2502e%25F1%258E%25CD%2584%2585%257D%253E%2595%2525%25EA%25D5iX%251F%25F0%257C%25EB%2599%25E12%25A3i%2506%25FC%257F%2526%25CA%25B30%25AA%25C0byzn%25A5%25BAU%25E0%253F%250DT%25BF%2500%2587%2589%2520%25A8s3%252B7%2500%2500%2500%2500IEND%25AEB%2560%2582

Then it POSTs the above data along with some other pieces. How can I do this in Objective C -- that is, get the 89PNG... string? I tried reading in the image using:

NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"png"]];

Then I converted to a hex string using:

NSString *dataString = [data description];
dataString = [[dataString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];

Which then left me with something that looks close:

89504e470d0a1a0a0000000d49484452000000200000001d0806000000cb9e6e87000001c949444154480dc597314b03411085730a422010b00aa4b24a952a20088295bf4110fc0156b682953fc04a10045b5bc1365540b08d602f288a82085a091289df24d9709caefb269c38f0d8bddbb76f5ee696b94b361c0e2bb3449665f3ec6bb3ff6a96fd61cf5c98a82389dbe01cfe00f4990f4017ec80baaa33e5590514b0a1060ec03db0b2fd8417eeaf287a8123253732b11b495a347203af1612a446c90082f6bc4db8982c76bd9f4a1cd6d533d020f91250635d25aa069e11fc5445e1d97991423240b93e507b9014c724e34b211998285915d47852891e03efaa28bc3795eb31d05245e12dab5c8f01cf19b8fb0b039e9e7fad1a901a91350d6215dc8258f309f71fe17442a3498db28189893dc1c0762a697edd7306c85d514aab704c6b1c7937a9393b16401f847217c74be3a474f2ebde0ad83be1b7136ecdca5e5c7ae4dda4e6a85e80e2af2e5e9fa474f2ebd2212469131c0ac9839933b8ad7ca2d83c6a00017bde9ba00b82b077ecb1770b546503903bc07ead7d5e7913c6f8af681d816ffd615a01163780f28c6349d4fb3df2ac858a8c0c70e314a80265f18ecd84857d3e9525ead569581ff07ceb99e132a36906fc7f26cab330aac062797a6ea5ba55e03f0d54bf00878920a873332b370000000049454e44ae426082

But unfortunately not close enough. What do I do next to convert this string to the 89PNG... string?


I figured out how to url encode the hex data according to Tumblr's multiple photo upload needs based on its posted Python code, and I created a category that will convert NSData from an image file to the url encoded hex string. There's probably a more efficient / faster way to do this... anyone?

Edit: I've created a GitHub project that takes care of this transformation.


You can do the % encoding by using NSString stringByAddingPercentEscapesUsingEncoding. I don't know how you get the original \-escaped string, though.

(The whole thing seems quite peculiar.)

0

精彩评论

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

关注公众号