java basic type codec

Java basic type codec
--------------------------------------------- ------
1. There is no encoding and decoding problem with single-byte encoding

2. Multi-byte editing code
  1) big-endian low address storage high position
  2) little-endian low address storage position
  3) for different methods The corresponding data can be obtained by corresponding decoding. If the corresponding decoding data cannot be obtained according to the opposite method,
3. The editing code of the numerical value is nothing but the left and right movement of the numerical value to obtain the corresponding byte, put it in the corresponding position and perform the | operation

4. According to the tcp/ip socket programming content, edit the long type code





package com.pjf.echodemo;

import java.io.IOException;
import java.math.BigDecimal;

public class EncodeDecodeDemo {

	public static void main(String[] args) throws Exception {

		long data = 123456787654321l;

		//display with sixteen
		System.out.println(Long.toHexString(data));
		
		//display in binary
		System.out.println(Long.toBinaryString(data));
		
		//display in decimal
		System.out.println(printByteDecByLong(data, 1));
		System.out.println(printByteDecByLong(data, 0));
		
		
		System.out.println("==============High and low bits========================");
		//Convert a long into a high and low array
		byte[] bytes = longToByteArrayWithBigEndian(data);
		// Parse the data in high and low bits
		System.out.println(readUnsignedLong(byteArrayToLongWithBigEndian(bytes)));
		System.out.println("==============High and low bits========================");
		
		
		System.out.println("==============Low and high bits========================");
		//Convert a long into a high-order array
		byte[] littleBytes = longToByteArrayWithLittleEndian(data);
		System.out.println(byteArrayToLongWithLittleEndian(littleBytes));
		System.out.println("==============Low and high bits========================");

		

		
//		System.out.println(byteArrayToLongWithBigEndian(bytes));
//		System.out.println(byteArrayToLongWithLittleEndian(bytes));

		
 
				

	}



	/***
	 * Display a byte array as decimal data
	 */

	/***
	 *
	 * @param data
	 * Data to be processed
	 * @param type
	 * 0: Print in little-endian mode, other modes are in big-endian mode
	 * @return
	 */
	public static String printByteDecByLong(long data, int type) {
		byte[] bytes = null;
		if (type == 0) {
			bytes = longToByteArrayWithLittleEndian(data);
		} else {
			bytes = longToByteArrayWithBigEndian(data);

		}

		StringBuilder sb = new StringBuilder();
		for (byte b : bytes) {
			sb.append(b & 0xff).append(" ");
		}

		return sb.toString();
	}

	/**
	 * Convert long into a byte array [byte order is big endian] high bit is on low address bit
	 */
	public static byte[] longToByteArrayWithBigEndian(long a) {
		return new byte[] { (byte) ((a >> 56) & 0xFF), (byte) ((a >> 48) & 0xFF), (byte) ((a >> 40) & 0xFF),
				(byte) ((a >> 32) & 0xFF), (byte) ((a >> 24) & 0xFF), (byte) ((a >> 16) & 0xFF),
				(byte) ((a >> 8) & 0xFF), (byte) (a & 0xFF) };

	}

	/**
	 * Convert long into a byte array [byte order is little endian] high bit is on low address bit
	 */
	public static byte[] longToByteArrayWithLittleEndian(long a) {

		return new byte[] { (byte) (a & 0xFF), (byte) ((a >> 8) & 0xFF), (byte) ((a >> 16) & 0xFF),
				(byte) ((a >> 24) & 0xFF), (byte) ((a >> 32) & 0xFF), (byte) ((a >> 40) & 0xFF),
				(byte) ((a >> 48) & 0xFF), (byte) ((a >> 56) & 0xFF) };
	}
	
	

	/**
	 *
	 * @param bytes
	 * number of bytes
	 * @return
	 */
	public static long byteArrayToLongWithBigEndian(byte[] bytes) {
		// return b[3] & 0xFF |
		// (b[2] & 0xFF) << 8 |
		// (b[1] & 0xFF) << 16 |
		// b[0] & 0xFF) << 24;

		return (bytes[0] & (long) 0xFF) << 56 | (bytes[1] & (long) 0xFF) << 48 | (bytes[2] & (long) 0xFF) << 40
				| (bytes[3] & (long) 0xFF) << 32 | (bytes[4] & (long) 0xFF) << 24 | (bytes[5] & (long) 0xFF) << 16
				| (bytes[6] & (long) 0xFF) << 8 | (bytes[7] & (long) 0xFF) << 0;
		// long data = 0;
		// int cnt = 8 - bytes.length;
		// /** Not enough bits to fill in */
		// int i=0;
		// for ( i = 0; i < cnt; i++) {
		//
		// data = data | (0 & 0xFF) <<( 8 * (8-i));
		// }
		// /***
		// * bytes are big-endian converted
		// */
		// for(int j=0;j<=bytes.length-1;j++){
		//
		// data = data | ((bytes[j] & 0xFF) << (8 * (8-i-j-1)));
		//
		// }

		// return data;
	}
	/***
	 *
	 * @param bytes byte data little-endian
	 * @return
	 */
	public static long byteArrayToLongWithLittleEndian(byte[] bytes) {
 		return (bytes[0] & (long) 0xFF) << 0 | (bytes[1] & (long) 0xFF) << 8 | (bytes[2] & (long) 0xFF) << 16
				| (bytes[3] & (long) 0xFF) << 24 | (bytes[4] & (long) 0xFF) << 32 | (bytes[5] & (long) 0xFF) << 40
				| (bytes[6] & (long) 0xFF) << 48 | (bytes[7] & (long) 0xFF) << 56;
	}

	 
	
	/**
	 *
	 * @param value signed long data
	 * @return converts signed long data to unsigned data
	 * @throws IOException
	 */
	public static final BigDecimal readUnsignedLong(long value) throws IOException {
		  if (value >= 0)
		   return new BigDecimal(value);
		  long lowValue = value & 0x7fffffffffffffffL;
		  return BigDecimal.valueOf(lowValue).add(BigDecimal.valueOf(Long.MAX_VALUE)).add(BigDecimal.valueOf(1));
		 }
	
	
}












5. Running results



6.java numeric type conversion rules
If the initial numeric type is signed, sign extension is performed; if it is a char type, then no matter what type it is to be converted to, zero extension is performed. There is another rule to keep in mind, if the length of the target type is less than the length of the source type, then directly intercept the length of the target type. For example, converting an int type to a byte type directly intercepts the right 8 bits of the int type.



The above is only for learning records!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326399750&siteId=291194637