int 转byte[]

  1. import java.io.*;    
  2. public class IOTest {    
  3.     public static void main(String[] args) throws Exception {    
  4.         int i = 65535;    
  5.         byte[] b = intToByteArray1(i);    
  6.         for (byte bb : b) {    
  7.             System.out.print(bb + " ");    
  8.         }    
  9.     }    
  10.     public static byte[] intToByteArray1(int i) {    
  11.         byte[] result = new byte[4];    
  12.         result[0] = (byte) ((i >> 24) & 0xFF);    
  13.         result[1] = (byte) ((i >> 16) & 0xFF);    
  14.         result[2] = (byte) ((i >> 8) & 0xFF);    
  15.         result[3] = (byte) (i & 0xFF);    
  16.         return result;    
  17.     }    
  18.     public static byte[] intToByteArray2(int i) throws Exception {    
  19.         ByteArrayOutputStream buf = new ByteArrayOutputStream();    
  20.         DataOutputStream out = new DataOutputStream(buf);    
  21.         out.writeInt(i);    
  22.         byte[] b = buf.toByteArray();    
  23.         out.close();    
  24.         buf.close();    
  25.         return b;    
  26.     }    
  27. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值