I wrote JavaScript string as binary that converts each character of string to Uint16Array(2bytes) like this:
for(let i = 0; i < name.length; i++) {
dv.setUint16(i * 2, name.charCodeAt(i));
}
I checked the file size and byte number was correct, but when I read a string from C, it prints unexpected numbers:
27904
28416
25600
25856
29184
28160
24832
29696
28416
29184
Read from JavaScript, it's like this, as I expected:
109
111
100
101
114
110
97
116
111
114
This is a code of C that reading a string from file:
// Read name
for(int i = 0; i < nameLen; i++) {
short int c;
fread(&c, 2, 1, fp);
printf("%d\n", c);
}
And it's JavaScript code for reading string:
for(let i = 0; i < nameLen; i++) {
let char = dv.getUint16(i * 2); // Each of character has 2 bytes
console.log(char);
}
Also when I tried with int, it saids:
117468416
117468928
117466112
117466368
117469696
117468672
117465344
117470208
117468928
117469696
As I know, short int is 2 byte length, so theoritically looks like it's fine, but why am I got those big numbers?
I tried this on my computer,
printf("%d\n", sizeof(short int));
And it saids 2.
I'm not good at C, so actually it's quite hard to find out what is the problem.
Any advice will very appreciate it. Thank you!
Thank you for comments. I resolved with shifting 8 bits to right, because DataView of JavaScript writes binary as little-endian as default.
char name[100] = "";
for(int i = 0; i < nameLen; i++) {
short int c;
fread(&c, 2, 1, fp);
c >>= 8;
}