Convert String to Byte in Java with Examples - Java 147

Convert String to Byte in Java with Examples – Java 147

Convert String to Byte in Java with Examples

對包含 byte 的字串進行數學運算時,通常會使用轉換成 byte ,從文本字段或文本區域讀取資料時,輸入的資料都會作為字串接收,將字串轉換為 byte ,可以使用 decode 、 Apache Commons Lang 等方法進行操作, Convert a String to a Byte in Java 任務是將該字串轉換為整數,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- conversion
       |                   +- string
       |                       +- StringToByteWithExamplesTest.java   

單元測試

Convert a String to Byte in Java 提供字串轉換成整數等操作。

decode

Convert a String to Byte in Java 使用 Byte.decode 轉換成整數。

	@Test
	public void decode() {
		String value = "102";
		byte result = Byte.decode(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "-60";
		result = Byte.decode(value);
		System.out.println(result);
		assertEquals(-60, result);
	}
102
-60

decodeThrowException

Convert a String to Byte in Java 使用 Byte.decode 轉換成整數,輸入不合格式的字串,會拋出例外。

	@Test(expected = NumberFormatException.class)
	public void decodeThrowException() {
		String value = "102";
		byte result = Byte.decode(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "httpclient-102";
		result = Byte.decode(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.Integer.valueOf(Integer.java:740)
	at java.lang.Integer.decode(Integer.java:1197)
	at java.lang.Byte.decode(Byte.java:277)
	at org.ruoxue.java_147.string.StringToByteWithExamplesTest.decodeThrowException(StringToByteWithExamplesTest.java:31)

constructor

Convert a String to Byte in Java 使用 Byte 建構子,傳入參數字串,建立一個新的 Byte 物件。

	@Test
	public void constructor() {
		String value = "102";
		byte result = new Byte(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "-60";
		result = new Byte(value);
		System.out.println(result);
		assertEquals(-60, result);
	}
102
-60

constructorThrowException

使用 Byte 建構子,傳入參數字串,建立一個新的 Byte 物件,輸入不合格式的字串,會拋出例外。

	@Test(expected = NumberFormatException.class)
	public void constructorThrowException() {
		String value = "102";
		byte result = new Byte(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "httpclient-102";
		result = new Byte(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.<init>(Byte.java:316)
	at org.ruoxue.java_147.string.StringToByteWithExamplesTest.constructorThrowException(StringToByteWithExamplesTest.java:56)

NumberUtils_toByte

使用 Apache Commons Lang – NumberUtils 轉換成整數。

	@Test
	public void NumberUtils_toByte() {
		String value = "102";
		byte result = NumberUtils.toByte(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "-60";
		result = NumberUtils.toByte(value);
		System.out.println(result);
		assertEquals(-60, result);

		value = "60";
		result = NumberUtils.toByte(value, (byte) 0);
		System.out.println(result);
		assertEquals(60, result);

		value = "httpclient-102";
		result = NumberUtils.toByte(value, (byte) 0);
		System.out.println(result);
		assertEquals(0, result);
	}
102
-60
60
0

StringToByteWithExamplesTest.java

Java String to Byte Examples 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.conversion.string;

import static org.junit.Assert.*;

import org.apache.commons.lang3.math.NumberUtils;
import org.junit.Test;

public class StringToByteWithExamplesTest {

	@Test
	public void decode() {
		String value = "102";
		byte result = Byte.decode(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "-60";
		result = Byte.decode(value);
		System.out.println(result);
		assertEquals(-60, result);
	}

	@Test(expected = NumberFormatException.class)
	public void decodeThrowException() {
		String value = "102";
		byte result = Byte.decode(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "httpclient-102";
		result = Byte.decode(value);
		System.out.println(result);
	}

	@Test
	public void constructor() {
		String value = "102";
		byte result = new Byte(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "-60";
		result = new Byte(value);
		System.out.println(result);
		assertEquals(-60, result);
	}

	@Test(expected = NumberFormatException.class)
	public void constructorThrowException() {
		String value = "102";
		byte result = new Byte(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "httpclient-102";
		result = new Byte(value);
		System.out.println(result);
	}

	@Test
	public void NumberUtils_toByte() {
		String value = "102";
		byte result = NumberUtils.toByte(value);
		System.out.println(result);
		assertEquals(102, result);

		value = "-60";
		result = NumberUtils.toByte(value);
		System.out.println(result);
		assertEquals(-60, result);

		value = "60";
		result = NumberUtils.toByte(value, (byte) 0);
		System.out.println(result);
		assertEquals(60, result);

		value = "httpclient-102";
		result = NumberUtils.toByte(value, (byte) 0);
		System.out.println(result);
		assertEquals(0, result);
	}
}

心得分享

Convert a String to a Byte in Java with Examples 將 String 實例轉換為 byte 或 Byte 實例的多種方法, 除了使用 Java 所提供的 decode 、 constructor 之外,也有其他選擇使用外部程式庫來轉換成 byte ,像是 Apache Commons Lang 等, Java String to Byte Examples 提供了幾種常見方法的操作範例。

發佈留言