设为首页 加入收藏

TOP

java byte[]、int、long互转
2014-11-23 23:31:54 来源: 作者: 【 】 浏览:2
Tags:java byte int long 互转

public class BinDataConvert {


public static void BinnCat( byte[] from, byte[] to, int offset, int len )
{
int max = offset + len;
int min = offset;
for( int i = min, j = 0; i < max; i++, j++ )
{
to = from[j];
}
}


public static byte[] LongToBin( long from, int len )
{
byte[] to = new byte[len];
int max = len;


for( int i_move = max – 1, i_to = 0; i_move >= 0; i_move–, i_to++ )
{
to[i_to] = (byte)( from >> ( 8 * i_move ) );
}


return to;
}
public static void LongToBin( long from, byte[] to, int offset, int len )
{
int max = len;
int min = offset;


for( int i_move = max – 1, i_to = min; i_move >= 0; i_move–, i_to++ )
{
to[i_to] = (byte)( from >> ( 8 * i_move ) );
}
}
public static byte[] IntToBin( int from, int len )
{
byte[] to = new byte[len];
int max = len;


for( int i_move = max – 1, i_to = 0; i_move >= 0; i_move–, i_to++ )
{
to[i_to] = (byte)( from >> ( 8 * i_move ) );
}


return to;
}
public static byte[] IntToBin( int from, byte[] to, int offset, int len )
{
int max = len;
int min = offset;


for( int i_move = max – 1, i_to = min; i_move >= 0; i_move–, i_to++ )
{
to[i_to] = (byte)( from >> ( 8 * i_move ) );
}


return to;
}


public static long BinToLong( byte[] from, int offset, int len )
{
long to;
int min = offset;
to = 0;
for( int i_move = len – 1, i_from = min; i_move >= 0; i_move–, i_from++ )
{
to = to << 8 | ( from[i_from] & 0xff );
}
return to;
}


public static int BinToInt( byte[] from, int offset, int len )
{
int to = 0;
int min = offset;
to = 0;


for( int i_move = len – 1, i_from = min; i_move >= 0; i_move–, i_from++ )
{
to = to << 8 | ( from[i_from] & 0xff );
}
return to;
}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇什么是属性访问器? 下一篇系统测试退出标准是什么?

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: