Cc.FileUtils getByteArrayFromFile Uint8Array to String

I am using the following code to convert a Uint8Array object to string:

@
var fileData = cc.FileUtils.getInstance().getByteArrayFromFile(“file.txt”);
for(i = 0; i < fileData.length; i++) {
console.log(i + “:” + fileData[i]);
}
@

the output is:

0: 116
1: 104
2: 97
3: 110
4: 107
5: 32
6: 121
7: 111
8: 117
9: 32
10: 34
11: 34

the last two chinese characters value is obvious wrong.

Then I tried:

@
var fileData = “thank you 谢谢”;
for(i = 0; i < fileData.length; i++) {
console.log(i + “:” + fileData.charCodeAt(i));
}
@

and the output is:

0: 116
1: 104
2: 97
3: 110
4: 107
5: 32
6: 121
7: 111
8: 117
9: 32
10: 35874
11: 35874

that’s the right output it should be ~

Hope someone can help me, thanks :slight_smile:

Hi Super SuRaccoon,

cc.FileUtils getByteArrayFromFile is used to read a binary file, for example “thank you 谢谢” is not binary characters.

Hi Super SuRaccoon, you could use cc.FileUtils getTextFileData.
The utf-8 format is supported now, please refer to this commit