Thanks Jonathan,
With some tweaks, my VBA Version here.... bitshifting taken from here...
http://www.excely.com/excel-vba/bit-shifting-function.shtml#.UoUFM8ZSiSo
''' <summary>
''' Encodes a latitude or longitude value by using Google's Polyline algorithm
''' </summary>
''' <param name="ToEnc">Latitude or Longitude to encode (or a difference value) (Single)</param>
''' <returns>Polyline encoded point (String)</returns>
''' <remarks>This function doesn't care what value you pass it, whether it's an absolute coordinate or a difference. Make sure you do all of the point difference calculations somewhere else before calling this method.</remarks>
Function Encode(ByVal ToEnc As Single) As String
Dim Coord As Double 'The integer version of the coordinate, as per steps 2 and 3
Dim B(5) As Byte 'The 5-bit chunks, as per steps 6 and 7. Note that all 6 bytes may not be used
Dim i As Integer 'Generic counter
Dim C(5) As String 'The chunks converted from bytes to characters
Dim E As Integer
E = -1 'Penultimate byte that contains data, the last one to get ORed by 0x20
'2., 3. Take the decimal value and multiply is by 1e5, rounding the result. Convert the decimal value to binary.
Coord = Math.Sgn(ToEnc) * Int(Math.Abs(ToEnc) * 100000)
'4. Left-shift the binary value one bit
Coord = shl(Coord, 1)
'5. If the original decimal value is negative, invert this encoding
If ToEnc < 0 Then Coord = Not Coord
'6. Break the binary value out into 5-bit chunks (starting from the right hand side)
'7. Place the 5-bit chunks in reverse order
'Steps 6 and 7 are done at the same time
B(0) = Coord And &H1F
B(1) = shr((Coord And &H3E0), 5)
B(2) = shr((Coord And &H7C00), 10)
B(3) = shr((Coord And &HF8000), 15)
B(4) = shr((Coord And &H1F00000), 20)
B(5) = shr((Coord And &H3E000000), 25)
'8. OR each value with 0x20 if another bit chunk follows
'Go through the 5-bit chunks looking for the first one that isn't zero
'When we find it, that means the one BEFORE that is the last to get the 0x20 modification
For i = 5 To 1 Step -1
If B(i) <> 0 Then
'This is the first nonzero value we've encountered, so keep track of this position and exit the loop
E = i - 1
Exit For
End If
Next
'Apply the 0x20 modification
For i = 0 To E
B(i) = B(i) Or &H20
Next
'10. Add 63 to each value
For i = 0 To 5
If B(i) > 0 Then B(i) = B(i) + 63
Next
'11. Convert each value to its ASCII equivalent
For i = 0 To 5
C(i) = Chr(B(i))
Encode = Encode + C(i)
Next
'Turn the char array into a string and return it
End Function
Public Function shr(ByVal Value As Long, ByVal Shift As Byte) As Long
Dim i As Byte
shr = Value
If Shift > 0 Then
shr = Int(shr / (2 ^ Shift))
End If
End Function
Public Function shl(ByVal Value As Long, ByVal Shift As Byte) As Long
shl = Value
If Shift > 0 Then
Dim i As Byte
Dim m As Long
For i = 1 To Shift
m = shl And &H40000000
shl = (shl And &H3FFFFFFF) * 2
If m <> 0 Then
shl = shl Or &H80000000
End If
Next i
End If
End Function