How to read a number from highest place value to lowest?
Well, you could construct the inverse of that number. 12312 will become 21321 and then use the piece of code you wrote. So:
while (n)
{inverse = inverse * 10 + (a % 10), a /= 10;}.