请教,如何将int类型转换为一个byte数组? 请教,如何将int类型转换为一个byte数组?每个数组元素对应一个int的字节 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如,有一个byte[255]的字节数组a,其中 a[0]~a[3]是一个整数的4个字节,a[4]~a[x]是一个字符串,x-4由是该整数的值,a[x+1]~a[x+4]是另外一个整数,那我该如何从这个字节数组中取得我所要的数据? int对应4个字节分别为:byte[] intBytes = new byte[4];intBytes[0] = (byte) (x >> 24);intBytes[1] = (byte) (x >> 16);intBytes[2] = (byte) (x >> 8);intBytes[3] = (byte) (x >> 0);而对应从字节到int,b3对应上面的intBytes[0]: public static int makeInt(byte b3, byte b2, byte b1, byte b0) { return (int) ((((b3 & 0xff) << 24) | ((b2 & 0xff) << 16) | ((b1 & 0xff) << 8) | ((b0 & 0xff) << 0))); } int a;String s = String.valueOf(a);byte[] b = s.getBytes(); Java 截取字符串后面固定几位 SWT中Table中放Text的问题?(急急急急急) java调用DLL文件 如何将类名作为参数? 寻求一个支持metapost 的java package。。。有知情者回复 问一个关于html 的问题,请高手指点啊! 菜鸟问题,多多给分,欢迎回答,吐血给分。 向高手求教:java可以写桌面应用吗? java验证邮箱有效性 JAVA接口中不可以有静态方法吗?1.8之前不可以 1.8可以了 真是。。。。。。。 怎样实现双击label后转换为textfield Font的构造器问题
byte[] intBytes = new byte[4];
intBytes[0] = (byte) (x >> 24);
intBytes[1] = (byte) (x >> 16);
intBytes[2] = (byte) (x >> 8);
intBytes[3] = (byte) (x >> 0);而对应从字节到int,b3对应上面的intBytes[0]:
public static int makeInt(byte b3, byte b2, byte b1, byte b0) {
return (int) ((((b3 & 0xff) << 24) | ((b2 & 0xff) << 16)
| ((b1 & 0xff) << 8) | ((b0 & 0xff) << 0)));
}
String s = String.valueOf(a);
byte[] b = s.getBytes();