java字符串字节数组_java-将字符串转换为字节数组,然后返回原始字符串

导入java.io.FileInputStream;导入java.io.ByteArrayOutputStream;

公共类FileHashStream{     //编写一个新方法,该方法将提供一个新的Byte数组,该数组通常从输入流中读取

public static byte[] read(InputStream is) throws Exception

{

String path = /* type in the absolute path for the 'commons-codec-1.10-bin.zip' */;

// must need a Byte buffer

byte[] buf = new byte[1024 * 16]

// we will use 16 kilobytes

int len = 0;

// we need a new input stream

FileInputStream is = new FileInputStream(path);

// use the buffer to update our "MessageDigest" instance

while(true)

{

len = is.read(buf);

if(len < 0) break;

md.update(buf, 0, len);

}

// close the input stream

is.close();

// call the "digest" method for obtaining the final hash-result

byte[] ret = md.digest();

System.out.println("Length of Hash: " + ret.length);

for(byte b : ret)

{

System.out.println(b + ", ");

}

String compare = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";

String verification = Hex.encodeHexString(ret);

System.out.println();

System.out.println("===")

System.out.println(verification);

System.out.println("Equals? " + verification.equals(compare));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值