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lci5SdW4oUnVuQmVoYXZpb3IgcnVuQmVoYXZpb3IsIFNxbENvbW1hbmQgY21kSGFuZGxlciwgU3FsRGF0YVJlYWRlciBkYXRhU3RyZWFtLCBCdWxrQ29weVNpbXBsZVJlc3VsdFNldCBidWxrQ29weUhhbmRsZXIsIFRkc1BhcnNlclN0YXRlT2JqZWN0IHN0YXRlT2JqKQ0KICAgYXQgU3lzdGVtLkRhdGEuU3FsQ2xpZW50LlNxbERhdGFSZWFkZXIuSGFzTW9yZVJvd3MoKQ0KICAgYXQgU3lzdGVtLkRhdGEuU3FsQ2xpZW50LlNxbERhdGFSZWFkZXIuUmVhZEludGVybmFsKEJvb2xlYW4gc2V0VGltZW91dCkNCiAgIGF0IFN5c3RlbS5EYXRhLlNxbENsaWVudC5TcWxEYXRhUmVhZGVyLlJlYWQoKQ0KICAgYXQgYXhhY29yZS5saWIuZGIuRGJTZXNzaW9uMy5SZWFkKCkNCg0KPC9wcmU+DQoNCjwvc3Bhbj4NCg0K
当我解码时:
<3c686561 643e0d0a 3c6c696e 6b207265 6c3d2773 74796c65 73686565 74272068 7265663d 272f5f63 6f6d6d6f 6e2f7374 796c6573 2e617370 78273e3c 2f6c696e 6b3e0d0a 3c2f6865 61643e0d 0a3c7363 72697074 206c616e 67756167 653d274a 61766153 63726970 74273e0d 0a0d0a66 756e6374 696f6e20 666e546f 67676c65 44657461 696c7328 29207b0d 0a202020 20747279 207b0d0a 20202020 20202020 69662028 78446574 61696c73 2e737479 6c652e64 6973706c 61792e74 6f557070 65724361 73652829 203d3d20 274e4f4e 45272920 7b0d0a20 20202020 20202020 20202078 44657461 696c732e 7374796c 652e6469 73706c61 79203d20 27273b0d 0a202020 20202020 20202020 20784465 7461696c 73427574 746f6e2e 76616c75 65202020 3d202748 69646520 44657461 696c7327 3b0d0a20 20202020 2020207d 20656c73 65207b0d 0a202020 20202020 20202020 20784465 7461696c 732e7374 796c652e 64697370 6c617920 3d20276e 6f6e6527 3b0d0a20 20202020 20202020 20202078 44657461 696c7342 7574746f 6e2e7661 6c756520 20203d20 27566965 77204465 7461696c 73273b0d 0a202020 20202020 207d0d0a 20202020 7d206361 74636820 28652920 7b0d0a20 2020207d 0d0a7d0d 0a0d0a3c 2f736372 6970743e 0d0a0d0a 3c707265 20636c61 73733d27 43546578 74273e0d 0a0d0a41 6e20756e 65786563 74656420 6572726f 72206861 73206f63 63757272 65642e0d 0a546865 20657272 6f722068 61732062 65656e20 6c6f6767 65642e0d 0a496620 74686520 70726f62 6c656d20 70657273 69737473 20706c65 61736520 636f6e74 61637420 73757070 6f72740d 0a6f7220 796f7572 20737973 74656d20 61646d69 6e697374 7261746f 722e0d0a 0d0a3c70 72653e0d 0a0d0a3c 696e7075 74206964 3d277844 65746169 6c734275 74746f6e 2720636c 6173733d 27434275 74746f6e 27207479 70653d27 62757474 6f6e2720 6f6e636c 69636b3d 27666e54 6f67676c 65446574 61696c73 28292720 76616c75 653d2756 69657720 44657461 696c7327 2f3e0d0a 0d0a3c62 722f3e0d 0a3c6272 2f3e0d0a 0d0a3c73 70616e20 69643d27 78446574 61696c73 27207374 796c653d 27646973 706c6179 3a206e6f 6e653b27 3e0d0a0d 0a3c7461 626c6520 636c6173 733d2743 54657874 273e0d0a 20203c74 723e3c74 643e5765 6220486f 73743a3c 2f74643e 3c74643e 46415853 45525645 522e5345 43555245 43415245 54454348 2e434f4d 3c2f7464 3e3c2f74 723e0d0a 20203c74 723e3c74 643e5765 6220506f 72743a3c 2f74643e 3c74643e 38303c2f 74643e3c 2f74723e 0d0a2020 3c74723e 3c74643e 53657276 6572204e 616d653a 3c2f7464 3e3c7464 3e666178 73657276 65723c2f 74643e3c 2f74723e 0d0a2020 3c74723e 3c74643e 4c6f6361 6c204950 3a3c2f74 643e3c74 643e3130 2e312e35 2e31323c 2f74643e 3c2f7472 3e0d0a20 203c7472 3e3c7464 3e4c6f67 2049443a 3c2f7464 3e3c7464 3e323031 31303530 362e3231 33393038 3535333c 2f74643e 3c2f7472 3e0d0a3c 2f746162 6c653e0d 0a0d0a3c 70726520 636c6173 733d2743 54657874 273e0d0a 0d0a4572 726f7220 4d657373 6167653a 0d0a0d0a 4572726f 7220636f 6e766572 74696e67 20646174 61207479 7065206e 76617263 68617220 746f2062 6967696e 742e0d0a 0d0a4572 726f7220 53746163 6b205472 6163653a 0d0a0d0a 20202061 74205379 7374656d 2e446174 612e5371 6c436c69 656e742e 53716c43 6f6e6e65 6374696f 6e2e4f6e 4572726f 72285371 6c457863 65707469 6f6e2065 78636570 74696f6e 2c20426f 6f6c6561 6e206272 65616b43 6f6e6e65 6374696f 6e290d0a 20202061 74205379 7374656d 2e446174 612e5371 6c436c69 656e742e 53716c49 6e746572 6e616c43 6f6e6e65 6374696f 6e2e4f6e 4572726f 72285371 6c457863 65707469 6f6e2065 78636570 74696f6e 2c20426f 6f6c6561 6e206272 65616b43 6f6e6e65 6374696f 6e290d0a 20202061 74205379 7374656d 2e446174 612e5371 6c436c69 656e742e 54647350 61727365 722e5468 726f7745 78636570 74696f6e 416e6457 61726e69 6e672854 64735061 72736572 53746174 654f626a 65637420 73746174 654f626a 290d0a20 20206174 20537973 74656d2e 44617461 2e53716c 436c6965 6e742e54 64735061 72736572 2e52756e 2852756e 42656861 76696f72 2072756e 42656861 76696f72 2c205371 6c436f6d 6d616e64 20636d64 48616e64 6c65722c 2053716c 44617461 52656164 65722064 61746153 74726561 6d2c2042 756c6b43 6f707953 696d706c 65526573 756c7453 65742062 756c6b43 6f707948 616e646c 65722c20 54647350 61727365 72537461 74654f62 6a656374 20737461 74654f62 6a290d0a 20202061 74205379 7374656d 2e446174 612e5371 6c436c69 656e742e 53716c44 61746152 65616465 722e4861 734d6f72 65526f77 7328290d 0a202020 61742053 79737465 6d2e4461 74612e53 716c436c 69656e74 2e53716c 44617461 52656164 65722e52 65616449 6e746572 6e616c28 426f6f6c 65616e20 73657454 696d656f 7574290d 0a202020 61742053 79737465 6d2e4461 74612e53 716c436c 69656e74 2e53716c 44617461 52656164 65722e52 65616428 290d0a20 20206174 20617861 636f7265 2e6c6962 2e64622e 44625365 7373696f 6e332e52 65616428 290d0a0d 0a3c2f70 72653e0d 0a0d0a3c 2f737061 6e3e0d0a 0d0a>
我的代码如下:
// test has my decoded data above
NSData * test = [[NSData alloc]initWithData:[Base64 decode:faxDocEncodedData]];
UIImage * testimage = [[UIImage alloc] initWithData:test];
UIImageView * faxImage;
[faxImage setImage:testimage];
我得到一个空白的屏幕
我正在使用我正在使用的解码类。再次请记住我没有编码。
#import <Foundation/Foundation.h>
@interface Base64 : NSObject {
}
+ (void) initialize;
+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length;
+ (NSString*) encode:(NSData*) rawBytes;
+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength;
+ (NSData*) decode:(NSString*) string;
@end
这是实施:
#import "Base64.h"
@implementation Base64
#define ArrayLength(x) (sizeof(x)/sizeof(*(x)))
static char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char decodingTable[128];
+ (void) initialize {
if (self == [Base64 class]) {
memset(decodingTable, 0, ArrayLength(decodingTable));
for (NSInteger i = 0; i < ArrayLength(encodingTable); i++) {
decodingTable[encodingTable[i]] = i;
}
}
}
+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length {
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
for (NSInteger i = 0; i < length; i += 3) {
NSInteger value = 0;
for (NSInteger j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger index = (i / 3) * 4;
output[index + 0] = encodingTable[(value >> 18) & 0x3F];
output[index + 1] = encodingTable[(value >> 12) & 0x3F];
output[index + 2] = (i + 1) < length ? encodingTable[(value >> 6) & 0x3F] : '=';
output[index + 3] = (i + 2) < length ? encodingTable[(value >> 0) & 0x3F] : '=';
}
return [[[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding] autorelease];
}
+ (NSString*) encode:(NSData*) rawBytes {
return [self encode:(const uint8_t*) rawBytes.bytes length:rawBytes.length];
}
+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength {
if ((string == NULL) || (inputLength % 4 != 0)) {
return nil;
}
while (inputLength > 0 && string[inputLength - 1] == '=') {
inputLength--;
}
NSInteger outputLength = inputLength * 3 / 4;
NSMutableData* data = [NSMutableData dataWithLength:outputLength];
uint8_t* output = data.mutableBytes;
NSInteger inputPoint = 0;
NSInteger outputPoint = 0;
while (inputPoint < inputLength) {
char i0 = string[inputPoint++];
char i1 = string[inputPoint++];
char i2 = inputPoint < inputLength ? string[inputPoint++] : 'A'; /* 'A' will decode to \0 */
char i3 = inputPoint < inputLength ? string[inputPoint++] : 'A';
output[outputPoint++] = (decodingTable[i0] << 2) | (decodingTable[i1] >> 4);
if (outputPoint < outputLength) {
output[outputPoint++] = ((decodingTable[i1] & 0xf) << 4) | (decodingTable[i2] >> 2);
}
if (outputPoint < outputLength) {
output[outputPoint++] = ((decodingTable[i2] & 0x3) << 6) | decodingTable[i3];
}
}
return data;
}
+ (NSData*) decode:(NSString*) string {
return [self decode:[string cStringUsingEncoding:NSASCIIStringEncoding] length:string.length];
}
@end
答案 0 :(得分:1)
您的代码中缺少两件事
您没有在上面的任何地方初始化faxImage。
您在此[faxImage setImage:testimage];
答案 1 :(得分:0)
来自网络服务器的XML数据回归不正确。请参阅:Is the following a valid base64 encoded tif image response?
感谢每个人试图帮助解决这个问题。