Android Question Compress bytes

Blueforcer

Well-Known Member
Licensed User
Longtime User
i need to convert following JS code, but i dont know how to do it in B4A
Can anyone help me please?

JavaScript:
const customHash = (input) => {
  const hexdigest = md5(input);
  const pairs = hexdigest.match(/(..?)/g);
  const bytes = pairs.map(x => parseInt(x, 16));
  const compressed = compress(bytes, 3);

  Console.log([compressed[0]]);
  Console.log([compressed[1]]);
  Console.log([compressed[2]]);
 
};


const compress = (bytes, target) => {
  const { length } = bytes;
  if (target > length) throw new Error('Fewer input bytes than requested output');

  // Calculate the segment size (divide and round down)
  const segSize = length / target >> 0;

  // Split 'bytes' array into 'target' number of segments.
  const segments = [];
  for (let i = 0; i < segSize * target; i += segSize) {
    segments.push(bytes.slice(i, i + segSize));
  }

  // Catch any left-over bytes in the last segment.
  const lastSeg = segments[segments.length - 1];
  segments[segments.length - 1] = lastSeg.concat(bytes.slice(target * segSize));

  const checksums = segments.map(x => x.reduce((acc, curr) => acc ^ curr));
  return checksums;
};

export default compress;
 
Top