diff options
Diffstat (limited to 'main/geohash.c')
| -rw-r--r-- | main/geohash.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/main/geohash.c b/main/geohash.c index f649824..dd0e29d 100644 --- a/main/geohash.c +++ b/main/geohash.c | |||
| @@ -38,10 +38,12 @@ void geohash_encode(double lat, double lon, int precision, char *out) | |||
| 38 | for (int i = 0; i < precision; i++) { | 38 | for (int i = 0; i < precision; i++) { |
| 39 | int byte_idx = (i * 5) / 8; | 39 | int byte_idx = (i * 5) / 8; |
| 40 | int bit_offset = (i * 5) % 8; | 40 | int bit_offset = (i * 5) % 8; |
| 41 | uint16_t val = (hash_bytes[byte_idx] << 8); | 41 | uint32_t val = ((uint32_t)hash_bytes[byte_idx] << 16); |
| 42 | if (byte_idx + 1 < (int)sizeof(hash_bytes)) | 42 | if (byte_idx + 1 < (int)sizeof(hash_bytes)) |
| 43 | val |= hash_bytes[byte_idx + 1]; | 43 | val |= ((uint32_t)hash_bytes[byte_idx + 1] << 8); |
| 44 | val = (val >> (16 - 5 - bit_offset)) & 0x1F; | 44 | if (byte_idx + 2 < (int)sizeof(hash_bytes)) |
| 45 | val |= hash_bytes[byte_idx + 2]; | ||
| 46 | val = (val >> (24 - 5 - bit_offset)) & 0x1F; | ||
| 45 | out[i] = BASE32[val]; | 47 | out[i] = BASE32[val]; |
| 46 | } | 48 | } |
| 47 | out[precision] = '\0'; | 49 | out[precision] = '\0'; |