关于易语言中 到无符号长整型(long) java中实现

在写java qq安卓协议的时候,遇到很多麻烦,其中一个就是以下汇编语言

没时间去看具体意思,只能摸索,如下代码

 

.版本 2

.子程序 到无符号长整型, 长整数型
.参数 long, 整数型

置入代码 ({ 139, 69, 8, 51, 210, 201, 194, 4, 0 })

返回 (0)
 

 

//* “长整数形:-2047848776”
//    * “转换后:2247118520”

 

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.math.BigInteger;
import java.util.Arrays;
import java.util.Random;

import org.apache.log4j.Logger;

import com.koow.kkwwo.c.Pack;
import com.koow.kkwwo.hex.util.HexUtil;

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

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

	/**
	 * 16进制字符串转Int Integer.parseInteger的时候,会将传入十六进制数字一致认为成正数,
	 * 所以这时候表示负数的十六进制字符串所表示的数字就超出了Integer所能表示的数字范围,会报出NumberFormatException
	 * 转换为BigInteger再转换Int
	 * 
	 * @param n
	 * @return
	 */
	public static Integer String16ToInt(String n) {
		n=Pack.c_Null(n);
		return Integer.valueOf(n, 16);
	}

	/**
	 * 16进制字符串转BigInteger
	 * 
	 * @param n
	 * @return
	 */
	public static BigInteger String16ToBigInteger(String n) {
		n=Pack.c_Null(n);
		return new BigInteger(n, 16);
	}

	/**
	 * Int转16进制字符串
	 * 
	 * @param n
	 * @return
	 */
	public static String LongToString16(long n) {
		String st = Long.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) {
		hexString=Pack.c_Null(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) {
		byte[] unitByte = new byte[b];
		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;
	}
	
	
	/**
	 *  返回b字节集在a字节集中最先出现的位置,查询位置值从 0 开始。如果未找到,返回 -1
	 * @param a
	 * @param b
	 * @param c
	 *  起始位置
	 * @return
	 *  0开始的起始位置
	 */
	public static Integer getByteIndexOf(byte[] a,byte[] b,Integer c){
		String aa=HexUtil.bytesToHex(a);
		String bb=HexUtil.bytesToHex(b);
		
		aa=aa.substring(c*2, aa.length());
		return aa.indexOf(bb)==-1?-1:aa.indexOf(bb, c)/2;
	}
	
	/**
	 * 返回一个字节集,其中包含指定字节集中从右边算起指定数量的字节。
	 * @param a
	 *      字节集
	 * @param b
	 *      指定数量
	 * @return
	 */
	public static byte[] getByteRight(byte[] a,Integer b){
		String aa=HexUtil.bytesToHex(a);
		
		aa=aa.substring(aa.length()-b*2, aa.length());
		return HexUtil.hexToByteArray(aa);
	}
	
	public static void main(String[] args){
		//byte[] a=new byte[]{1,-100,3,-100,101,6};
		//byte[] b=new byte[]{-100,101,6};
		//System.out.println(getByteIndexOf(a, b, 0));
		
		
		byte[] a=new byte[]{1,-100,3,-100,101,6};
		System.out.println(Arrays.toString(a));
	}

}

 

 

package com.koow.kkwwo.hex.util;

import com.koow.kkwwo.c.Pack;

public class HexUtil {

	/**
	 * 字节转十六进制
	 * 
	 * @param b
	 *            需要进行转换的byte字节
	 * @return 转换后的Hex字符串
	 */
	public static String byteToHex(byte b) {
		String hex = Integer.toHexString(b & 0xFF);
		if (hex.length() < 2) {
			hex = "0" + hex;
		}
		return hex;

	}

	/**
	 * 字节数组转16进制
	 * 
	 * @param bytes
	 *            需要转换的byte数组
	 * @return 转换后的Hex字符串
	 */
	public static String bytesToHex(byte[] bytes) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			String hex = Integer.toHexString(bytes[i] & 0xFF);
			if (hex.length() < 2) {
				sb.append(0);
			}
			sb.append(hex);
		}
		return sb.toString();

	}
	
	/**
	 * 字节数组转16进制(带空格大写)
	 * 
	 * @param bytes
	 *            需要转换的byte数组
	 * @return 转换后的Hex字符串
	 */
	public static String bytesToHex2(byte[] bytes) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			String hex = Integer.toHexString(bytes[i] & 0xFF);
			if (hex.length() < 2) {
				sb.append(0);
			}
			sb.append(hex.toUpperCase()+" ");
		}
		return sb.toString().trim();

	}

	/**
	 * Hex字符串(16进制)转byte
	 * 
	 * @param inHex
	 *            待转换的Hex字符串
	 * @return 转换后的byte
	 */
	public static byte hexToByte(String inHex) {
		return (byte) Integer.parseInt(inHex, 16);

	}

	/**
	 * hex字符串(16进制)转byte数组
	 * 
	 * @param inHex
	 *            待转换的Hex字符串
	 * @return 转换后的byte数组结果
	 */
	public static byte[] hexToByteArray(String inHex) {
		inHex=Pack.c_Null(inHex);
		int hexlen = inHex.length();
		byte[] result;
		if (hexlen % 2 == 1) {
			// 奇数
			hexlen++;
			result = new byte[(hexlen / 2)];
			inHex = "0" + inHex;
		} else {
			// 偶数
			result = new byte[(hexlen / 2)];
		}
		int j = 0;
		for (int i = 0; i < hexlen; i += 2) {
			result[j] = hexToByte(inHex.substring(i, i + 2));
			j++;
		}
		return result;
	}

}

 

 

package com.koow.kkwwo.c;

import java.util.Arrays;


import com.koow.kkwwo.bytes.util.ToButeArray;
import com.koow.kkwwo.hex.util.HexUtil;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		// QQ安卓协议登陆
		Login l=new Login();
		l.Fun_Login("763614984", "");
             
	
		
		byte[] ss=ToButeArray.longToButeArray(-2047848776);
		System.out.println("转换为byte[]:"+Arrays.toString(ss));
		
		byte[] sa=ToButeArray.subByteArray(ss, ss.length/2, ss.length/2);
		
		System.out.println("byte[]截取后:"+Arrays.toString(sa));
		System.out.println("byte[]转换16位字符串再转换为BigInteger:"+ToButeArray.String16ToBigInteger(HexUtil.bytesToHex(sa)));
		System.out.println("易语言__________________到无符号长整型:2247118520");
	}

	
}

 

 

以下为控制台输出

原byte[]:[-1, -1, -1, -1, -123, -16, 78, -72]

byte[]截取后:[-123, -16, 78, -72]
byte[]转换16位字符串再转换为BigInteger:2247118520
易语言__________________到无符号长整型:2247118520

你可能感兴趣的:(qq安卓协议,备忘,java)