博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java字节序
阅读量:7022 次
发布时间:2019-06-28

本文共 4078 字,大约阅读时间需要 13 分钟。

  hot3.png

 
/*** 通信格式转换** Java和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时,需要进行相应的转换* 高、低字节之间的转换* windows的字节序为低字节开头* linux,unix的字节序为高字节开头* java则无论平台变化,都是高字节开头  */ public class FormatTransfer {/**  * 将int转为低字节在前,高字节在后的byte数组  * @param n int  * @return byte[]  */public static byte[] toLH(int n) {  byte[] b = new byte[4];  b[0] = (byte) (n & 0xff);  b[1] = (byte) (n >> 8 & 0xff);  b[2] = (byte) (n >> 16 & 0xff);  b[3] = (byte) (n >> 24 & 0xff);  return b;} /**  * 将int转为高字节在前,低字节在后的byte数组  * @param n int  * @return byte[]  */public static byte[] toHH(int n) {  byte[] b = new byte[4];  b[3] = (byte) (n & 0xff);  b[2] = (byte) (n >> 8 & 0xff);  b[1] = (byte) (n >> 16 & 0xff);  b[0] = (byte) (n >> 24 & 0xff);  return b;} /**  * 将short转为低字节在前,高字节在后的byte数组  * @param n short  * @return byte[]  */public static byte[] toLH(short n) {  byte[] b = new byte[2];  b[0] = (byte) (n & 0xff);  b[1] = (byte) (n >> 8 & 0xff);  return b;} /**  * 将short转为高字节在前,低字节在后的byte数组  * @param n short  * @return byte[]  */public static byte[] toHH(short n) {  byte[] b = new byte[2];  b[1] = (byte) (n & 0xff);  b[0] = (byte) (n >> 8 & 0xff);  return b;} /**  * 将将int转为高字节在前,低字节在后的byte数组 public static byte[] toHH(int number) {  int temp = number;  byte[] b = new byte[4];  for (int i = b.length - 1; i > -1; i--) {    b = new Integer(temp & 0xff).byteValue();    temp = temp >> 8;  }  return b;} public static byte[] IntToByteArray(int i) {    byte[] abyte0 = new byte[4];    abyte0[3] = (byte) (0xff & i);    abyte0[2] = (byte) ((0xff00 & i) >> 8);    abyte0[1] = (byte) ((0xff0000 & i) >> 16);    abyte0[0] = (byte) ((0xff000000 & i) >> 24);    return abyte0;} */ /**  * 将float转为低字节在前,高字节在后的byte数组  */public static byte[] toLH(float f) {  return toLH(Float.floatToRawIntBits(f));} /**  * 将float转为高字节在前,低字节在后的byte数组  */public static byte[] toHH(float f) {  return toHH(Float.floatToRawIntBits(f));} /**  * 将String转为byte数组  */public static byte[] stringToBytes(String s, int length) {  while (s.getBytes().length < length) {    s += " ";  }  return s.getBytes();} /**  * 将字节数组转换为String  * @param b byte[]  * @return String  */public static String bytesToString(byte[] b) {  StringBuffer result = new StringBuffer("");  int length = b.length;  for (int i=0; i
= 0) { s = s + b; } else { s = s + 256 + b; } s = s * 256; } if (b[3] >= 0) { s = s + b[3]; } else { s = s + 256 + b[3]; } return s;} /** * 将低字节数组转换为int * @param b byte[] * @return int */public static int lBytesToInt(byte[] b) { int s = 0; for (int i = 0; i < 3; i++) { if (b[3-i] >= 0) { s = s + b[3-i]; } else { s = s + 256 + b[3-i]; } s = s * 256; } if (b[0] >= 0) { s = s + b[0]; } else { s = s + 256 + b[0]; } return s;} /** * 高字节数组到short的转换 * @param b byte[] * @return short */public static short hBytesToShort(byte[] b) { int s = 0; if (b[0] >= 0) { s = s + b[0]; } else { s = s + 256 + b[0]; } s = s * 256; if (b[1] >= 0) { s = s + b[1]; } else { s = s + 256 + b[1]; } short result = (short)s; return result;} /** * 低字节数组到short的转换 * @param b byte[] * @return short */public static short lBytesToShort(byte[] b) { int s = 0; if (b[1] >= 0) { s = s + b[1]; } else { s = s + 256 + b[1]; } s = s * 256; if (b[0] >= 0) { s = s + b[0]; } else { s = s + 256 + b[0]; } short result = (short)s; return result;} /** * 高字节数组转换为float * @param b byte[] * @return float */public static float hBytesToFloat(byte[] b) { int i = 0; Float F = new Float(0.0); i = ((((b[0]&0xff)<<8 | (b[1]&0xff))<<8) | (b[2]&0xff))<<8 | (b[3]&0xff); return F.intBitsToFloat(i);} /** * 低字节数组转换为float * @param b byte[] * @return float */public static float lBytesToFloat(byte[] b) { int i = 0; Float F = new Float(0.0); i = ((((b[3]&0xff)<<8 | (b[2]&0xff))<<8) | (b[1]&0xff))<<8 | (b[0]&0xff); return F.intBitsToFloat(i);} /** * 将byte数组中的元素倒序排列 */public static byte[] bytesReverseOrder(byte[] b) { int length = b.length; byte[] result = new byte[length]; for(int i=0; i
参考: [1]. 

转载于:https://my.oschina.net/itfanr/blog/195687

你可能感兴趣的文章
JAVA学习笔记1
查看>>
2013-1-17 打开/关闭默认共享的命令
查看>>
oracle数据库中函数的递归调用
查看>>
pkgmgmt: Comparison between different Linux Systems..
查看>>
python--gevent协程及协程概念
查看>>
Java 打包成exe安装包
查看>>
EF执行出错~NotSupportedException
查看>>
A.出题人的RP值
查看>>
jQuery中$().each与$.each的区别
查看>>
大数据开发从入门小白到删库跑路(一)- 获取Hadoop
查看>>
ES6新特性概览
查看>>
[转] React Hot Loader 3 beta 升级指南
查看>>
slice,substr和substring的区别
查看>>
迭代器、生成器、面向过程编程
查看>>
使用async实现异步控制
查看>>
第一次实训作业
查看>>
Hash
查看>>
nginx 配置手机端PC端访问不同的项目
查看>>
SpriteKit-(SKNode)
查看>>
并发编程可能存在的问题
查看>>