Real easy to do. Just needs a little math. So suppose your grid is based on 16x16 sized squares. So if a block is anywhere from pixel (I'll just be talking about horizontally so the x-axis but the same will apply to the vertical Y-axis) 0-15, it will be in grid 1 and if it is in 16-31, then grid 2 and so on. So lets pick a pixel location say at x position 17. Take that number 17 and divide by the size of your grids which is 16. 17/16 = 1.0625. Now we want to use the floor fuction on that number so the floor(1.0625) = 1. Now multiply your new number 1 by the size of your grid which is...16! And you get 16. So any location from 0-15 will become 0, any location from 16 to 31 will become 16 and so on.
Here's some more math stuff you can play around with.