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

  • 相关阅读:
    网站运维之 优化
    网站运维之 风险防控
    网站运维之 使用IIS日志分析器1.03.exe进行IIS服务器日志分析
    MySQL数据库优化
    深入理解Java GC
    深入理解React虚拟DOM
    深入理解new String()
    深入理解JVM内存模型
    MySQL的四种事务隔离级别
    Node.js Stream(流)
  • 原文地址:https://www.cnblogs.com/wuqihui/p/2578025.html
Copyright © 2011-2022 走看看