将String转换为byte数组,将byteArray转换为string

时间:2014-11-04 10:23:16

标签: nsstring bytearray

我在本地文件中有字节数据,但是如何将该文本转换为字节数组并从该数据中获取原始信息。

包含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]

1 个答案:

答案 0 :(得分: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