Sunday 28 May 2017

I want to translate this code.(node.js -> python)

node js

  function crc32(buf, size) {
  "use strict";
  let crc = 0xffffffff;
  for (let i = 0; i < size; i += 1) {
    crc = ((crc << 8) ^ crctable.table[((crc >> 24) ^ buf[i]) & 0xff]); // eslint-disable-line
  }
  const buffer = new Buffer(4);
  buffer.writeUIntBE(0xffffffff + crc + 1, 0, 4, true);
  return buffer;
}

python

def crc_create(self, buf):
        with open('crc.json') as data_file:
            crc_table = json.load(data_file)
        crc = 0xffffffff
        for i in buf:
            print i
            crc = (crc << 8)^int(crc_table['table'][((crc >> 24) ^ i) & 0xff], 16)
        return 0xffffffff + crc + 1

When the same character is assigned, these values(^buf[i], ^i) & 0xff) are different

Why do these codes show different results?

The correct result is nodejs and i want to know the correct Python code.



via David.K

No comments:

Post a Comment