java 中如何处理其他语言中的无符号整型数据 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 当然是用更大的基本类型处理 比如long 问题是long 的大小都有点小了对方通过soket发送过来的二进制 我该怎么处理 C++中的DWORD为32位无符号整数, 但在java中,就不能用int类型表示了,得用long,因为java中的int要包含负数,如果把C++中的DWORD直接转为java的int,超过Integer.MAX_VALUE的值就会变成负数从网上找来的代码,地址忘了,再贴上来吧 public static long getAs4Byte(final ByteBuffer buf, ByteOrder byteOrder) { byte[] bytes = new byte[4]; buf.get(bytes); StringBuilder builder = new StringBuilder(); String tmp = ""; if (ByteOrder.BIG_ENDIAN.equals(byteOrder)) { for (int i = 0; i < bytes.length; i++) { // java中byte的最大值是127,读取到的byte可能大于127,经过转换会变成负数. tmp = Integer.toHexString(bytes[i] & 0xFF);// 所以在这里处理一下. builder.append(tmp.length()==1?"0"+tmp:tmp); } } else { for (int i = bytes.length - 1; i >= 0; i--) { builder.append(Integer.toHexString(bytes[i] & 0xFF)); } } long value = new BigInteger(builder.toString(), 16).longValue(); return value; } 下面是我的实现方法,可以做互转,java中的long的最大值是不能显示无符号最大值的,所以我输出了string类型。package Comand.Types;import java.math.BigInteger;/** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2009</p> * * <p>Company: </p> * * @author not attributable * @version 1.0 */public class Uint { public static BigInteger MaxValue; public static BigInteger MinValue = new BigInteger("0"); public BigInteger ThisValue = null; BigInteger MinInt = new BigInteger(Integer.toString(Integer.MIN_VALUE)); public Uint(String Value) { ThisValue = new BigInteger(Value); } public Uint(byte[] Value) { int NumInt = bytetoshort(Value); String NumString = Integer.toString(NumInt); ThisValue = new BigInteger(NumString); if (MinInt.compareTo(new BigInteger("0")) < 0) { MinInt= new BigInteger("4294967296"); ThisValue = ThisValue.add(MinInt); } } public byte[] GetBytes() { MaxValue = new BigInteger("4294967296"); if (ThisValue.compareTo(new BigInteger(Integer.toString(Integer.MAX_VALUE))) > 0) { MaxValue = MaxValue.divide(new BigInteger("-1")); ThisValue = ThisValue.add(MaxValue); } String TempString = getstring(); int b = Integer.parseInt(TempString); return shorttobytes(b); } public String getstring() { return ThisValue.toString(); } private byte[] shorttobytes(int n) { byte[] intBytes = new byte[4]; intBytes[0] = (byte) (n >> 24); intBytes[1] = (byte) (n >> 16); intBytes[2] = (byte) (n >> 8); intBytes[3] = (byte) (n >> 0); intBytes = Reverse(intBytes); return intBytes; } private byte[] Reverse(byte[] b) { byte[] temp = new byte[b.length]; for (int i = 0; i < b.length; i++) { temp[i] = b[b.length - 1 - i]; } return temp; } public int bytetoshort(byte b[]) { b = Reverse(b); int mask = 0xff; int temp = 0; int n = 0; for (int i = 0; i < 4; i++) { n <<= 8; temp = b[i] & mask; n |= temp; } return n; }} 有关JAVA 3D的入门的请教 谁能推荐几本学习swing、awt或swt的书 一些基础问题不懂,有时间帮忙解决一下 applet到底是靠jre还是jvm装载的 请各位推荐一款手机 初学java,问个小问题:这个TestA.java有什么错? 有关实体BEAN的问题!! 如何和浏览器产生一个socket通讯???我还是没有解决 希望大家再帮帮忙 生活没有滋味!·#¥%……—* 紧急!为什么我的applet不能够连接oracle数据库。我用的JDBC,提示找不到JDBC类。应该如何配置服务器啊 新手 咨询 判断字符串长度 递归的问题!
对方通过soket发送过来的二进制 我该怎么处理
从网上找来的代码,地址忘了,再贴上来吧
public static long getAs4Byte(final ByteBuffer buf, ByteOrder byteOrder) {
byte[] bytes = new byte[4];
buf.get(bytes);
StringBuilder builder = new StringBuilder();
String tmp = "";
if (ByteOrder.BIG_ENDIAN.equals(byteOrder)) {
for (int i = 0; i < bytes.length; i++) {
// java中byte的最大值是127,读取到的byte可能大于127,经过转换会变成负数.
tmp = Integer.toHexString(bytes[i] & 0xFF);// 所以在这里处理一下.
builder.append(tmp.length()==1?"0"+tmp:tmp);
}
} else {
for (int i = bytes.length - 1; i >= 0; i--) {
builder.append(Integer.toHexString(bytes[i] & 0xFF));
}
}
long value = new BigInteger(builder.toString(), 16).longValue();
return value;
}
package Comand.Types;import java.math.BigInteger;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Uint { public static BigInteger MaxValue;
public static BigInteger MinValue = new BigInteger("0");
public BigInteger ThisValue = null;
BigInteger MinInt = new BigInteger(Integer.toString(Integer.MIN_VALUE)); public Uint(String Value) {
ThisValue = new BigInteger(Value);
} public Uint(byte[] Value) {
int NumInt = bytetoshort(Value);
String NumString = Integer.toString(NumInt);
ThisValue = new BigInteger(NumString); if (MinInt.compareTo(new BigInteger("0")) < 0) {
MinInt= new BigInteger("4294967296");
ThisValue = ThisValue.add(MinInt);
}
} public byte[] GetBytes() { MaxValue = new BigInteger("4294967296"); if (ThisValue.compareTo(new BigInteger(Integer.toString(Integer.MAX_VALUE))) > 0) {
MaxValue = MaxValue.divide(new BigInteger("-1"));
ThisValue = ThisValue.add(MaxValue);
} String TempString = getstring();
int b = Integer.parseInt(TempString); return shorttobytes(b);
} public String getstring() {
return ThisValue.toString();
} private byte[] shorttobytes(int n) {
byte[] intBytes = new byte[4];
intBytes[0] = (byte) (n >> 24);
intBytes[1] = (byte) (n >> 16);
intBytes[2] = (byte) (n >> 8);
intBytes[3] = (byte) (n >> 0);
intBytes = Reverse(intBytes);
return intBytes;
} private byte[] Reverse(byte[] b) { byte[] temp = new byte[b.length];
for (int i = 0; i < b.length; i++) {
temp[i] = b[b.length - 1 - i];
}
return temp;
} public int bytetoshort(byte b[]) { b = Reverse(b);
int mask = 0xff;
int temp = 0;
int n = 0;
for (int i = 0; i < 4; i++) {
n <<= 8;
temp = b[i] & mask;
n |= temp;
}
return n;
}
}