Table of Contents
ToggleJava Convert String to Byte
對包含 byte 的字串進行數學運算時,通常會使用轉換成 byte ,從文本字段或文本區域讀取資料時,輸入的資料都會作為字串接收,將字串轉換為 byte ,可以使用 parseByte 、 valueOf 等方法進行操作, Java String Byte 任務是將該字串轉換為整數,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- string
| +- StringToByteTest.java
單元測試
Java String to Byte 提供字串轉換成整數等操作。
parseByte
Java String to Byte 使用 Byte.parseByte 轉換成整數,此方法接受另一個參數來更改基數,例如 16(十六進制)或 2(二進制)。
@Test
public void parseByte() {
String value = "102";
byte result = Byte.parseByte(value);
System.out.println(result);
assertEquals(102, result);
value = "-60";
result = Byte.parseByte(value);
System.out.println(result);
assertEquals(-60, result);
value = "60";
// (6)*16^1 + (0)*16^0 = 96
// 96 + 0 = 96
result = Byte.parseByte(value, 16);
System.out.println(result);
assertEquals(96, result);
value = "111110";
// (1)*2^5 + (1)*2^4 + (1)*2^3 + (1)*2^2 + (1)*2^1 + (0)*2^0 = 62
// 32 + 16 + 8 + 4 + 2 = 62
result = Byte.parseByte(value, 2);
System.out.println(result);
assertEquals(62, result);
102
-60
96
62
parseByteThrowException
Java String to Byte 使用 Byte.parseByte 轉換成整數,輸入不合格式的字串,會拋出例外。
@Test(expected = NumberFormatException.class)
public void parseByteThrowException() {
String value = "102";
byte result = Byte.parseByte(value);
System.out.println(result);
assertEquals(102, result);
value = "httpclient-102";
result = Byte.parseByte(value);
System.out.println(result);
}
102
java.lang.NumberFormatException: For input string: "httpclient-102"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Byte.parseByte(Byte.java:149)
at java.lang.Byte.parseByte(Byte.java:175)
at org.ruoxue.java_147.string.StringToByteTest.parseByteThrowException(StringToByteTest.java:44)
valueOf
Java String to Byte 使用 Byte.valueOf 轉換成整數。
@Test
public void valueOf() {
String value = "102";
Byte result = Byte.valueOf(value);
System.out.println(result);
assertEquals(102, result.byteValue());
value = "-60";
result = Byte.valueOf(value);
System.out.println(result);
assertEquals(-60, result.byteValue());
value = "60";
// (6)*16^1 + (0)*16^0 = 96
result = Byte.valueOf(value, 16);
System.out.println(result);
assertEquals(96, result.byteValue());
value = "111110";
// (1)*2^5 + (1)*2^4 + (1)*2^3 + (1)*2^2 + (1)*2^1 + (0)*2^0 = 62
// 32 + 16 + 8 + 4 + 2 = 62
result = Byte.valueOf(value, 2);
System.out.println(result);
assertEquals(62, result.byteValue());
}
102
-60
96
62
valueOfThrowException
Convert String to Byte in Java 使用 Byte.valueOf 轉換成整數,輸入不合格式的字串,會拋出例外。
@Test(expected = NumberFormatException.class)
public void valueOfThrowException() {
String value = "102";
Byte result = Byte.valueOf(value);
System.out.println(result);
assertEquals(102, result.byteValue());
value = "httpclient-102";
result = Byte.valueOf(value);
System.out.println(result);
}
102
java.lang.NumberFormatException: For input string: "httpclient-102"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Byte.parseByte(Byte.java:149)
at java.lang.Byte.valueOf(Byte.java:205)
at java.lang.Byte.valueOf(Byte.java:231)
at org.ruoxue.java_147.string.StringToByteTest.valueOfThrowException(StringToByteTest.java:82)
StringToByteTest.java
Convert String to Byte in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.string;
import static org.junit.Assert.*;
import org.junit.Test;
public class StringToByteTest {
@Test
public void parseByte() {
String value = "102";
byte result = Byte.parseByte(value);
System.out.println(result);
assertEquals(102, result);
value = "-60";
result = Byte.parseByte(value);
System.out.println(result);
assertEquals(-60, result);
value = "60";
// (6)*16^1 + (0)*16^0 = 96
// 96 + 0 = 96
result = Byte.parseByte(value, 16);
System.out.println(result);
assertEquals(96, result);
value = "111110";
// (1)*2^5 + (1)*2^4 + (1)*2^3 + (1)*2^2 + (1)*2^1 + (0)*2^0 = 62
// 32 + 16 + 8 + 4 + 2 = 62
result = Byte.parseByte(value, 2);
System.out.println(result);
assertEquals(62, result);
}
@Test(expected = NumberFormatException.class)
public void parseByteThrowException() {
String value = "102";
byte result = Byte.parseByte(value);
System.out.println(result);
assertEquals(102, result);
value = "httpclient-102";
result = Byte.parseByte(value);
System.out.println(result);
}
@Test
public void valueOf() {
String value = "102";
Byte result = Byte.valueOf(value);
System.out.println(result);
assertEquals(102, result.byteValue());
value = "-60";
result = Byte.valueOf(value);
System.out.println(result);
assertEquals(-60, result.byteValue());
value = "60";
// (6)*16^1 + (0)*16^0 = 96
result = Byte.valueOf(value, 16);
System.out.println(result);
assertEquals(96, result.byteValue());
value = "111110";
// (1)*2^5 + (1)*2^4 + (1)*2^3 + (1)*2^2 + (1)*2^1 + (0)*2^0 = 62
// 32 + 16 + 8 + 4 + 2 = 62
result = Byte.valueOf(value, 2);
System.out.println(result);
assertEquals(62, result.byteValue());
}
@Test(expected = NumberFormatException.class)
public void valueOfThrowException() {
String value = "102";
Byte result = Byte.valueOf(value);
System.out.println(result);
assertEquals(102, result.byteValue());
value = "httpclient-102";
result = Byte.valueOf(value);
System.out.println(result);
}
}
心得分享
String to Byte in Java 轉換方法 parseByte 和 valueOf 似乎完全相同,分別傳回基本型別 byte 及物件 Byte , valueOf 方法在內部委託給 parseByte 方法,這兩種方法之間存在一個細微差別: valueOf 方法在內部使用整數快取,快取將為 -128 和 127 之間的數字返回相同的 Byte 實例,建議使用 valueOf 而不是 parseByte 來取得物件 Byte ,因為快取可以為系統提高效率, Convert String to Byte in Java 提供將 String 實例轉換為 byte 或 Byte 實例的多種方法。