必威-必威-欢迎您

必威,必威官网企业自成立以来,以策略先行,经营致胜,管理为本的商,业推广理念,一步一个脚印发展成为同类企业中经营范围最广,在行业内颇具影响力的企业。

iOS必威 base64位文件

2019-09-12 19:42 来源:未知
//// Base64.h// 儿童医院//// Created by Apple on 16/1/7.// Copyright © 2016年 展能. All rights reserved.//#import <Foundation/Foundation.h>@interface Base64 : NSObject+(NSString *)encode:data;+decode:(NSString *)data;@end

//// Base64.m// 儿童医院//// Created by Apple on 16/1/7.// Copyright © 2016年 展能. All rights reserved.//#import "Base64.h"static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";@interface Base64char2Int:c;@end@implementation Base64+(NSString *)encode:data{ if (data.length == 0) return nil; char *characters = malloc(data.length * 3 / 2); if (characters == NULL) return nil; int end = data.length - 3; int index = 0; int charCount = 0; int n = 0; while (index <= end) { int d = [data bytes])[index]) & 0x0ff) << 16) | [data bytes])[index + 1]) & 0x0ff) << 8) | [data bytes])[index + 2]) & 0x0ff); characters[charCount++] = encodingTable[(d >> 18) & 63]; characters[charCount++] = encodingTable[(d >> 12) & 63]; characters[charCount++] = encodingTable[(d >> 6) & 63]; characters[charCount++] = encodingTable[d & 63]; index += 3; if(n++ >= 14) { n = 0; characters[charCount++] = ' '; } } if(index == data.length - 2) { int d = [data bytes])[index]) & 0x0ff) << 16) | [data bytes])[index + 1]) & 255) << 8); characters[charCount++] = encodingTable[(d >> 18) & 63]; characters[charCount++] = encodingTable[(d >> 12) & 63]; characters[charCount++] = encodingTable[(d >> 6) & 63]; characters[charCount++] = '='; } else if(index == data.length - 1) { int d = [data bytes])[index]) & 0x0ff) << 16; characters[charCount++] = encodingTable[(d >> 18) & 63]; characters[charCount++] = encodingTable[(d >> 12) & 63]; characters[charCount++] = '='; characters[charCount++] = '='; } NSString * rtnStr = [[NSString alloc] initWithBytesNoCopy:characters length:charCount encoding:NSUTF8StringEncoding freeWhenDone:YES]; return rtnStr; }+decode:(NSString *)data{ if(data == nil || data.length <= 0) { return nil; } NSMutableData *rtnData = [[NSMutableData alloc]init]; int slen = data.length; int index = 0; while  { while (index < slen && [data characterAtIndex:index] <= ' ') { index++; } if (index >= slen || index + 3 >= slen) { break; } int byte = ([self char2Int:[data characterAtIndex:index]] << 18) + ([self char2Int:[data characterAtIndex:index + 1]] << 12) + ([self char2Int:[data characterAtIndex:index + 2]] << 6) + [self char2Int:[data characterAtIndex:index + 3]]; Byte temp1 = (byte >> 16) & 255; [rtnData appendBytes:&temp1 length:1]; if([data characterAtIndex:index + 2] == '=') { break; } Byte temp2 = (byte >> 8) & 255; [rtnData appendBytes:&temp2 length:1]; if([data characterAtIndex:index + 3] == '=') { break; } Byte temp3 = byte & 255; [rtnData appendBytes:&temp3 length:1]; index += 4; } return rtnData;}+char2Int:c{ if (c >= 'A' && c <= 'Z') { return c - 65; } else if (c >= 'a' && c <= 'z') { return c - 97 + 26; } else if (c >= '0' && c <= '9') { return c - 48 + 26 + 26; } else { switch { case '+': return 62; case '/': return 63; case '=': return 0; default: return -1; } }}@end
TAG标签:
版权声明:本文由必威发布于必威-编程,转载请注明出处:iOS必威 base64位文件