Java tutorial
//package com.java2s; // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE public class Main { /** * Converts the given number into a char-array. If the length of the array * is shorter than the required exact length, the array is padded with leading * '0' chars. If the array is longer than the wanted length the most * significant digits are cut off until the array has the exact length. * * @param number The number to convert to a char array. * @param exactArrayLength The exact length of the returned array. * @return The numebr as char array, one char for each decimal digit. * @preconditions (exactArrayLength >= 0) * @postconditions (result <> null) * and (result.length == exactArrayLength) */ public static char[] toCharArray(int number, int exactArrayLength) { char[] charArray = null; String numberString = Integer.toString(number); char[] numberChars = numberString.toCharArray(); if (numberChars.length > exactArrayLength) { // cut off digits beginning at most significant digit charArray = new char[exactArrayLength]; for (int i = 0; i < charArray.length; i++) { charArray[i] = numberChars[i]; } } else if (numberChars.length < exactArrayLength) { // pad with '0' leading chars charArray = new char[exactArrayLength]; int offset = exactArrayLength - numberChars.length; for (int i = 0; i < charArray.length; i++) { charArray[i] = (i < offset) ? '0' : numberChars[i - offset]; } } else { charArray = numberChars; } return charArray; } }