[Java] 16진수 변환하기

샘플코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public static void main(String[] args) throws IOException {
	parseByte();
}

public static double parseByte() {
	String hex;
	// for Test
	hex = "29a"; // 666
//		hex = "3e7"; // 999
//		hex = "FFFFFD05"; // -763
//		hex = "FFFFFFFFF1"; // -15
	
	System.out.println("hex : "+hex);
	System.out.println("hexToDec(hex) : "+hexToDec(hex));
	
	return Double.parseDouble(String.valueOf(hexToDec(hex)));
}

/**
 * @param hex
 * @return
 */
public static Number hexToDec(String hex) {

	if (hex == null) {
		throw new NullPointerException("hexToDec: hex String is null.");
	}

	// You may want to do something different with the empty string.
	if (hex.equals("")) {
		return Byte.valueOf("0");
	}

	// If you want to pad "FFF" to "0FFF" do it here.

	hex = hex.toUpperCase();

	// Check if high bit is set.
	boolean isNegative = hex.startsWith("8") || hex.startsWith("9") || hex.startsWith("A") || hex.startsWith("B")
			|| hex.startsWith("C") || hex.startsWith("D") || hex.startsWith("E") || hex.startsWith("F");

	BigInteger temp;

	if (isNegative) {
		// Negative number
		temp = new BigInteger(hex, 16);
		BigInteger subtrahend = BigInteger.ONE.shiftLeft(hex.length() * 4);
		temp = temp.subtract(subtrahend);
	} else {
		// Positive number
		temp = new BigInteger(hex, 16);
	}

	// Cut BigInteger down to size.
	if (hex.length() <= 2) {
		return (Byte) temp.byteValue();
	}
	if (hex.length() <= 4) {
		return (Short) temp.shortValue();
	}
	if (hex.length() <= 8) {
		return (Integer) temp.intValue();
	}
	if (hex.length() <= 16) {
		return (Long) temp.longValue();
	}

	return temp;
}


Tags:

Updated:

Leave a comment