4

I have a issue in omitting \r\n after encoding the string, below is the encoded string which I have got,

QzAxODBCMDQtNDdDMi00QzhDLTg1MTAtNUE1MzU1RDIzRDA4Ojk4Mzg5MzRkYzky\r\nNTRkYWE4ODljY2Q0ZGYxNjljYTU
Cœur
  • 37,241
  • 25
  • 195
  • 267
Santosh Gurram
  • 1,007
  • 3
  • 13
  • 22
  • Your code produced this base64-encoded string? How? Show the code. Or are you receiving this from somewhere and you need to decode it? Ideally, you'd fix the source to not do that, but in a pinch you can simply replace the literal `\r\n` with an actual newline. Use `-[NSString stringByReplacingOccurrencesOfString:withString:]` or the like. – Ken Thomases Jun 16 '12 at 15:28
  • @KenThomases, Thanks for your reply, actually I getting the string as `QzAxODBCMDQtNDdDMi00QzhDLTg1MTAtNUE1MzU1RDIzRDA4Ojk4Mzg5MzRkYzky NTRkYWE4ODljY2Q0ZGYxNjljYTU` like this after `ky` the `NTR` starts in next line. I using the NSData+base64.h & .m files to encode the string. – Santosh Gurram Jun 16 '12 at 15:31
  • 1
    Why is it a problem that there's a newline? That's typical of base64 because many transmission media (like email) have a line-length limit. Anyway, if you really want to eliminate it, you can still use the method I recommended above. Replace `@"\n"` and/or `@"\r"` with `@""`. – Ken Thomases Jun 16 '12 at 15:47
  • @KenThomases, Thanks for ur reply, I have tried the above mentioned method but still no luck. As you said it is right that its shouldn't be th problem but when I am getting the response I see the encoded string `QzAxODBCMDQtNDdDMi00QzhDLTg1MTAtNUE1MzU1RDIzRDA4Ojk4Mzg5MzRkYzky\r\nNTRkYWE4ODljY2Q0ZGYxNjljYTU` like this. Also I have added it in my header (HTTPHeader while requesting the server), I don't see this header parameter but rest of the header fields when I print in the log. Any guess about this. Thanks. – Santosh Gurram Jun 16 '12 at 15:54
  • You just said you're not getting a string with a literal "\r\n" it it and now you say you are getting that. Which is it? From where are you getting the string? Show code. Show verbatim log output. – Ken Thomases Jun 16 '12 at 16:35
  • @SantoshGurram You need to edit your question and post where you got your `NSData+base64.h` and `.m` files. Be specific. We need to look at those files to answer your question. – rob mayoff Jun 16 '12 at 18:02

1 Answers1

3

You can easily remove the \r\n from a string like this:

NSString *outString = [inString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];

If you want to create the strings without \r\n in the first place, then it depends on how you create them. For example, if you're using the Base64 category from Cocoa with Love, you need to add this line before the call to BIO_write in base64EncodedString:

BIO_set_flags(context, BIO_FLAGS_BASE64_NO_NL);

He explains this in the article.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • I am getting the encoded string like mentioned below `QzAxODBCMDQtNDdDMi00QzhDLTg1MTAtNUE1MzU1RDIzRDA4Ojk4Mzg5MzRkYzky NTRkYWE4ODljY2Q0ZGYxNjljYTUw` but I need it in one line. Please help if possible explain with a scenario. – Santosh Gurram Jun 26 '12 at 12:51