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

  • 相关阅读:
    局部 与 整体 修正 逐渐逼近
    en-zh(社会问题)social problems
    单调性 [1 + 1 / (n)]^n
    en-zh(科学技术)science and technology
    mysql函数之截取字符串
    看数据库的文件大小 MySQL Binlog日志的生成和清理规则
    Brouwer不动点
    布尔巴基学派
    量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html
    COMSOL
  • 原文地址:https://www.cnblogs.com/wuqihui/p/2578025.html
Copyright © 2011-2022 走看看