byte[] 转换为int 结果为0

原byte[]:[0, 0, 0, 0, 0, -102, 29, 79]
byte[]转int方法一:0
byte[]转int方法二:0
byte[]转16进制文本:00000000009a1d4f


解决方法:byte[]转16进制文本后再转int:10100047
 

package com.koow.kkwwo.bytes.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

import org.apache.log4j.Logger;

/**
 * 和 byte[]的相互转换
 * 
 * @author Koow
 * 
 */
public class ToButeArray {

	private static Logger logger = Logger.getLogger(ToButeArray.class);

	
	/**
	 * 16进制字符串转Int
	 * @param n
	 * @return
	 */
	public static Integer String16ToInt(String n) {
		return Integer.valueOf(n, 16);
	}
	
	/**
	 * Int转16进制字符串
	 * @param n
	 * @return
	 */
	public static String IntToString16(Integer n) {
		String st = Integer.toHexString(n).toUpperCase();
		st = String.format("%5s", st);
		st = st.replaceAll(" ", "0");
		return st;
	}

	/**
	 * 整数转换成字节数组 关键技术:ByteArrayOutputStream和DataOutputStream
	 * 
	 * @param n
	 *            需要转换整数
	 * @return
	 */
	public static byte[] intToButeArray(int n) {
		byte[] byteArray = null;
		try {
			ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
			DataOutputStream dataOut = new DataOutputStream(byteOut);
			dataOut.writeInt(n);
			byteArray = byteOut.toByteArray();
		} catch (IOException e) {
			logger.error("IntToButeArray类_intToButeArray方法异常", e);
		}
		return byteArray;
	}

	/**
	 * long转换成字节数组 
	 * 
	 * @param n
	 *            需要转换long
	 * @return
	 */
	public static byte[] longToButeArray(long n) {
		byte[] byteArray = null;
		try {
			ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
			DataOutputStream dataOut = new DataOutputStream(byteOut);
			dataOut.writeLong(n);
			byteArray = byteOut.toByteArray();
		} catch (IOException e) {
			logger.error("IntToButeArray类_longToButeArray方法异常", e);
		}
		return byteArray;
	}

	/**
	 * 字节数组转换成整数
	 * 
	 * @param byteArray
	 *            需要转换的字节数组
	 * @return
	 */
	public static int byteArrayToInt(byte[] byteArray) {
		int n = 0;
		try {
			ByteArrayInputStream byteInput = new ByteArrayInputStream(byteArray);
			DataInputStream dataInput = new DataInputStream(byteInput);
			n = dataInput.readInt();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error("IntToButeArray类_byteArrayToInt方法异常", e);
		}
		return n;
	}

	/**
	 * 将指定byte数组以16进制的形式打印到控制台
	 * 
	 * @param b
	 */
	public static void printHexString(byte[] b) {
		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());
		}

	}

	/**
	 * Convert 字符集转16进制文本(字符串) byte[] to hex
	 * string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。*
	 * 
	 * @param src
	 *            byte[] data*@return hex string
	 */

	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	/**
	 * Convert hex string to byte[] 16进制文本(字符串)转字符集 当字符串为1的时候,会出现返回为空
	 * 推荐使用HexUtil 工具类
	 * 
	 * @param hexString
	 *            the hex string
	 * @return byte[]
	 */
	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	/**
	 * 合并byte数组
	 */
	public static byte[] unitByteArray(byte[] byte1, byte[] byte2) {
		byte[] unitByte = new byte[byte1.length + byte2.length];
		System.arraycopy(byte1, 0, unitByte, 0, byte1.length);
		System.arraycopy(byte2, 0, unitByte, byte1.length, byte2.length);
		return unitByte;
	}

	/**
	 * 截取byte数组
	 * 
	 * @param byte1
	 * @param a
	 *            从a位置开始截取,0为开始
	 * @param b
	 *            截取多少位
	 * @return
	 */
	public static byte[] subByteArray(byte[] byte1, Integer a, Integer b) {
		System.out.println(b - a);
		byte[] unitByte = new byte[b];
		System.out.println(a);
		System.out.println(b);
		System.out.println(Arrays.toString(byte1));
		System.arraycopy(byte1, a, unitByte, 0, b);
		return unitByte;
	}

	/**
	 * 判断byte是否小于零 将byte=-109转换为147
	 * 
	 * @param b
	 * @return
	 */
	public static int byteToPositive(byte b) {

		if (b < 0) {
			return b + 256;
		} else {
			return b;
		}
	}

	/**
	 * 使用 0xFF 与 将byte=-109转换为147
	 * 
	 * @param b
	 * @return
	 */
	public static int byteToPositive2(byte b) {
		return b & 0xFF;
	}

	/**
	 * Convert char to byte
	 * 
	 * @param c
	 *            char
	 * @return byte
	 */
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	public static byte[] GetRandomBin(Integer len) {
		byte[] b = new byte[len];
		Random random = new Random();
		random.nextBytes(b);
		return b;
	}

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值