0

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!

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
modernator
  • 4,341
  • 12
  • 47
  • 76
  • 1
    27904 is hex 0x6d00, and the decimal version of 0x6d is 109. – unwind Feb 21 '17 at 11:09
  • 1
    Java experts, don't crucify me for asking this: Aren't java multi-byte scalars [big-endian](http://stackoverflow.com/questions/362384/are-integers-in-java-little-endian-or-big-endian)? that seems to be where this is heading. You have a BE-short being loaded into something that uses LE. – WhozCraig Feb 21 '17 at 11:09
  • 2
    @WhozCraig You meant JavaScript, right? – unwind Feb 21 '17 at 11:14
  • 1
    https://en.wikipedia.org/wiki/Endianness – Selçuk Cihan Feb 21 '17 at 11:15
  • 1
    "As I know, short int is 2 byte length" - That's not guaranteed. Don't rely on it, use correct marshalling with bitshifts & bitops. There are enough Q&A here, do some research. – too honest for this site Feb 21 '17 at 11:38
  • 1
    When debugging binary data always use at least hex representation. You would have seen that f.ex. the first number expected is 0x6D, what you get is 0x6d00, ie. a byte-order problem. –  Feb 24 '17 at 03:40
  • @K3N Okay, thanks! – modernator Feb 24 '17 at 06:49

1 Answers1

0

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;
}
modernator
  • 4,341
  • 12
  • 47
  • 76