栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > Java面试题

java byte[]、int、long互转

Java面试题 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/264342.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号