int转byte,byte转16进制

public class Utils
{
    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    public static void main (String[] args){
      byte[] a =   IntToByteArray(185);
      byte[] b =   toHH2(1008);
       int c =  toInt(new byte[]{-71,0,0,0});
       int d =  toIntHH(new byte[]{0,0,0,-71});
//        byte[] f = new byte[]{0x03,(byte)0xf0};
        byte[] f = new byte[]{0,0,3,-16};
        int dd =  toIntHH(f);
        System.out.print(f);
         System.out.print(a);
         System.out.print(dd);
        System.out.print(b);
       byte[] byt =  new byte[]{3,-16};
        String str = bytesToHex2(byt);
        printHexString(byt);
        System.out.print(str);


    }

   // int 转 byte[]   低字节在前(低字节序)
    public static  byte[] IntToByteArray(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }

 //int 转 byte[]   高字节在前(高字节序)
    public static byte[] toHH(int n) {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        return b;
    }

    public static byte[] toHH2(int n) {
        byte[] b = new byte[2];
        b[1] = (byte) (n & 0xff);
        b[0] = (byte) (n >> 8 & 0xff);
        return b;
    }

  // byte[] 转 int 低字节在前(低字节序)


    public static int toInt(byte[] b) {
        int res = 0;
        for (int i = 0; i < b.length; i++) {
            res += (b[i] & 0xff) << (i * 8);
        }
        return res;
     }


   // byte[] 转 int 高字节在前(高字节序)
    public static  int toIntHH(byte[] b) {
        int res = 0;
        for (int i = 0; i < b.length; i++) {
            res += (b[i] & 0xff) << ((3 - i) * 8);
        }
        return res;
    }

    public static String bytesToHex(byte[] bytes) {
        // 一个byte为8位,可用两个十六进制位标识
        char[] buf = new char[bytes.length * 2];
        int a = 0;
        int index = 0;
        for (byte b : bytes) { // 使用除与取余进行转换
            if (b < 0) {
                a = 256 + b;
            } else {
                a = b;
            }

            buf[index++] = HEX_CHAR[a / 16];
            buf[index++] = HEX_CHAR[a % 16];
        }
        return new String(buf);
    }

    public static String bytesToHex2(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        sb.append("0x");
        for (byte b : bytes) {
            sb.append(HEX_CHAR[((byte) b & 0xf0) / 16]);
            sb.append(HEX_CHAR[((byte) b & 0x0f)]);
        }
        return sb.toString();
    }

    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() );
        }

    }

    }

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import org.apache.http.util.TextUtils;

public class TCPSocket{
	public static void main(String args[]){
		String cmdInfor="00 00 00 00 00 06 01 01 00 01 00 20";
		send(cmdInfor);
	}
	public static String send(String cmdInfor){ 
		String strReturn = null;
		try {  
			//要连接的服务端IP地址  
			String host = "192.168.1.2"; 
			//要连接的服务端对应的监听端口  
			int port = 502;   
			//将十六进制的字符串转换成字节数组
			byte[] cmdInfor2 = hexStrToBinaryStr(cmdInfor);
			
			//1.建立客户端socket连接,指定服务器位置及端口  
			Socket clientSocket =new Socket(host,port);  
			
			//2.得到socket读写流  
			OutputStream os=clientSocket.getOutputStream();  
			PrintWriter pw=new PrintWriter(os);  
			//输入流  
			InputStream is=clientSocket.getInputStream();  
			
			//3.利用流按照一定的操作,对socket进行读写操作  
			os.write(cmdInfor2);  
			os.flush();  
			clientSocket.shutdownOutput();  
			
			//接收服务器的响应 
            		int line = 0; 
			byte[] buf = new byte[is.available()];
			
			 //接收收到的数据
           		 while((line=is.read(buf))!=-1){
	           	        //将字节数组转换成十六进制的字符串
	            		strReturn= BinaryToHexString(buf);
           		 }
			//4.关闭资源  
			is.close();  
			pw.close();  
			os.close();  
			clientSocket.close();
		}catch (Exception e){
			e.printStackTrace();
		}
		return strReturn;
	}  
	
	/**
	 * 将十六进制的字符串转换成字节数组
	 *
	 * @param hexString
	 * @return
	 */
	public static byte[] hexStrToBinaryStr(String hexString) {
 
		if (TextUtils.isEmpty(hexString)) {
			return null;
		}
 
		hexString = hexString.replaceAll(" ", "");
 
		int len = hexString.length();
		int index = 0;
 
		byte[] bytes = new byte[len / 2];
 
		while (index < len) {
 
			String sub = hexString.substring(index, index + 2);
 
			bytes[index/2] = (byte)Integer.parseInt(sub,16);
 
			index += 2;
		}
 
 
		return bytes;
	}
	
	/**
	 * 将字节数组转换成十六进制的字符串
	 *
	 * @return
	 */
	public static String BinaryToHexString(byte[] bytes) {
	    String hexStr = "0123456789ABCDEF";
	    String result = "";
	    String hex = "";
	    for (byte b : bytes) {
	        hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
	        hex += String.valueOf(hexStr.charAt(b & 0x0F));
	        result += hex + " ";
	    }
	    return result;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值