首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

byte[]到short、int、long的互相转换

2012-10-08 
byte[]到short、int、long的相互转换?001 public final static byte[] getBytes(short s, boolean asc) 002{

byte[]到short、int、long的相互转换

?001 public final static byte[] getBytes(short s, boolean asc)

002{003????byte[] buf = new byte[2];004????if (asc)005????????for (int i = buf.length - 1; i >= 0; i--) 006????????{007????????????buf[i] = (byte) (s & 0x00ff);008??????????s >>= 8;009????????}010????else011????????for (int i = 0; i < buf.length; i++) 012????????{013????????????buf[i] = (byte) (s & 0x00ff);014??????????s >>= 8;015????????}016????return buf;017}018019public final static byte[] getBytes(int s, boolean asc) 020{021????byte[] buf = new byte[4];022????if (asc)023??????for (int i = buf.length - 1; i >= 0; i--) 024??????{025????????buf[i] = (byte) (s & 0x000000ff);026????????s >>= 8;027??????}028????else029??????for (int i = 0; i < buf.length; i++)030??????{031????????buf[i] = (byte) (s & 0x000000ff);032????????s >>= 8;033??????}034????return buf;035}036037public final static byte[] getBytes(long s, boolean asc) 038{039????byte[] buf = new byte[8];040????if (asc)041??????for (int i = buf.length - 1; i >= 0; i--) 042????????{043????????buf[i] = (byte) (s & 0x00000000000000ff);044????????s >>= 8;045??????}046????else047??????for (int i = 0; i < buf.length; i++) 048????????{049????????buf[i] = (byte) (s & 0x00000000000000ff);050????????s >>= 8;051??????}052????return buf;053}054055public final static short getShort(byte[] buf, boolean asc) 056{057????if (buf == null) 058????{059??????throw new IllegalArgumentException("byte array is null!");060????}061????if (buf.length > 2) 062????{063??????throw new IllegalArgumentException("byte array size > 2 !");064????}065????short r = 0;066????if (asc)067??????for (int i = buf.length - 1; i >= 0; i--) 068????????{069????????r <<= 8;070????????r |= (buf[i] & 0x00ff);071??????}072????else073??????for (int i = 0; i < buf.length; i++) 074????????{075????????r <<= 8;076????????r |= (buf[i] & 0x00ff);077??????}078????return r;079}080081public final static int getInt(byte[] buf, boolean asc) 082{083????if (buf == null) 084????{085??????throw new IllegalArgumentException("byte array is null!");086????}087????if (buf.length > 4) 088????{089??????throw new IllegalArgumentException("byte array size > 4 !");090????}091????int r = 0;092????if (asc)093??????for (int i = buf.length - 1; i >= 0; i--) 094????????{095????????r <<= 8;096????????r |= (buf[i] & 0x000000ff);097??????}098????else099??????for (int i = 0; i < buf.length; i++) 100????????{101????????r <<= 8;102????????r |= (buf[i] & 0x000000ff);103??????}104????return r;105}106107public final static long getLong(byte[] buf, boolean asc) 108{109????if (buf == null) 110????{111??????throw new IllegalArgumentException("byte array is null!");112????}113????if (buf.length > 8) 114????{115??????throw new IllegalArgumentException("byte array size > 8 !");116????}117????long r = 0;118????if (asc)119??????for (int i = buf.length - 1; i >= 0; i--) 120????????{121????????r <<= 8;122????????r |= (buf[i] & 0x00000000000000ff);123??????}124????else125??????for (int i = 0; i < buf.length; i++) 126????????{127????????r <<= 8;128????????r |= (buf[i] & 0x00000000000000ff);129??????}130????return r;131}

热点排行