zoukankan      html  css  js  c++  java
  • Java各个类型与byte[]的转换

    short 转byte[]

     int temp = val;
      byte[] bt=new byte[2];
      for (int i = 0; i < 2; i++)
      {
         bt[i]=new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
         temp = temp >> 8; // 向右移8位
      }

    int 转byte[]

      int temp = v;
      byte[] bt = new byte[4];
      for (int i = 0; i < bt.length; i++)
      {
       bt[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
       temp = temp >> 8; // 向右移8位
      }

    long 转byte[]

      long temp = v;
      byte writeBuffer[] = new byte[8];
      for (int i = 0; i < writeBuffer.length; i++)
      {
       writeBuffer[i] = new Long(temp & 0xff).byteValue();   // 将最低位保存在最低位
       temp = temp >> 8;   // 向右移8位
      }

    String 转byte[]

    String s=new String("测试");

    byte[] wrbyte = s.getBytes();

     byte[]转short

     int reval1 = 0;
      int reval2 = 0;
      int reval3 = 0;
      int reval4 = 0;
      try
      {
         reval1 = stream.read();
         reval2 = stream.read();
         reval3 = stream.read();
         reval4 = stream.read();
      } catch (IOException e)
      {
       // TODO Auto-generated catch block
         e.printStackTrace();
      }

    int returnval=(reval1) | (reval2 << 8) ;

    byte[]转int

      int reval1 = 0;
      int reval2 = 0;
      int reval3 = 0;
      int reval4 = 0;
      try
      {
         reval1 = stream.read();
         reval2 = stream.read();
         reval3 = stream.read();
         reval4 = stream.read();
      } catch (IOException e)
      {
       // TODO Auto-generated catch block
         e.printStackTrace();
      }

    int returnval=(reval1) | (reval2 << 8) | (reval3 << 16) |(reval4 << 24);
     byte[]转long

      int reval1 = 0;
      int reval2 = 0;
      int reval3 = 0;
      int reval4 = 0;
      try
      {
         reval1 = stream.read();
         reval2 = stream.read();
         reval3 = stream.read();
         reval4 = stream.read();
      } catch (IOException e)
      {
       // TODO Auto-generated catch block
         e.printStackTrace();
      }

    int returnval=(reval1) | (reval2 << 8) | (reval3 << 16) |(reval4 << 24)|(reval4 << 32)|(reval4 << 48)|(reval4 << 64)|(reval4 << 72);;

    byte[]转String

  • 相关阅读:
    js如何实现上拉加载更多...
    基于epoll的TP传输层实现
    ActiveMQ简介与安装
    序列化和反序列化实现
    NoSQL之Redis数据库初探
    分布式缓存之memcached--命令测试
    Java正则表达式测试用例
    Java之正则表达式
    Java反射机制练习(增强可扩展性)
    Java反射机制(获取Class对象的三种方式+获取Class中的构造函数进行对象的初始化+获取反射类的字段+获取反射类的一般方法)
  • 原文地址:https://www.cnblogs.com/wuqihui/p/2578025.html
Copyright © 2011-2022 走看看