can't able to integrate base64decode in my class

Posted by MaheshBabu on Stack Overflow See other posts from Stack Overflow or by MaheshBabu
Published on 2010-12-24T14:22:00Z Indexed on 2010/12/25 7:54 UTC
Read the original article Hit count: 173

Filed under:

Hi folks,

i am getting the image that is in base64 encoded format.

I need to decode it.

i am writing the code for decoding is

 + (NSData *) base64DataFromString: (NSString *)string {
  unsigned long ixtext, lentext;
  unsigned char ch, input[4], output[3];
  short i, ixinput;
  Boolean flignore, flendtext = false;
  const char *temporary;
  NSMutableData *result;

  if (!string)
    return [NSData data];

  ixtext = 0;
  temporary = [string UTF8String];
  lentext = [string length];
  result = [NSMutableData dataWithCapacity: lentext];
  ixinput = 0;
  while (true) {
    if (ixtext >= lentext)
      break;
    ch = temporary[ixtext++];
    flignore = false;
    if ((ch >= 'A') && (ch <= 'Z'))
      ch = ch - 'A';
    else if ((ch >= 'a') && (ch <= 'z'))
      ch = ch - 'a' + 26;
    else if ((ch >= '0') && (ch <= '9'))
      ch = ch - '0' + 52;
    else if (ch == '+')
      ch = 62;
    else if (ch == '=')
      flendtext = true;
    else if (ch == '/')
      ch = 63;
    else
      flignore = true; 

    if (!flignore) {
      short ctcharsinput = 3;
      Boolean flbreak = false;

      if (flendtext) {
         if (ixinput == 0)
           break;                
         if ((ixinput == 1) || (ixinput == 2)) {
           ctcharsinput = 1;
         else
           ctcharsinput = 2;

         ixinput = 3;
         flbreak = true;
      }

      input[ixinput++] = ch;

      if (ixinput == 4)
        ixinput = 0;

      output[0] = (input[0] << 2) | ((input[1] & 0x30) >> 4);
      output[1] = ((input[1] & 0x0F) << 4) | ((input[2] & 0x3C) >> 2);
      output[2] = ((input[2] & 0x03) << 6) | (input[3] & 0x3F);

      for (i = 0; i < ctcharsinput; i++)
        [result appendBytes: &output[i] length: 1];
    }

    if (flbreak)
      break;
  }
  return result;
}

i am calling this in my method like this

NSData *data = [base64DataFromString:theXML];

theXML is encoded data.

but it shows error decodeBase64 undeclared.

How can i use this method.

can any one pls help me.

Thank u in advance.

© Stack Overflow or respective owner

Related posts about iphone