zoukankan      html  css  js  c++  java
  • 基本类型数据转换(int,char,byte)

    public class DataUtil {
    
        public static void main(String[] args) {
            int a = 8;
            int value = charToInt(byteToChar(intToByte(a)));
            int value2 = byteToInt(charToByte(intTochar(a)));
            System.out.println(value);
            System.out.println(value2);
        }
    
        public static byte[] intToByte(int value){
            byte[] b = new byte[4];
            b[0] = (byte) (value & 0xff);
            b[1] = (byte) ((value >> 8) & 0xff);
            b[2] = (byte) ((value >> 16) & 0xff);
            b[3] = (byte) ((value >> 24) & 0xff);
            return b;
        }
    
        public static int byteToInt(byte[] bytes){
            return  (bytes[0] & 0xff) | ((bytes[1] & 0xff) << 8)
                    | ((bytes[2] & 0xff) << 16) | ((bytes[3] & 0xff) << 24);
        }
    
        public static char[] intTochar(int value){
            char[] c = new char[2];
            c[0] = (char) (value & 0xffff);
            c[1] = (char) ((value >> 16) & 0xffff);
            return c;
        }
    
        public static int charToInt(char[] chars){
            return (chars[0] & 0xffff) | ((chars[1] & 0xffff) << 16);
        }
    
        public static char[] byteToChar(byte[] bytes){
            char[] v = new char[2];
            v[0] = (char) ((bytes[0] & 0xffff) | ((bytes[1] & 0xffff) << 8));
            v[1] = (char) ((bytes[2] & 0xffff) | ((bytes[3] & 0xffff) << 8));
            return v;
        }
    
        public static byte[] charToByte(char[] chars){
            byte[] bytes = new byte[4];
            bytes[0] = (byte) (chars[0] & 0xff);
            bytes[1] = (byte) ((chars[0] >> 8) & 0xff);
            bytes[2] = (byte) (chars[1] & 0xff);
            bytes[3] = (byte) ((chars[1] >> 8) & 0xff);
            return bytes;
        }
    
        public static byte[] objectToBytes(Object object) throws IOException{
            try(ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                ObjectOutputStream outputStream = new ObjectOutputStream(arrayOutputStream)){
                outputStream.writeObject(object);
                outputStream.flush();
                return arrayOutputStream.toByteArray();
            }
        }
    
        public static Object bytesToObject(byte[] bytes) throws IOException,ClassNotFoundException{
            try (ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
                ObjectInputStream objectInputStream = new ObjectInputStream(arrayInputStream)){
                return objectInputStream.readObject();
            }
        }
    }
  • 相关阅读:
    袁创:如何成为黄金程序猿
    划重点!新版电子病历评级标准讲解会上6大核心要点
    台湾医院信息化见闻录
    2500行代码实现高性能数值表达式引擎
    HIT创业感言:只有长寿的企业才有持续价值
    袁创:寂静的战争
    相约南湖,南京都昌信息亮相南湖HIT论坛
    我们是谁?南京都昌信息科技有限公司!
    医疗链的系列谈 第一篇 基本概念研究
    论电子病历控件的现状和发展方向
  • 原文地址:https://www.cnblogs.com/suiyueqiannian/p/6961070.html
Copyright © 2011-2022 走看看