Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6568621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:30:06+00:00 2026-05-25T14:30:06+00:00

Say that I have the Python code: someString = file(filename.jpg).read() How can I replicate

  • 0

Say that I have the Python code:

someString = file("filename.jpg").read()

How can I replicate this line of code in Objective C on iOS? I’m trying to convert a jpg into a string, for eventual url encoding so I can pass it on to the Tumblr API. I tried base64 encoding to convert the image to a string initially, but that doesn’t seem to get a valid response from tumblr. I also tried NSUTF8Encoding, but those attempts return nil strings (see below).

Why am I doing this? Someone from Tumblr posted a Python example of how to submit multiple photos for a photoset in response to this thread, and the process appears to start with converting the images to strings using file.read(). The API unfortunately doesn’t take a simple ‘multipart/form-data’ request, only ‘application/x-www-form-urlencoded’ format.

What sort of encoding is Python using to do this read(), and how can I replicate it in Objective C on the iPhone?

I have tried the following:

NSString *utf8String = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"jpg"] encoding:NSUTF8StringEncoding error:&error];

This gives a nil string and error 261 The operation couldn’t be completed.

Using base64 encoding gives me a string, but the Tumblr API doesn’t accept it after url encoding it, so I assume it’s not doing what Python is doing with read() (though I could just be missing a step). The Tubmlr API responds with "status":400,"msg":"Bad Request"},"response":{"errors":["Error uploading photo."]

There is a similar question on stackoverflow about uploading photos to photosets on Tumblr here, but this question is more specific to the posted Python code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:30:06+00:00Added an answer on May 25, 2026 at 2:30 pm

    I figured the answer out by simply logging the string — it looks like it’s a hexadecimal format, with \xXX where XX is the hexadecimal code for each byte, with ASCII substitutions for certain easily-printed characters.

    \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
    

    The above can be replicated in Objective C by reading in the image to NSData, and then getting the description (which is a list of 2 digit hexadecimal-formatted byte values). After that, you could loop through each byte and decide how to represent it based on its value:

    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"anImage" ofType:@"jpg"]];
    NSString *hexFormatString = [data describe];
    hexFormatString = [[hexFormatString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSMutableString * newString = [NSMutableString string];    
    for (int x=0; x<[hexFormatString length]; x+=2) {
        NSString *component = [hexFormatString substringWithRange:NSMakeRange(x, 2)];
        int value = 0;
        sscanf([component cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
        if ((value <=46 && value >= 45) || (value <=57 && value >= 48) || (value <=90 && value >= 65) || (value == 95) || (value <=122 && value >= 97)) {  //48-57, 65-90, 97-122
            [newString appendFormat:@"%c", (char)value];
        }
        else {
            [newString appendFormat:@"%%%@", [component uppercaseString]];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have only DHT (distributed hash table) implemented (in Python), and
Say I have a Python function that returns multiple values in a tuple: def
Say that I have two models- Users and Accounts. Each account can have at
Say that i have a generic dictionary with data like this (I hope the
Say that I have the following code: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(myRunnable); Now, if
let's say that I have this function in Rails (I have actually :P -
I have a piece of Python code, that interacts with a PostgreSQL database via
Say, I have the following Python code if obj is None: identifier = None
Say that I have two tables like those: Employers (id, name, .... , deptId).
Say that I have LINQ query such as: var authors = from x in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.