如题

解决方案 »

  1.   

    当然是用更大的基本类型处理 比如long
      

  2.   

    问题是long 的大小都有点小了
    对方通过soket发送过来的二进制  我该怎么处理
      

  3.   

    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;
    }
      

  4.   

    下面是我的实现方法,可以做互转,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;
        }
    }