- 浏览: 138658 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (129)
- framework (18)
- Java (25)
- web2.0 (7)
- Oracle (23)
- 框架师 (20)
- design pattern (5)
- j2ee (6)
- 日常问题收集 (51)
- linux (12)
- Oralce优化 (4)
- life (3)
- sqlplus (1)
- sql (2)
- C# (4)
- mysql (9)
- 拿来主义 (12)
- 标签 (0)
- ThinkPHP (1)
- python (1)
- mybatis (2)
- html5 (1)
- jquery (2)
- hadoop (3)
- git (1)
- struts2 (1)
- shiro (1)
- struts (1)
最新评论
-
xiao88xian:
...
jQuery常用经典技巧
开发问题收集
package com.util; /** * * <ul> * <li>文件名称: com.born.util.ByteUtil.java</li> * <li>文件描述: byte转换工具</li> * <li>版权所有: 版权所有(C)2001-2006</li> * <li>公 司: bran</li> * <li>内容摘要:</li> * <li>其他说明:</li> * <li>完成日期:2011-7-18</li> * <li>修改记录0:无</li> * </ul> * * @version 1.0 * @author 许力多 */ public class ByteUtil { /** * 转换short为byte * * @param b * @param s * 需要转换的short * @param index */ public static void putShort(byte b[], short s, int index) { b[index + 1] = (byte) (s >> 8); b[index + 0] = (byte) (s >> 0); } /** * 通过byte数组取到short * * @param b * @param index * 第几位开始取 * @return */ public static short getShort(byte[] b, int index) { return (short) (((b[index + 1] << 8) | b[index + 0] & 0xff)); } /** * 转换int为byte数组 * * @param bb * @param x * @param index */ public static void putInt(byte[] bb, int x, int index) { bb[index + 3] = (byte) (x >> 24); bb[index + 2] = (byte) (x >> 16); bb[index + 1] = (byte) (x >> 8); bb[index + 0] = (byte) (x >> 0); } /** * 通过byte数组取到int * * @param bb * @param index * 第几位开始 * @return */ public static int getInt(byte[] bb, int index) { return (int) ((((bb[index + 3] & 0xff) << 24) | ((bb[index + 2] & 0xff) << 16) | ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 0xff) << 0))); } /** * 转换long型为byte数组 * * @param bb * @param x * @param index */ public static void putLong(byte[] bb, long x, int index) { bb[index + 7] = (byte) (x >> 56); bb[index + 6] = (byte) (x >> 48); bb[index + 5] = (byte) (x >> 40); bb[index + 4] = (byte) (x >> 32); bb[index + 3] = (byte) (x >> 24); bb[index + 2] = (byte) (x >> 16); bb[index + 1] = (byte) (x >> 8); bb[index + 0] = (byte) (x >> 0); } /** * 通过byte数组取到long * * @param bb * @param index * @return */ public static long getLong(byte[] bb, int index) { return ((((long) bb[index + 7] & 0xff) << 56) | (((long) bb[index + 6] & 0xff) << 48) | (((long) bb[index + 5] & 0xff) << 40) | (((long) bb[index + 4] & 0xff) << 32) | (((long) bb[index + 3] & 0xff) << 24) | (((long) bb[index + 2] & 0xff) << 16) | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0)); } /** * 字符到字节转换 * * @param ch * @return */ public static void putChar(byte[] bb, char ch, int index) { int temp = (int) ch; // byte[] b = new byte[2]; for (int i = 0; i < 2; i ++ ) { bb[index + i] = new Integer(temp & 0xff).byteValue(); // 将最高位保存在最低位 temp = temp >> 8; // 向右移8位 } } /** * 字节到字符转换 * * @param b * @return */ public static char getChar(byte[] b, int index) { int s = 0; if (b[index + 1] > 0) s += b[index + 1]; else s += 256 + b[index + 0]; s *= 256; if (b[index + 0] > 0) s += b[index + 1]; else s += 256 + b[index + 0]; char ch = (char) s; return ch; } /** * float转换byte * * @param bb * @param x * @param index */ public static void putFloat(byte[] bb, float x, int index) { // byte[] b = new byte[4]; int l = Float.floatToIntBits(x); for (int i = 0; i < 4; i++) { bb[index + i] = new Integer(l).byteValue(); l = l >> 8; } } /** * 通过byte数组取得float * * @param bb * @param index * @return */ public static float getFloat(byte[] b, int index) { int l; l = b[index + 0]; l &= 0xff; l |= ((long) b[index + 1] << 8); l &= 0xffff; l |= ((long) b[index + 2] << 16); l &= 0xffffff; l |= ((long) b[index + 3] << 24); return Float.intBitsToFloat(l); } /** * double转换byte * * @param bb * @param x * @param index */ public static void putDouble(byte[] bb, double x, int index) { // byte[] b = new byte[8]; long l = Double.doubleToLongBits(x); for (int i = 0; i < 4; i++) { bb[index + i] = new Long(l).byteValue(); l = l >> 8; } } /** * 通过byte数组取得float * * @param bb * @param index * @return */ public static double getDouble(byte[] b, int index) { long l; l = b[0]; l &= 0xff; l |= ((long) b[1] << 8); l &= 0xffff; l |= ((long) b[2] << 16); l &= 0xffffff; l |= ((long) b[3] << 24); l &= 0xffffffffl; l |= ((long) b[4] << 32); l &= 0xffffffffffl; l |= ((long) b[5] << 40); l &= 0xffffffffffffl; l |= ((long) b[6] << 48); l &= 0xffffffffffffffl; l |= ((long) b[7] << 56); return Double.longBitsToDouble(l); } }
public class DataTypeChangeHelper { /** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return convert result */ public static int unsignedByteToInt(byte b) { return (int) b & 0xFF; } /** * 将一个单字节的Byte转换成十六进制的数 * * @param b * byte * @return convert result */ public static String byteToHex(byte b) { int i = b & 0xFF; return Integer.toHexString(i); } /** * 将一个4byte的数组转换成32位的int * * @param buf * bytes buffer * @param byte[]中开始转换的位置 * @return convert result */ public static long unsigned4BytesToInt(byte[] buf, int pos) { int firstByte = 0; int secondByte = 0; int thirdByte = 0; int fourthByte = 0; int index = pos; firstByte = (0x000000FF & ((int) buf[index])); secondByte = (0x000000FF & ((int) buf[index + 1])); thirdByte = (0x000000FF & ((int) buf[index + 2])); fourthByte = (0x000000FF & ((int) buf[index + 3])); index = index + 4; return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL; } /** * 将16位的short转换成byte数组 * * @param s * short * @return byte[] 长度为2 * */ public static byte[] shortToByteArray(short s) { byte[] targets = new byte[2]; for (int i = 0; i < 2; i++) { int offset = (targets.length - 1 - i) * 8; targets[i] = (byte) ((s >>> offset) & 0xff); } return targets; } /** * 将32位整数转换成长度为4的byte数组 * * @param s * int * @return byte[] * */ public static byte[] intToByteArray(int s) { byte[] targets = new byte[2]; for (int i = 0; i < 4; i++) { int offset = (targets.length - 1 - i) * 8; targets[i] = (byte) ((s >>> offset) & 0xff); } return targets; } /** * long to byte[] * * @param s * long * @return byte[] * */ public static byte[] longToByteArray(long s) { byte[] targets = new byte[2]; for (int i = 0; i < 8; i++) { int offset = (targets.length - 1 - i) * 8; targets[i] = (byte) ((s >>> offset) & 0xff); } return targets; } /**32位int转byte[]*/ public static byte[] int2byte(int res) { byte[] targets = new byte[4]; targets[0] = (byte) (res & 0xff);// 最低位 targets[1] = (byte) ((res >> 8) & 0xff);// 次低位 targets[2] = (byte) ((res >> 16) & 0xff);// 次高位 targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。 return targets; } /** * 将长度为2的byte数组转换为16位int * * @param res * byte[] * @return int * */ public static int byte2int(byte[] res) { // res = InversionByte(res); // 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000 int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或 return targets; } }
发表评论
-
Ubuntu 12.04(32位)安装Oracle 11g(32位)全过程以及几乎所有问题的解决办法
2014-08-06 16:26 802from :http://blog.csdn.net/idb ... -
org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessi
2014-01-16 10:16 1701关于shiro错误的分析错误提示: org.apache. ... -
在windows下配置hadoop-eclipse插件,并连接虚拟机的hadoop集群
2013-08-19 15:51 972在windows下配置hadoop-eclipse插件,并连 ... -
如何禁止浏览器自动完成(Turn Off auto text complete)
2013-08-09 11:26 1338关于如何禁止浏览器自动完成功能,大概有两种方式,第一种是通过 ... -
Maven乱码问题解决
2013-08-07 17:22 773本文转载自: http:/ ... -
Git SSH Key 生成步骤
2013-08-03 21:38 743git是分布式的代码管理工具,远程的代码管理是基于ssh的, ... -
Public key for *.rpm is not installed
2013-08-03 13:10 778>[root@RedHat mnt]# cd /opt ... -
更新你的OEL
2013-08-03 13:01 904windows下有自动更新的功能,linux下也有,对应的RH ... -
Linux下Mysql的配置
2013-08-03 12:53 585安装 rmp -ivh MySQL-server-4.1. ... -
如何设置Mysql允许局域网或外部连接的方法
2013-08-02 14:54 1622如何设置Mysql允许局域网或外部连接的方法 在linux ... -
Virtual Box虚拟机host-only 方式联网
2013-07-29 20:41 8091.需要讲本地的外网网络共享打开 2.查看VirtualB ... -
hadoop中datanode无法启动
2013-07-21 18:47 1441多日无暇学习使用Hadoop了,今天在练习安装使用Hive的 ... -
使用OSCache实现页面的缓存,优化性能
2013-07-10 14:21 1011使用OSCache实现页面的缓存,优化性能 OSC ... -
查找附近点--Geohash方案讨论
2013-06-06 09:27 1264基础数据中,一般保存了目标位置的经纬度;利用用户提供的经纬度 ... -
LBS附近地点搜索算法资料集合
2013-06-06 09:18 837方法一:基于球面距离搜索附近地点 附近地点搜索初探 ... -
geohash算法原理及实现方式
2013-06-05 09:53 1805geohash算法原理及实现方式 1、geohas ... -
附近地点搜索初探
2013-06-05 09:19 690FROM:http://tech.idv2.com/201 ... -
MyBatis延迟加载
2013-05-26 20:15 804当你要使用one to one,many to one 就 ... -
我的gVim配置备份
2013-05-11 16:03 1220if(has("win32") || h ... -
tomcat虚拟路径的几种配置方法
2013-04-23 10:18 910摘自:http://dolphin-ygj.javaeye. ...
相关推荐
下面小编就为大家带来一篇java byte数组与int,long,short,byte的转换实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...
char short int long float double 转换成byte数组
byte数组转换为int 保留几位小数 null转String String转Byte String转Boolean String转Int String转Short String转Double Int转String Double转Long Double转Int Long转Double Long转Int String转Long Long转String
举例分析 equals 和 hashcode 方法,hashcode应该怎么样生成 8个基本类型与基本对象的比较:byte与Byte shot与Short int与Integer long与Long float与Float double与Double char与Character
字节转换什么事啊您是否曾经想过通过I2C,SPI,串行或其他协议或总线传输int , short , long , double或任何其他数字类型,但是您已将变量转换为字符串以能够按char进行传输。 该库使您可以将任何数值转换为字节...
由于实际开发中这样的类型转换很多,所以Java语言在设计时,没有为该操作设计语法,而是由JVM自动完成。 转换规则 从存储范围小的类型到存储范围大的类型。 具体规则为: byte→short(char)→int→long→float→...
(1)基本类型 byte short int long float double char boolean (2)引用类型 类 数组 接口 枚举 注解 变量:内存中的一块存储空间, 存储的就是常量。 特点:一个变量只能存储一个数据,不能存储多个。 需求:...
在java中的整数类型有四种,分别是byte short in long,本文重点给大家介绍java中的整数类型(short int long),由于byte只是一个字节0或1,在此就不多说了,对java中的整数类型感兴趣的朋友一起学习吧
在编程过程中经常用到各种数据类型之间的相互转换,此代码中实现了short、int、long。float、double等类型与byte数组相互转换,根据数据类型的位数不同,对应的数组大小也不同、例如int32位,4个字节,则需要大小为4...
一些初学JAVA的朋友可能会遇到JAVA的数据类型之间转换的苦恼,例如,整数和float,double型之间的转换,整数和String类型之间的转换,以及处理、显示时间方面的问...byte、short、int、long;浮点型float、double。其
在switch(expr1)中,expr1只能是一个整数表达式或者枚举常量(更大字体),整数表达式可以是int基本类型或Integer包装类型,由于,byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型的包装类型也...
主要介绍了Java 基础 byte[]与各种数据类型互相转换的简单示例的相关资料,这里对byte[]类型对long,int,double,float,short,cahr,object,string类型相互转换的实例,需要的朋友可以参考下
当需要把某个属性值或者文本块内容转换为int、short、long时,不需要先把byte数组转化成string,然后再转化成int、short、long。FastXml直接把byte数组转换成对应的类型。 如果xml文档中有些标签内容你并...
byte→short(char)→int→long→float→double 也就是说byte类型的变量可以自动转换为short类型,示例代码: byte b = 10; short sh = b; 在类型转换时可以跳跃。示例代码: byte b1 = 100; int n = b1; ...
原始类型封装类booleanBoolean charCharacter byteByte shortShort intInteger longLong floatFloat doubleDouble 引用类型和原始类型的行为完全不同,并且它们具有不同的语义。引用类型和原始类型具有不同的特征和...
第一种对应的是java中的整形 int long short byte 而第二种对应java中的 浮点型 float double oracle中的的date 对应java中的 java.util.date java.sql.date 是都可以的 但通常应用java.util.date 因为它的作用范畴...
键盘输入1个字符串,如果是整数数字串,如”4353”, 分别按八进制和十六进制输出,以及按byte,short,int,long,char输出;每个数占10位长.每行输出3个数; 如果是带小数数字串,如”243.58”,分别按short,int,long,float...
Java Number类是Java中的一个抽象类,它是所有数值类型的超类,包括整数、浮点数和大数。它提供了一组用于操作数值类型的方法,如转换、比较、算术运算等。 二、Java Number类提供了哪些基本的数字操作? Java ...
基本类型:byte,short,int,long,float,double,boolean,char 3.包装类型:Byte,Short,Integer,Long,Float,Double,Boolean,Character 4.String 5.Collection 6.Map 7.Enum 8.TimeZone 9.Class 10.BigDecimal 11...