输入流bytearrayinputstream转为输出流bytearrayoutputstream

  • Post author:
  • Post category:其他


输入流转输出流

如下:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class TestAbc {
	public static void main(String[] args) {
		byte[] b = new byte[] { -116, -126, -104, 77, 76, 86, 98, 104, 113,
				102, 80, 68, 89, 85, 65, 0, -115, -112, -125, 104, 116, 116,
				112, 58, 47, 47, 50, 49, 49, 46, 49, 51, 54, 46, 49, 49, 50,
				46, 56, 52, 47, 77, 76, 86, 98, 104, 113, 102, 80, 68, 89, 85,
				65, 0, -120, 5, -127, 3, 3, -12, 99, -119, 23, -128, 49, 51,
				54, 53, 57, 50, 52, 48, 55, 54, 57, 47, 84, 89, 80, 69, 61, 80,
				76, 77, 78, 0, -118, -128, -114, 2, -126, 17 };
		printHexString(b);
		System.out.println(" ");
		ByteArrayInputStream pduDataStream = new ByteArrayInputStream(b);
		int temp = pduDataStream.read();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		while((-1 != temp) && ('\0' != temp)){       //'\0' != temp,读取到0时停止
			out.write(temp);
			temp = pduDataStream.read();
		}
		byte[] arr = out.toByteArray();
		for(int i = 0; i < arr.length; i ++){
			System.out.print(arr[i] + " ");
		}
	}
          
	public static void printHexString(byte[] b) {         //将byte转为16进制
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			System.out.print(hex.toUpperCase());
		}
		System.out.println("");
	}
}


输出log如下:

8C82984D4C56626871665044595541008D9083687474703A2F2F3231312E3133362E3131322E38342F4D4C56626871665044595541008805810303F46389178031333635393234303736392F545950453D504C4D4E008A808E028211             //转码后的十六进制输出
 
-116 -126 -104 77 76 86 98 104 113 102 80 68 89 85 65            //转为输出流



版权声明:本文为tianshi4851原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。