网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  java与其他语言数据类型之间的转换方法     
  文章作者:未知  文章来源:赛迪网技术社区  
  查看:64次  录入:管理员--2007-11-16  
 

/**
* 通信格式转换
*
* 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<length; i++) {
    result.append((char)(b & 0xff));
  }
  return result.tostring();
} 

/**
  * 将字符串转换为byte数组
  * @param s string
  * @return byte[]
  */
public static byte[] stringtobytes(string s) {
  return s.getbytes();
} 

/**
  * 将高字节数组转换为int
  * @param b byte[]
  * @return int
  */
public static int hbytestoint(byte[] b) {
  int s = 0;
  for (int i = 0; i < 3; i++) {
    if (b >= 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<length; i++) {
    result[length-i-1] = b;
  }
  return result;
} 

/**
  * 打印byte数组
  */
public static void printbytes(byte[] bb) {
  int length = bb.length;
  for (int i=0; i<length; i++) {
    system.out.print(bb + " ");
  }
  system.out.println("");
} 

public static void logbytes(byte[] bb) {
  int length = bb.length;
  string out = "";
  for (int i=0; i<length; i++) {
    out = out + bb + " ";
  } 

} 

/**
  * 将int类型的值转换为字节序颠倒过来对应的int值
  * @param i int
  * @return int
  */
public static int reverseint(int i) {
  int result = formattransfer.hbytestoint(formattransfer.tolh(i));
  return result;
} 

/**
  * 将short类型的值转换为字节序颠倒过来对应的short值
  * @param s short
  * @return short
  */
public static short reverseshort(short s) {
  short result = formattransfer.hbytestoshort(formattransfer.tolh(s));
  return result;
} 

/**
  * 将float类型的值转换为字节序颠倒过来对应的float值
  * @param f float
  * @return float
  */
public static float reversefloat(float f) {
  float result = formattransfer.hbytestofloat(formattransfer.tolh(f));
  return result;
} 

}

 
 
上一篇: 新手入门:ajax的jsp示例以及相关知识    下一篇: 实例解析:在java语言中对文件操作大全
  相关文档
ibm 发布java字节码分析工具biptk 11-17
实例讲解实例讲解jsp调用sql server的存储过程 02-21
在java软件中如何更好防止内存泄漏(组图) 11-17
jdk安装 11-16
jdbc接口技术 11-17
p2p 介绍 11-17
java代码编程规范 11-17
javaants-1.0(网络蚂蚁java版) 11-17
java2十大经典中文图书 11-16
java高级编程——泛型类型第二部分 11-17
内部类标识符 11-17
fixed 方法 11-16
jsp语法手册 11-17
mini java编译器(二) 11-17
java初学者实践教程20-异常处理 11-17
关于refactoring思考 11-17
三步学会java socket编程 11-16
jndi设计内幕 11-17
数据库连接池java实现小结 11-16
用 java 实现回调例程 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息