| Refresh | Home EGTry.com

convert integer to hex string


java code

package bin;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;


public class IntToHexString {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		for(int i=0; i<15; i++) {
			int num=16*i;
			String hex=intToHex(num);
			System.out.println(num+"=>"+hex);
		}

	}
	
	public static String intToHex(int i) {
		String hex=Integer.toHexString(i);
		if (hex.length()<2) hex="0"+hex;
		return hex;
	}

}


sample output

0=>00
16=>10
32=>20
48=>30
64=>40
80=>50
96=>60
112=>70
128=>80
144=>90
160=>a0
176=>b0
192=>c0
208=>d0
224=>e0