Skip to content

Instantly share code, notes, and snippets.

@sui77
Created July 30, 2019 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sui77/853de79d4d496fc2e50dda15fac464af to your computer and use it in GitHub Desktop.
Save sui77/853de79d4d496fc2e50dda15fac464af to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/9.0.0/bignumber.min.js"></script>
<script>
function packTorch(entryId, operationId, lat, long) {
let sEntryId = parseInt(entryId).toString(16);
let sOperationId = new BigNumber(operationId).toString(16);
// map coords to positive int range
let sLat = Math.round((90 + lat) * 1e+7).toString(16);
let sLong = Math.round((180 + long) * 1e+7).toString(16);
let hex = padZero(sEntryId, 5)
+ padZero(sOperationId, 16)
+ padZero(sLat, 8)
+ padZero(sLong, 8);
// fill with zeros cause MEMO_HASH expects 64 chars
while (hex.length < 64) {
hex += '0';
}
return hex;
}
function unpackTorch(s) {
let entryId = parseInt('0x' + s.substr(0, 5));
let operationId = new BigNumber('0x' + s.substr(5, 16)).toString();
let lat = (parseInt('0x' + s.substr(21, 8)) / 1e+7 - 90).toFixed(7);
let long = (parseInt('0x' + s.substr(29, 8)) / 1e+7 - 180).toFixed(7);
return {
entryId: entryId,
operationId: operationId,
lat: lat,
long: long
}
}
function padZero(s, len) {
while (s.length < len) s = '0' + s;
return s;
}
let packed = packTorch(45, '107482355446345729', 53.5539,10.01602);
console.log("packed: ", packed);
let unpacked = unpackTorch(packed);
console.log("unpacked: ", unpacked);
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment