java中实现简单的Int64

转自:http://blog.chinaunix.net/space.php?uid=20585918&do=blog&id=1622868

java中貌似没有支持8个字节的类型(这是我的猜测,因为找不到),但是有时候又要用,呵呵~至少我是,只好自己写了一个假的8字节类型Int64,虽然不是很好,但将就可以用一用吧 ^_^
 
public class Int64
{
    byte[] m_byteData;
    public Int64()
    {
        m_byteData = new byte[8];
    }
    public Int64(String str)
    {
        m_byteData = new byte[8];
        SetValue(str);
    }
    public byte[] Data()
    {
        return m_byteData;
    }
    public void SetValue(String str)
    {
        int len = str.length();
        int count = len / 2;
        String temp = "";
        short num = 0;
        int i = 0;
        for (i = 0; i < count; i ++)
        {
            temp = str.substring(i*2, i*2+1);
            num = (short)Integer.parseInt(temp);
            m_byteData[i] = (byte)(num<<4);
            temp = str.substring(i*2+1, i*2+2);
            num = (short)Integer.parseInt(temp);
            m_byteData[i] += (byte)(num);
        }
        if (len % 2 == 1)   // 最后一个数
        {
            temp = str.substring(len-1, len);
            num = (short)Integer.parseInt(temp);
            m_byteData[i] = (byte)(num<<4);
            num = 0x0f;   // 加入结束标志
            m_byteData[i] += (byte)(num);           
        }
        else if (i < 8)   // 加入结束标志
        {
            num = 0xf0;      
            m_byteData[i] = (byte)(num);
        }
    }
   
    public String ToString()
    {
        String str = "";
        short temp;
        for (int i = 0; i < 8; i ++)
        {
            temp = (short)((m_byteData[i] & 0xf0) >> 4);
            if (temp > 9)
            {
                break;
            }
            str += temp;
            temp = (short) (m_byteData[i] & 0x0f);
            if (temp > 9)
            {
                break;
            }
            str += temp;
        }
        return str;       
    }
    public static String VauleOf(Int64 value)
    {
        String str = "";
        short temp;
        for (int i = 0; i < 8; i ++)
        {
            temp = (short)((value.m_byteData[i] & 0xf0) >> 4);
            if (temp > 9)
            {
                break;
            }
            str += temp;
            temp = (short) (value.m_byteData[i] & 0x0f);
            if (temp > 9)
            {
                break;
            }
            str += temp;
        }
        return str;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值