- 浏览: 119071 次
- 性别:
- 来自: 西安
最新评论
-
xiaoxin:
(⊙o⊙)… 我到现在才知道,QQQ
Runtime.getRuntime().exec()常见问题 -
uuhorse:
不错,解决了困扰我许久的问题,都准备搞flex了
js判断上传文件大小 -
fenghoer:
ie下不好用??
js判断上传文件大小
public class ByteConvert { // 以下 是整型数 和 网络字节序的 byte[] 数组之间的转换 public static byte[] longToBytes(long n) { byte[] b = new byte[8]; b[7] = (byte) (n & 0xff); b[6] = (byte) (n >> 8 & 0xff); b[5] = (byte) (n >> 16 & 0xff); b[4] = (byte) (n >> 24 & 0xff); b[3] = (byte) (n >> 32 & 0xff); b[2] = (byte) (n >> 40 & 0xff); b[1] = (byte) (n >> 48 & 0xff); b[0] = (byte) (n >> 56 & 0xff); return b; } public static void longToBytes( long n, byte[] array, int offset ){ array[7+offset] = (byte) (n & 0xff); array[6+offset] = (byte) (n >> 8 & 0xff); array[5+offset] = (byte) (n >> 16 & 0xff); array[4+offset] = (byte) (n >> 24 & 0xff); array[3+offset] = (byte) (n >> 32 & 0xff); array[2+offset] = (byte) (n >> 40 & 0xff); array[1+offset] = (byte) (n >> 48 & 0xff); array[0+offset] = (byte) (n >> 56 & 0xff); } public static long bytesToLong( byte[] array ) { return ((((long) array[ 0] & 0xff) << 56) | (((long) array[ 1] & 0xff) << 48) | (((long) array[ 2] & 0xff) << 40) | (((long) array[ 3] & 0xff) << 32) | (((long) array[ 4] & 0xff) << 24) | (((long) array[ 5] & 0xff) << 16) | (((long) array[ 6] & 0xff) << 8) | (((long) array[ 7] & 0xff) << 0)); } public static long bytesToLong( byte[] array, int offset ) { return ((((long) array[offset + 0] & 0xff) << 56) | (((long) array[offset + 1] & 0xff) << 48) | (((long) array[offset + 2] & 0xff) << 40) | (((long) array[offset + 3] & 0xff) << 32) | (((long) array[offset + 4] & 0xff) << 24) | (((long) array[offset + 5] & 0xff) << 16) | (((long) array[offset + 6] & 0xff) << 8) | (((long) array[offset + 7] & 0xff) << 0)); } public static byte[] intToBytes(int n) { byte[] b = new byte[4]; b[3] = (byte) (n & 0xff); b[2] = (byte) (n >> 8 & 0xff); b[1] = (byte) (n >> 16 & 0xff); b[0] = (byte) (n >> 24 & 0xff); return b; } public static void intToBytes( int n, byte[] array, int offset ){ array[3+offset] = (byte) (n & 0xff); array[2+offset] = (byte) (n >> 8 & 0xff); array[1+offset] = (byte) (n >> 16 & 0xff); array[offset] = (byte) (n >> 24 & 0xff); } public static int bytesToInt(byte b[]) { return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 | (b[0] & 0xff) << 24; } public static int bytesToInt(byte b[], int offset) { return b[offset+3] & 0xff | (b[offset+2] & 0xff) << 8 | (b[offset+1] & 0xff) << 16 | (b[offset] & 0xff) << 24; } public static byte[] uintToBytes( long n ) { byte[] b = new byte[4]; b[3] = (byte) (n & 0xff); b[2] = (byte) (n >> 8 & 0xff); b[1] = (byte) (n >> 16 & 0xff); b[0] = (byte) (n >> 24 & 0xff); return b; } public static void uintToBytes( long n, byte[] array, int offset ){ array[3+offset] = (byte) (n ); array[2+offset] = (byte) (n >> 8 & 0xff); array[1+offset] = (byte) (n >> 16 & 0xff); array[offset] = (byte) (n >> 24 & 0xff); } public static long bytesToUint(byte[] array) { return ((long) (array[3] & 0xff)) | ((long) (array[2] & 0xff)) << 8 | ((long) (array[1] & 0xff)) << 16 | ((long) (array[0] & 0xff)) << 24; } public static long bytesToUint(byte[] array, int offset) { return ((long) (array[offset+3] & 0xff)) | ((long) (array[offset+2] & 0xff)) << 8 | ((long) (array[offset+1] & 0xff)) << 16 | ((long) (array[offset] & 0xff)) << 24; } public static byte[] shortToBytes(short n) { byte[] b = new byte[2]; b[1] = (byte) ( n & 0xff); b[0] = (byte) ((n >> 8) & 0xff); return b; } public static void shortToBytes(short n, byte[] array, int offset ) { array[offset+1] = (byte) ( n & 0xff); array[offset] = (byte) ((n >> 8) & 0xff); } public static short bytesToShort(byte[] b){ return (short)( b[1] & 0xff |(b[0] & 0xff) << 8 ); } public static short bytesToShort(byte[] b, int offset){ return (short)( b[offset+1] & 0xff |(b[offset] & 0xff) << 8 ); } public static byte[] ushortToBytes(int n) { byte[] b = new byte[2]; b[1] = (byte) ( n & 0xff); b[0] = (byte) ((n >> 8) & 0xff); return b; } public static void ushortToBytes(int n, byte[] array, int offset ) { array[offset+1] = (byte) ( n & 0xff); array[offset] = (byte) ((n >> 8) & 0xff); } public static int bytesToUshort(byte b[]) { return b[1] & 0xff | (b[0] & 0xff) << 8; } public static int bytesToUshort(byte b[], int offset) { return b[offset+1] & 0xff | (b[offset] & 0xff) << 8; } public static byte[] ubyteToBytes( int n ){ byte[] b = new byte[1]; b[0] = (byte) (n & 0xff); return b; } public static void ubyteToBytes( int n, byte[] array, int offset ){ array[0] = (byte) (n & 0xff); } public static int bytesToUbyte( byte[] array ){ return array[0] & 0xff; } public static int bytesToUbyte( byte[] array, int offset ){ return array[offset] & 0xff; } // char 类型、 float、double 类型和 byte[] 数组之间的转换关系还需继续研究实现。 }
public class ByteConvertTest { public static String byte2Hex(byte[] buf) { StringBuffer strbuf = new StringBuffer(); strbuf.append("{"); for (byte b : buf) { if (b == 0) { strbuf.append("00"); } else if (b == -1) { strbuf.append("FF"); } else { String str = Integer.toHexString(b).toUpperCase(); // sb.append(a); if (str.length() == 8) { str = str.substring(6, 8); } else if (str.length() < 2) { str = "0" + str; } strbuf.append(str); } strbuf.append(" "); } strbuf.append("}"); return strbuf.toString(); } public static byte[] longToBytes(long n) { byte[] b = new byte[8]; b[7] = (byte) (n & 0xff); b[6] = (byte) (n >> 8 & 0xff); b[5] = (byte) (n >> 16 & 0xff); b[4] = (byte) (n >> 24 & 0xff); b[3] = (byte) (n >> 32 & 0xff); b[2] = (byte) (n >> 40 & 0xff); b[1] = (byte) (n >> 48 & 0xff); b[0] = (byte) (n >> 56 & 0xff); return b; } public static long bytesToLong( byte[] array ) { return ((((long) array[ 0] & 0xff) << 56) | (((long) array[ 1] & 0xff) << 48) | (((long) array[ 2] & 0xff) << 40) | (((long) array[ 3] & 0xff) << 32) | (((long) array[ 4] & 0xff) << 24) | (((long) array[ 5] & 0xff) << 16) | (((long) array[ 6] & 0xff) << 8) | (((long) array[ 7] & 0xff) )); } public static int bytesToInt(byte b[]) { return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 | (b[0] & 0xff) << 24; } public static long bytesToUint(byte[] array) { return ((long) (array[3] & 0xff)) | ((long) (array[2] & 0xff)) << 8 | ((long) (array[1] & 0xff)) << 16 | ((long) (array[0] & 0xff)) << 24; } public static byte[] uintToBytes( long n ) { byte[] b = new byte[4]; b[3] = (byte) (n & 0xff); b[2] = (byte) (n >> 8 & 0xff); b[1] = (byte) (n >> 16 & 0xff); b[0] = (byte) (n >> 24 & 0xff); return b; } public static byte[] shortToBytes(short n) { byte[] b = new byte[2]; b[1] = (byte) ( n & 0xff); b[0] = (byte) ((n >> 8) & 0xff); return b; } public static short bytesToShort(byte[] b){ return (short)( b[1] & 0xff |(b[0] & 0xff) << 8 ); } static void testShortConvert(){ System.out.println("=================== short convert ============="); System.out.println("byte2Hex(shortToBytes((short)0x11f2))"+byte2Hex(shortToBytes((short)0x11f2))); System.out.print("println 0x11f2:"); System.out.println((short)0x11f2); System.out.println("byte2Hex(shortToBytes((short)0xf1f2))"+byte2Hex(shortToBytes((short)0xf1f2))); System.out.print("println 0xf1f2:"); System.out.println((short)0xf1f2); System.out.print("println bytesToShort(shortToBytes((short)0x11f2)):"); System.out.println((short)bytesToShort(shortToBytes((short)0x11f2))); System.out.print("println bytesToShort(shortToBytes((short)0xf1f2)):"); System.out.println((short)bytesToShort(shortToBytes((short)0xf1f2))); } public static byte[] ushortToBytes(int n) { byte[] b = new byte[2]; b[1] = (byte) (n & 0xff); b[0] = (byte) (n >> 8 & 0xff); return b; } public static int bytesToUshort(byte b[]) { return b[1] & 0xff | (b[0] & 0xff) << 8; } static void testUshortConvert(){ System.out.println("=================== Ushort convert ============="); System.out.println("byte2Hex(ushortToBytes(0x11f2))"+byte2Hex(ushortToBytes(0x11f2))); System.out.print("println 0x11f2:"); System.out.println(0x11f2); System.out.println("byte2Hex(ushortToBytes(0xf1f2))"+byte2Hex(ushortToBytes(0xf1f2))); System.out.print("println 0xf1f2:"); System.out.println(0xf1f2); System.out.print("println bytesToUshort(ushortToBytes(0x11f2)):"); System.out.println(bytesToUshort(ushortToBytes(0x11f2))); System.out.print("println bytesToUshort(ushortToBytes(0xf1f2)):"); System.out.println(bytesToUshort(ushortToBytes(0xf1f2))); } public static byte[] ubyteToBytes( int n ){ byte[] b = new byte[1]; b[0] = (byte) (n & 0xff); return b; } public static int bytesToUbyte( byte[] array ){ return array[0] & 0xff; } static void testUbyteConvert(){ System.out.println("=================== Ubyte convert ============="); System.out.println("byte2Hex(ubyteToBytes(0x1112))"+byte2Hex(ubyteToBytes(0x1112))); System.out.print("println 0x1112:"); System.out.println(0x1112); System.out.println("byte2Hex(ubyteToBytes(0xf2))"+byte2Hex(ubyteToBytes(0xf2))); System.out.print("println 0xf2:"); System.out.println(0xf2); System.out.print("println bytesToUbyte(ubyteToBytes(0x1112)):"); System.out.println(bytesToUbyte(ubyteToBytes(0x1112))); System.out.print("println bytesToUbyte(ubyteToBytes(0xf1f2)):"); System.out.println(bytesToUbyte(ubyteToBytes(0xf1f2))); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub byte[] array = new byte[4]; array[3] = (byte) 0xF4; array[2] = 0x13; array[1] = 0x12; array[0] = 0x11; System.out.println("=================== Integer bytes ============="); System.out.println("the bytes is:"+byte2Hex(array) ); System.out.print("println bytesToInt :"); System.out.println( bytesToInt(array)); System.out.printf("printf bytesToInt :%X\n", bytesToInt(array)); System.out.println("=================== long bytes ============="); byte[] longBytes = new byte[8]; longBytes[7] = (byte) 0xf7; longBytes[6] = (byte) 0x16; longBytes[5] = (byte) 0xf5; longBytes[4] = (byte) 0x14; longBytes[3] = (byte) 0xf3; longBytes[2] = (byte) 0x12; longBytes[1] = (byte) 0xf1; longBytes[0] = (byte) 0x10; System.out.println( "the bytes is:"+byte2Hex(longBytes) ); System.out.printf("printf bytesToLong:%X\n",bytesToLong(longBytes)); System.out.println("=================byte to long ================"); byte b = (byte)0xf1; System.out.print("Println the byte:"); System.out.println(b); System.out.printf("Printf the byte:%X\n",b); long l = b; System.out.print("Println byte to long:"); System.out.println(l); System.out.printf("printf byte to long:%X\n",l); System.out.println("================= uint Bytes ================"); byte[] uint = new byte[4]; uint[3] = (byte) 0xf3; uint[2] = (byte) 0x12; uint[1] = (byte) 0xf1; uint[0] = (byte) 0xFF; System.out.println( "the bytes is:"+byte2Hex(uint) ); System.out.printf("printf bytesToUint:%X\n",bytesToUint(uint)); System.out.print("Println bytesToUint:"); System.out.println(bytesToUint(uint)); System.out.println("byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l)):"+byte2Hex(uintToBytes(0x11f2f3f4f5f6f7f8l))); System.out.println("===============Long Integer=============="); System.out.print("println 0x11f2f3f4f5f6f7f8l:"); System.out.println(0x11f2f3f4f5f6f7f8l); System.out.printf("Printf 0x11f2f3f4f5f6f7f8l:%X\n",0x11f2f3f4f5f6f7f8l); System.out.println("println byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))"+byte2Hex(longToBytes(0x11f2f3f4f5f6f7f8l))); // 注意,下面的这行,并不能获得正确的uint。 System.out.printf("printf bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l):%X\n",bytesToUint(longToBytes(0x11f2f3f4f5f6f7f8l))); System.out.println("===============bytesToLong(longToBytes())=============="); System.out.println(bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l))); System.out.printf("%X\n",bytesToLong(longToBytes(0x11f2f3f4f5f6f7f8l))); testShortConvert(); testUshortConvert(); testUbyteConvert(); } }
发表评论
-
java读取文件及文件流
2012-02-24 14:44 1142//java读取文件或是文件流的代码,涵盖了读取jar文 ... -
读写二进制文件到字节数组
2012-02-23 15:59 0public class DiskTest { pu ... -
java读写二进制文件
2012-02-23 12:09 5841写入二进制文件,用ultraEdit打开看data1.dat, ... -
java编码
2012-02-22 13:15 4662从基础的开始最小的单元是位(bit),接着是字节(Byte), ... -
处理 Java 程序中的内存漏洞? 研究何时应该关注内存漏洞以及如何预防内存漏洞
2012-02-22 11:32 1150研究何时应该关注内存漏洞以及如何预防内存漏洞 (作者: ... -
阻塞队列 查找文件夹下文件,匹配出指定字符的文件名、行数、该行语句
2011-11-30 15:05 1085public class BlockingQueueTest ... -
读写txt文件
2011-11-21 14:33 885读取txt文件: /** * 读取txt文件的 ... -
解析算术表达式
2011-11-21 14:01 1061现有字符串形式的算术表达式,求计算其值。 Strin ... -
struts2 解决上传问题
2011-11-16 18:10 848记录下最近接触的用struct上传文件的方法。 ... -
队列处理
2011-11-16 17:40 665在做项目的时候有这个需求:用户提交请求需要排队处理(先来 ... -
Runtime.getRuntime().exec()常见问题
2011-11-15 16:14 24161JAVA调用一个bat批处理程序,调用几十次后会莫名的不再执 ... -
dom增删改查xml文件
2011-11-14 10:25 3152public class DOMForXml { ... -
多线程——waiting-notify机制实例(三个人打水)
2011-11-09 11:03 858//三个学生到一个水龙头下排队取水,三人都取完水后一起离 ... -
数字格式化
2011-03-22 11:00 785double pi=3.1415927; //圆周率 ...
相关推荐
写一个方法,将int类型转换为字节数组,输入任意int类型整型,输出字节数组;写第二个方法,输入字节数组,输出对应int类型数据。
ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...
主要介绍了Java整型数与网络字节序byte[]数组转换关系,结合实例形式归纳整理了java整型数和网络字节序的byte[]之间转换的各种情况,需要的朋友可以参考下
LabVIEW程序,功能:将4字节的unsigned char输入组合成1个32-bit int值,若输入字节数不等于4则报错。
C#各种数据类型转换,字符串 转换 char数组,...将Base64字符串解码为普通字符串,图片 转换 byte数组,byte数组 转换 图片,ip 转换 长整形,长整形 转换 IP,将8位日期型整型数据转换为日期字符串数据,string型转换为bool型,
下面小编就为大家带来一篇Java基本类型与byte数组之间相互转换方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
字节位数和float型转换小工具,可实现双向转换,占容小,速度快捷
C#开发,收到下位机串口数据(温度信息),可能是正数也可能是负数,如何转换?...补充知识:c# byte数组转换 8位有符号整数 16位有符号整数 32位有符号整数 byte数组 byte[] aa = new byte[] { 0xF8
该代码可以实现单精度浮点数和16进制字符之间的转换,包含源代码
通信接收四个字节转浮点小数和数据解包,测试成功,完美使用
count 数组中用作数据的字节数 返回值 计算所得的哈希代码 说明:本实例用到了ComputeHash 方法的第一种重载形式。 设 计过程 (1)打开Visual Studio 2008 开发环境,新建一个Windows窗体应用程序,并将其命名为...
case 4://当指定该值为4时,当Date为BYTE数组时,将把一个BYTE转换成一个char传送 case 5://当指定该值为5时,当Date为短整型数组时,将把一个short转换成两个char传送 case 6://当指定该值为6时,当Date为浮点型数组...
Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...
Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...
Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...
Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...
Len 函数 返回字符串中的字符数量,或者存储变量所需的字节数。 Length 属性 返回在搜索字符串中匹配的长度。 LoadPicture 函数 返回图片对象。仅用于 32 位平台。 Log 函数 返回数的自然对数。 LTrim 函数 返回...
本文研究的主要是Python内建模块struct的相关内容,具体如下...(1)struct.pack:用于将Python的值根据格式符,转换为字符串(因为Python中没有字节(Byte)类型,可以把这里的字符串理解为字节流,或字节数组)。 (2
//main()的参数是string类型的数组,用来做为长,宽时,要转换成整型。 { int w=new Integer(args[0]).intValue(); int h=Integer.parseInt(args[1]); //int h=Integer.valueOf(args[1]).intValue(); //以上为三种将...
4.java语言中的逻辑变量可以和整型变量相互强制转换。 5.面向对象的软件开发方法用类把数据和基于数据的操作封装在一起,并且类之间可以存在继承关系。 6.方法可以没有返回值,或有一个返回值,也可以有多个返回值。...