2014-11-04 296 views
0

我在本地文件中有字節數據,但是如何將該文本轉換爲字節數組並且還從該數據獲取原始信息。將字符串轉換爲字節數組並將字節數組轉換爲字符串

含有txt文件下列數據的文件

[80,75,3,4,20,0,6,0,8,0,0,0,33,0,170,247,88,164,122,1,0 ,0,20,6,0,0,19,0,8,2,91,67,111,110,116,101,110,116,95,84,121,112,101,115,93,46,120,109,108,32,162,4,2,40,160,0,2,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,204,84,201,106,195,48,16,189,23,250,15,70,215,18,43,73,161,148 ,18,39,135,46,199,54,144,244,3,20,107,108,139,216,146,208,76,210,228,239,59,118,22,74,200,130,105,160,189,216,216,210,188,101,134,121,131,209,170,42,163,37,4,52,206,38,162,23,119,69,4,54,117,218,216,60,17,159,211,183,206,163,136,144,148,213,170,116,22,18,177,6,20,163,225,237,205 ,96,186,246,128,17,87,91,76,68,65,228,159,164,196,180,128,74,97,236,60,88,62,201,92,168,20,241,103,200,165,87,233,92,229,32,251,221,238,131,76,157,37,176,212,161,26,67,12,7,47, 144,169,69,73,209,235,138]

回答

0

這不你問究竟什麼

NSString *text = @"[80,75,3,4,20,0,6,0,8,0,0,0,33,0,170,247,88,164,122,1,0,0,20,6,0,0,19,0,8,2,91,67,111,110,116,101,110,116,95,84,121,112,101,115,93,46,120,109,108,32,162,4,2,40,160,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,204,84,201,106,195,48,16,189,23,250,15,70,215,18,43,73,161,148,18,39,135,46,199,54,144,244,3,20,107,108,139,216,146,208,76,210,228,239,59,118,22,74,200,130,105,160,189,216,216,210,188,101,134,121,131,209,170,42,163,37,4,52,206,38,162,23,119,69,4,54,117,218,216,60,17,159,211,183,206,163,136,144,148,213,170,116,22,18,177,6,20,163,225,237,205,96,186,246,128,17,87,91,76,68,65,228,159,164,196,180,128,74,97,236,60,88,62,201,92,168,20,241,103,200,165,87,233,92,229,32,251,221,238,131,76,157,37,176,212,161,26,67,12,7,47,144,169,69,73,209,235,138]"; 

// Delete the first and last character ('[' and ']') 
text = [text substringWithRange:NSMakeRange(1, [text length] - 2)]; 

// Populate an array with the string representation of every number in the text file 
NSArray *numbersAsStrings = [text componentsSeparatedByString:@","]; 

// Declare an array of bytes 
Byte byteData[[numbersAsStrings count]]; 

// Fill the byte array using the numbers from the text file 
for (int i = 0; i < numbersAsStrings.count; i++) { 
    NSString *currentNumber = [numbersAsStrings objectAtIndex:i]; 
    byteData[i] = [currentNumber intValue]; 
} 

// Use the byte array to create an NSString represantation of the original text 
NSString *convertedText = [[NSString alloc] initWithBytes:byteData length:sizeof(byteData) encoding:<#(NSStringEncoding)#>]; 

NSLog(@"%@", convertedText); 

只需添加用於生成該文件的編碼字符串,你可以找到完整的列表here