设为首页 加入收藏

TOP

Java位操作
2014-10-27 12:30:08 】 浏览:317
Tags:Java 操作

  public class ByteConvert {


  public static byte[] stringToByte(String inputString) {


  int strLen = inputString.length();


  char[] charResult = new char[strLen];


  byte[] byteResult = new byte[strLen];


  for (int i = 0; i < strLen; i++) {


  charResult[i] = inputString.charAt(i);


  byteResult[i] = (byte) charResult[i];


  }


  return byteResult;


  }


  /**


  * 将int型转为byte[],高位byte存储高位


  * @param int inputNum


  * @return byte[]


  */


  public static byte[] intToByte(int inputNum) {


  int count = 0;


  if (inputNum < (Math.pow(2, 8)))


  count = 1;


  else if (inputNum < (Math.pow(2, 16)))


  count = 2;


  else if (inputNum < (Math.pow(2, 24)))


  count = 3;


  else if (inputNum < (Math.pow(2, 32)))


  count = 4;


  byte[] byteResult = new byte[count];


  for (int i = 0; i < count; i++) {


  byteResult[i] = (byte) ((inputNum >> (8 * i)) & 0xff);


  }


  return byteResult;


  }


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Javasocket应用的几个小实例 下一篇spring和jmx

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目