buf = 3 bytes from the HTU21D-F for temperature or humidity 2 data bytes and 1 crc8 byte len = number of bytes in buf but it must be 3. return value < 0 error return value = 0 CRC good return value > 0 CRC bad
423595
function calc_crc8(buf, len)
{
    var dataandcrc;
    // Generator polynomial: x**8 + x**5 + x**4 + 1 = 1001 1000 1
    var poly = 0x98800000;
    var i;

    if (len === null) return -1;
    if (len != 3) return -1;
    if (buf === null) return -1;

    // Justify the data on the MSB side. Note the poly is also
    // justified the same way.
    dataandcrc = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8);
    for (i = 0; i < 24; i++) {
        if (dataandcrc & 0x80000000)
            dataandcrc ^= poly;
        dataandcrc <<= 1;
    }
    return (dataandcrc === 0);
}
1020808
function encodeNumber(buf, value, len) {
  var index = 0;
  
  while (len > 0) {
    var tmpValue = value >>> ((len - 1) * 8);
    buf[index++] = tmpValue & 0xFF;
    len--;
  }
}
810396
function crxToZip(buf) {
    function calcLength(a, b, c, d) {
        let length = 0;

        length += a << 0;
        length += b << 8;
        length += c << 16;
        length += d << 24 >>> 0;
        return length;
    }

    // 50 4b 03 04
    // This is actually a zip file
    if (buf[0] === 80 && buf[1] === 75 && buf[2] === 3 && buf[3] === 4) {
        return buf;
    }

    // 43 72 32 34 (Cr24)
    if (buf[0] !== 67 || buf[1] !== 114 || buf[2] !== 50 || buf[3] !== 52) {
        throw new Error("Invalid header: Does not start with Cr24");
    }

    // 02 00 00 00
    // or
    // 03 00 00 00
    const isV3 = buf[4] === 3;
    const isV2 = buf[4] === 2;

    if ((!isV2 && !isV3) || buf[5] || buf[6] || buf[7]) {
        throw new Error("Unexpected crx format version number.");
    }

    if (isV2) {
        const publicKeyLength = calcLength(buf[8], buf[9], buf[10], buf[11]);
        const signatureLength = calcLength(buf[12], buf[13], buf[14], buf[15]);

        // 16 = Magic number (4), CRX format version (4), lengths (2x4)
        const zipStartOffset = 16 + publicKeyLength + signatureLength;

        return buf.slice(zipStartOffset, buf.length);
    }
    // v3 format has header size and then header
    const headerSize = calcLength(buf[8], buf[9], buf[10], buf[11]);
    const zipStartOffset = 12 + headerSize;

    return buf.slice(zipStartOffset, buf.length);
}
308777
function floatFromBuffer (buf) {
  if (buf.length < FLOAT_ENTROPY_BYTES) {
    throw new Error(
      'buffer must contain at least ' + FLOAT_ENTROPY_BYTES + ' bytes of entropy'
    )
  }
  var position = 0

  // http://stackoverflow.com/questions/15753019/floating-point-number-from-crypto-randombytes-in-javascript
  return (((((((
    buf[position++] % 32) / 32 +
    buf[position++]) / 256 +
    buf[position++]) / 256 +
    buf[position++]) / 256 +
    buf[position++]) / 256 +
    buf[position++]) / 256 +
    buf[position]) / 256
}
1345307
def _get_temperature(self, data):
        ''''''
        if data[1:2] == 0x7FFF:
            return None

        temperature = twos_complement((data[1] << 8) + data[2], 16) / 200
        return round(temperature, 2)
105406
function amf3decDate(buf) {  // The UI29 should be 1
    var uTz = amf3decUI29(buf);
    var ts = buf.readDoubleBE(uTz.len);
    return { len: uTz.len + 8, value: ts }
}
531448
def accumulate(self, buf):
        ''''''
        accum = self.crc
        for b in buf:
            tmp = b ^ (accum & 0xff)
            tmp = (tmp ^ (tmp<<4)) & 0xFF
            accum = (accum>>8) ^ (tmp<<8) ^ (tmp<<3) ^ (tmp>>4)
        self.crc = accum
799781
def read_binary_real(fname,fd,length)
      raise CFFormatError.new("Real greater than 8 bytes: #{length}") if length > 3

      nbytes = 1 << length
      buff = fd.read(nbytes)

      CFReal.new(
        case length
        when 0 # 1 byte float? must be an error
          raise CFFormatError.new("got #{length+1} byte float, must be an error!")
        when 1 # 2 byte float? must be an error
          raise CFFormatError.new("got #{length+1} byte float, must be an error!")
        when 2 then
          buff.reverse.unpack("e")[0]
        when 3 then
          buff.reverse.unpack("E")[0]
        else
          fail "unexpected length: #{length}"
        end
      )
    end
159934
def _crc16(data, crc, table):
    
    bytes_to_int = lambda x: ord(x) if sys.version_info.major == 2 else x
    for byte in data:
        crc = ((crc << 8) & 0xff00) ^ table[((crc >> 8) & 0xff) ^ bytes_to_int(byte)]
    return crc & 0xffff
802174
@GwtIncompatible("doesn't work")
  public static int fromBytes(byte b1, byte b2, byte b3, byte b4) {
    return b1 << 24 | (b2 & 0xFF) << 16 | (b3 & 0xFF) << 8 | (b4 & 0xFF);
  }
856637
private void returnTemperature(Buffer buffer) {
        Callback<Integer> callback = getFirstCallback(BeanMessageID.CC_TEMP_READ);
        if (callback != null) {
            callback.onResult((int) buffer.readByte());
        }
    }
131347
private int getNumOfLenBytes(int len) {
        int numOfLenBytes = 0;

        if (len < 128) {
            numOfLenBytes = 1;
        } else if (len < (1 << 8)) {
            numOfLenBytes = 2;
        } else if (len < (1 << 16)) {
            numOfLenBytes = 3;
        } else if (len < (1 << 24)) {
            numOfLenBytes = 4;
        } else {
            numOfLenBytes = 5;
        }
        return numOfLenBytes;
    }
690569
public static int readUnsignedShortBigEndian(ByteBuffer buf) {
		int value;
		value = ((buf.get() & 0xFF)) << 8;
		value |= (buf.get() & 0xFF);
		return value;
	}
276859
def temperature(self):
        
        result = self.i2c_read(2)
        value = struct.unpack('>H', result)[0]

        if value < 32768:
            return value / 256.0
        else:
            return (value - 65536) / 256.0
751289
private static int oneUcs4ToUtf8Char(byte[] utf8Buffer, int ucs4Char) {
        int utf8Length = 1;

        //JS_ASSERT(ucs4Char <= 0x7FFFFFFF);
        if ((ucs4Char & ~0x7F) == 0)
            utf8Buffer[0] = (byte)ucs4Char;
        else {
            int i;
            int a = ucs4Char >>> 11;
            utf8Length = 2;
            while (a != 0) {
                a >>>= 5;
                utf8Length++;
            }
            i = utf8Length;
            while (--i > 0) {
                utf8Buffer[i] = (byte)((ucs4Char & 0x3F) | 0x80);
                ucs4Char >>>= 6;
            }
            utf8Buffer[0] = (byte)(0x100 - (1 << (8-utf8Length)) + ucs4Char);
        }
        return utf8Length;
    }
1180231
public static int crc16(byte[] bytes, int off, int len) {

        int crc = 0x0000;
        int end = off + len;

        for (int i = off; i < end; i++) {
            crc = doCrc(bytes[i], crc);
        }

        return crc & 0xFFFF;
    }
1319593
def _decode_temp(byte_1, byte_2):
        """"""
        temp = (byte_1 << 8) + byte_2
        if (temp > 32767):
            temp = temp - 65536
        temp = temp / 10
        return temp