当前位置: 技术问答>java相关
byte[]
来源: 互联网 发布时间:2015-06-10
本文导语: 怎样将任意类型的数据出入byte[]中 如int i=132; byte[] num=new byte[4]; 使num[]中存放的是132的二进制代码 有没有类似String.getBytes()的方法? | num[0] = (byte)((i & 0xFF000000) >>> 24); num[1] = (byte)((i & ...
怎样将任意类型的数据出入byte[]中
如int i=132;
byte[] num=new byte[4];
使num[]中存放的是132的二进制代码
有没有类似String.getBytes()的方法?
如int i=132;
byte[] num=new byte[4];
使num[]中存放的是132的二进制代码
有没有类似String.getBytes()的方法?
|
num[0] = (byte)((i & 0xFF000000) >>> 24);
num[1] = (byte)((i & 0xFF0000) >>> 16);
num[2] = (byte)((i & 0xFF00) >>> 8);
num[3] = (byte)(i & 0xFF);
num[1] = (byte)((i & 0xFF0000) >>> 16);
num[2] = (byte)((i & 0xFF00) >>> 8);
num[3] = (byte)(i & 0xFF);
|
/*
* $Id: DataConverter.java,v 1.1.1.1 2002/01/19 20:33:25 kevem Exp $
* This file is part of jxUtil: http://jxutil.sourceforge.net
*
* Copyright (c) 2001 Keve Müller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* jxUtil is hosted by SourceForge.
* SourceForge is a trademark of VA Linux Systems, Inc.
*/
package org.sourceforge.jxutil.io;
import java.math.BigInteger;
public abstract class DataConverter {
DataConverter() {
}
public static DataConverter getInstance() {
// BE? LE?
throw new InternalError();
}
public static DataConverter getInstance(boolean bele) {
return bele ? DataConverterBE.instance : DataConverterLE.instance;
}
public final byte getByte(byte[] buf, int ofs) {
return buf[ofs];
}
public final int getUnsignedByte(byte[] buf, int ofs) {
return buf[ofs]&0xFF;
}
public abstract short getShort(byte[] buf, int ofs);
public final int getUnsignedShort(byte[] buf, int ofs) {
return getShort(buf,ofs)&0xFFFF;
}
public abstract int getInt(byte[] buf, int ofs);
public final long getUnsignedInt(byte[] buf, int ofs) {
return getInt(buf,ofs)&0xFFFFFFFFl;
}
public abstract long getLong(byte[] buf, int ofs);
public final BigInteger getUnsignedLong(byte[] buf, int ofs) {
throw new InternalError();
}
public final float getFloat(byte[] buf, int ofs) {
return Float.intBitsToFloat(getInt(buf,ofs));
}
public final double getDouble(byte[] buf, int ofs) {
return Double.longBitsToDouble(getLong(buf,ofs));
}
public final byte getByte(byte[] buf) {
return buf[0];
}
public final int getUnsignedByte(byte[] buf) {
return buf[0]&0xFF;
}
public final short getShort(byte[] buf) {
return getShort(buf,0);
}
public final int getUnsignedShort(byte[] buf) {
return getShort(buf,0)&0xFFFF;
}
public final int getInt(byte[] buf) {
return getInt(buf,0);
}
public final long getUnsignedInt(byte[] buf) {
return getInt(buf,0)&0xFFFFFFFFl;
}
public final long getLong(byte[] buf) {
return getLong(buf,0);
}
public final BigInteger getUnsignedLong(byte[] buf) {
throw new InternalError();
}
public final float getFloat(byte[] buf) {
return Float.intBitsToFloat(getInt(buf,0));
}
public final double getDouble(byte[] buf) {
return Double.longBitsToDouble(getLong(buf,0));
}
public final void setByte(byte x, byte[] buf, int ofs) {
buf[ofs]=x;
}
public final void setUnsignedByte(int x, byte[] buf, int ofs) {
buf[ofs]=(byte)x;
}
public abstract void setShort(short x, byte[] buf, int ofs);
public final void setUnsignedShort(int x, byte[] buf, int ofs) {
setShort((short)x,buf,ofs);
}
public abstract void setInt(int x, byte[] buf, int ofs);
public final void setUnsignedInt(long x, byte[] buf, int ofs) {
setInt((int)x,buf,ofs);
}
public abstract void setLong(long x, byte[] buf, int ofs);
public final void setUnsignedLong(BigInteger x, byte[] buf, int ofs) {
throw new InternalError();
}
public final void setFloat(float x, byte[] buf, int ofs) {
setInt(Float.floatToIntBits(x),buf,ofs);
}
public final void setDouble(double x, byte[] buf, int ofs) {
setLong(Double.doubleToLongBits(x),buf,ofs);
}
public final void setByte(byte x, byte[] buf) {
buf[0]=x;
}
public final void setUnsignedByte(int x, byte[] buf) {
buf[0]=(byte)x;
}
public final void setShort(short x, byte[] buf) {
setShort(x,buf,0);
}
public final void setUnsignedShort(int x, byte[] buf) {
setUnsignedShort(x,buf,0);
}
public final void setInt(int x, byte[] buf) {
setInt(x,buf,0);
}
public final void setUnsignedInt(long x, byte[] buf) {
setUnsignedInt(x,buf,0);
}
public final void setLong(long x, byte[] buf) {
setLong(x,buf,0);
}
public final void setUnsignedLong(BigInteger x, byte[] buf) {
setUnsignedLong(x,buf,0);
}
public final void setFloat(float x, byte[] buf) {
setInt(Float.floatToIntBits(x),buf,0);
}
public final void setDouble(double x, byte[] buf) {
setLong(Double.doubleToLongBits(x),buf,0);
}
public final byte[] setByte(byte x) {
byte[] buf=new byte[1];
buf[0]=x;
return buf;
}
public final byte[] setUnsignedByte(int x) {
byte[] buf=new byte[1];
buf[0]=(byte)x;
return buf;
}
public final byte[] setShort(short x) {
byte[] buf=new byte[2];
setShort(x,buf,0);
return buf;
}
public final byte[] setUnsignedShort(int x) {
byte[] buf=new byte[2];
setUnsignedShort(x,buf,0);
return buf;
}
public final byte[] setInt(int x) {
byte[] buf=new byte[4];
setInt(x,buf,0);
return buf;
}
public final byte[] setUnsignedInt(long x) {
byte[] buf=new byte[4];
setUnsignedInt(x,buf,0);
return buf;
}
public final byte[] setLong(long x) {
byte[] buf=new byte[8];
setLong(x,buf,0);
return buf;
}
public final byte[] setUnsignedLong(BigInteger x) {
byte[] buf=new byte[8];
setUnsignedLong(x,buf,0);
return buf;
}
public final byte[] setFloat(float x) {
byte[] buf=new byte[4];
setFloat(x,buf,0);
return buf;
}
public final byte[] setDouble(double x) {
byte[] buf=new byte[8];
setDouble(x,buf,0);
return buf;
}
}
* $Id: DataConverter.java,v 1.1.1.1 2002/01/19 20:33:25 kevem Exp $
* This file is part of jxUtil: http://jxutil.sourceforge.net
*
* Copyright (c) 2001 Keve Müller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* jxUtil is hosted by SourceForge.
* SourceForge is a trademark of VA Linux Systems, Inc.
*/
package org.sourceforge.jxutil.io;
import java.math.BigInteger;
public abstract class DataConverter {
DataConverter() {
}
public static DataConverter getInstance() {
// BE? LE?
throw new InternalError();
}
public static DataConverter getInstance(boolean bele) {
return bele ? DataConverterBE.instance : DataConverterLE.instance;
}
public final byte getByte(byte[] buf, int ofs) {
return buf[ofs];
}
public final int getUnsignedByte(byte[] buf, int ofs) {
return buf[ofs]&0xFF;
}
public abstract short getShort(byte[] buf, int ofs);
public final int getUnsignedShort(byte[] buf, int ofs) {
return getShort(buf,ofs)&0xFFFF;
}
public abstract int getInt(byte[] buf, int ofs);
public final long getUnsignedInt(byte[] buf, int ofs) {
return getInt(buf,ofs)&0xFFFFFFFFl;
}
public abstract long getLong(byte[] buf, int ofs);
public final BigInteger getUnsignedLong(byte[] buf, int ofs) {
throw new InternalError();
}
public final float getFloat(byte[] buf, int ofs) {
return Float.intBitsToFloat(getInt(buf,ofs));
}
public final double getDouble(byte[] buf, int ofs) {
return Double.longBitsToDouble(getLong(buf,ofs));
}
public final byte getByte(byte[] buf) {
return buf[0];
}
public final int getUnsignedByte(byte[] buf) {
return buf[0]&0xFF;
}
public final short getShort(byte[] buf) {
return getShort(buf,0);
}
public final int getUnsignedShort(byte[] buf) {
return getShort(buf,0)&0xFFFF;
}
public final int getInt(byte[] buf) {
return getInt(buf,0);
}
public final long getUnsignedInt(byte[] buf) {
return getInt(buf,0)&0xFFFFFFFFl;
}
public final long getLong(byte[] buf) {
return getLong(buf,0);
}
public final BigInteger getUnsignedLong(byte[] buf) {
throw new InternalError();
}
public final float getFloat(byte[] buf) {
return Float.intBitsToFloat(getInt(buf,0));
}
public final double getDouble(byte[] buf) {
return Double.longBitsToDouble(getLong(buf,0));
}
public final void setByte(byte x, byte[] buf, int ofs) {
buf[ofs]=x;
}
public final void setUnsignedByte(int x, byte[] buf, int ofs) {
buf[ofs]=(byte)x;
}
public abstract void setShort(short x, byte[] buf, int ofs);
public final void setUnsignedShort(int x, byte[] buf, int ofs) {
setShort((short)x,buf,ofs);
}
public abstract void setInt(int x, byte[] buf, int ofs);
public final void setUnsignedInt(long x, byte[] buf, int ofs) {
setInt((int)x,buf,ofs);
}
public abstract void setLong(long x, byte[] buf, int ofs);
public final void setUnsignedLong(BigInteger x, byte[] buf, int ofs) {
throw new InternalError();
}
public final void setFloat(float x, byte[] buf, int ofs) {
setInt(Float.floatToIntBits(x),buf,ofs);
}
public final void setDouble(double x, byte[] buf, int ofs) {
setLong(Double.doubleToLongBits(x),buf,ofs);
}
public final void setByte(byte x, byte[] buf) {
buf[0]=x;
}
public final void setUnsignedByte(int x, byte[] buf) {
buf[0]=(byte)x;
}
public final void setShort(short x, byte[] buf) {
setShort(x,buf,0);
}
public final void setUnsignedShort(int x, byte[] buf) {
setUnsignedShort(x,buf,0);
}
public final void setInt(int x, byte[] buf) {
setInt(x,buf,0);
}
public final void setUnsignedInt(long x, byte[] buf) {
setUnsignedInt(x,buf,0);
}
public final void setLong(long x, byte[] buf) {
setLong(x,buf,0);
}
public final void setUnsignedLong(BigInteger x, byte[] buf) {
setUnsignedLong(x,buf,0);
}
public final void setFloat(float x, byte[] buf) {
setInt(Float.floatToIntBits(x),buf,0);
}
public final void setDouble(double x, byte[] buf) {
setLong(Double.doubleToLongBits(x),buf,0);
}
public final byte[] setByte(byte x) {
byte[] buf=new byte[1];
buf[0]=x;
return buf;
}
public final byte[] setUnsignedByte(int x) {
byte[] buf=new byte[1];
buf[0]=(byte)x;
return buf;
}
public final byte[] setShort(short x) {
byte[] buf=new byte[2];
setShort(x,buf,0);
return buf;
}
public final byte[] setUnsignedShort(int x) {
byte[] buf=new byte[2];
setUnsignedShort(x,buf,0);
return buf;
}
public final byte[] setInt(int x) {
byte[] buf=new byte[4];
setInt(x,buf,0);
return buf;
}
public final byte[] setUnsignedInt(long x) {
byte[] buf=new byte[4];
setUnsignedInt(x,buf,0);
return buf;
}
public final byte[] setLong(long x) {
byte[] buf=new byte[8];
setLong(x,buf,0);
return buf;
}
public final byte[] setUnsignedLong(BigInteger x) {
byte[] buf=new byte[8];
setUnsignedLong(x,buf,0);
return buf;
}
public final byte[] setFloat(float x) {
byte[] buf=new byte[4];
setFloat(x,buf,0);
return buf;
}
public final byte[] setDouble(double x) {
byte[] buf=new byte[8];
setDouble(x,buf,0);
return buf;
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。