【java通过移位转16进制】教程文章相关的互联网学习教程文章

java通过移位转16进制【代码】

publicclass Main {publicstaticvoid main(String []args){Main main = new Main();System.out.println("main: "+main.Hex(j));System.out.println("Sys: "+Integer.toHexString(j));}private String Hex(int i){String string="";String temp = Integer.toBinaryString(i);int len = temp.length();System.out.println(temp);System.out.println(len);while(len%4!=0){temp="0"+temp;len=temp.length();}while(len>0){//System.ou...

java 计算两个字节16进制包长【代码】

包头经常用两个字节压缩bcd码来表示 打包,如果338字节,转换成十进制数00x01,0x52 /*** function:2 hex byte[] to int,use pack* @param len* @return 2 hex byte[]*/ public static byte[] convertLen2HexByte(int len) {byte[] lenByte = new byte[2];String hexLen = Integer.toHexString(len);//奇数前补0hexLen = (hexLen.length() & 1) == 1 ? "0" + hexLen : hexLen;lenByte = convertHexStrToByteArray(hexLen);retur...

Java 16进制字符串和字节数组转换的几种方法

1 十六进制字符串转字节数组方法 /** * 16进制表示的字符串转换为字节数组 * * @param hexString 16进制表示的字符串 * @return byte[] 字节数组 */ public static byte[] hexStringToByteArray(String hexString) { hexString = hexString.replaceAll(" ", ""); int len = hexString.length(); byte[] bytes = new byte[len / 2]; for (int i = 0; i < len; i += 2...

Java 16进制字符串 取反

目的:对java 数字字符串进行取反 步骤:现将java数字字符串转化为二进制byte 然后对二进制byte进行取反 在把byte转化为16进制字符串。 直接贴代码: public static void main(String[] args) { String javaStr="10270000"; byte [] bytes = hexStringToByte(javaStr); byte temp; for(int i=0;i<bytes.length;i++){ temp = bytes[i]; bytes[i] = (byte) (~temp); ...

JavaSocket短连接实现分别接收字符串和16进制数据【图】

做个笔记,在接收16进制数据的时候乱码了。原因是Socket在接收数据的时候需要根据不同的数据定义不同的接收方式,也就是约定好传输协议(具体体现在后面服务端接收16进制那里)。 字符串的发送接收 字符串发送:字符串接收:客户端代码: 没什么好说的,复制粘贴导包。public static void main(String[] args){try{Socket socket = new Socket("localhost", 8888);//得到一个输出流,用于向服务器发送数据OutputStream outputStream ...