<!-- https://mvnrepository.com/artifact/org.apache.directory.studio/org.apache.commons.codec --> <dependency> <groupId>org.apache.directory.studio</groupId> <artifactId>org.apache.commons.codec</artifactId> <version>1.8</version> </dependency>
DigestUtils.md5Hex(behaviorInterestStr.getBytes());
Java getBytes() 方法
getBytes() 方法有兩種形式:
-
getBytes(String charsetName): 使用指定的字符集將字符串編碼為 byte 序列,並將結果存儲到一個新的 byte 數組中。
-
getBytes(): 使用平台的默認字符集將字符串編碼為 byte 序列,並將結果存儲到一個新的 byte 數組中。
語法
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException 或 public byte[] getBytes()
參數
-
charsetName -- 支持的字符集名稱。
返回值
返回 byte 數組。
實例
import java.io.*; public class Test { public static void main(String args[]) { String Str1 = new String("runoob"); try{ byte[] Str2 = Str1.getBytes(); System.out.println("返回值:" + Str2 ); Str2 = Str1.getBytes( "UTF-8" ); System.out.println("返回值:" + Str2 ); Str2 = Str1.getBytes( "ISO-8859-1" ); System.out.println("返回值:" + Str2 ); } catch ( UnsupportedEncodingException e){ System.out.println("不支持的字符集"); } } }
以上程序執行結果為:
返回值:[B@7852e922 返回值:[B@4e25154f 返回值:[B@70dea4e