代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
一个处理整数的类
作者:
/ 发布于
2011/1/3
/
374
处理整数
import java.io.*; import java.nio.*; public final class IntegerUtil { private static final String digits = "0123456789abcdef"; /** * converts byte array to integer. byte array shall be of size 4 (32 bit) */ public static final int getInt(byte[] bytes) throws IOException { if(bytes==null || bytes.length!=4) { throw new IllegalArgumentException("Illegal Byte Array"); } ByteBuffer buf = ByteBuffer.allocate(bytes.length); buf=buf.put(bytes); return buf.getInt(0); } /** * return integer array view for a given byte array. If byte array length is not divisible by 4 then there is possibility of loosing last bytes. * for e.g if byte array size is 5 then it ignores 5th byte in getting integer array. */ public static int[] getIntegerArray(byte[] bytes) throws IOException { if(bytes==null) { throw new IllegalArgumentException("Illegal Byte Array"); } ByteBuffer buf = ByteBuffer.allocate(bytes.length); buf=buf.put(bytes); buf=(ByteBuffer)buf.rewind(); IntBuffer intBuffer=buf.asIntBuffer(); intBuffer=(IntBuffer)intBuffer.rewind(); int []array=new int[bytes.length/4]; for(int i=0;i<array.length;i++) { array[i]=intBuffer.get(i); } return array; } /** * gets the Integer from ByteBuffer. ByteBuffer shall contain only 4 bytes */ public static final int getInt(ByteBuffer buf) throws IOException { if(buf==null || buf.array().length!=4) { throw new IllegalArgumentException("Illegal Byte Buffer"); } return buf.getInt(0); } /** * return a byte value for a given position from integer */ public static final byte getByte(int i, int pos) throws IOException { if(pos<0 || pos>3) { throw new IllegalArgumentException("Illegal Position "+pos); } return getBytes(i)[pos]; } /** * converts integer to byte array */ public static final byte[] getBytes(int i) throws IOException { return getByteBuffer(i).array(); } /** * Creates ByteBuffer Wrapper for a given integer */ public static final ByteBuffer getByteBuffer(int i) throws IOException { ByteBuffer buf = ByteBuffer.allocate(4); buf=buf.putInt(i); return buf; } /** * Creates ByteBuffer Wrapper for a given integer. ByteBuffer would be either direct or non-direct based on the flag */ public static final ByteBuffer getByteBuffer(int i, boolean bDirectBuffer) throws IOException { ByteBuffer buf = null; if(bDirectBuffer==false) { buf=ByteBuffer.allocate(4); } else { buf=ByteBuffer.allocateDirect(4); } buf=buf.putInt(i); return buf; } /** * returns byte array as ByteBuffer. byte array size shall be 4 (32 bit) */ public static final ByteBuffer getByteBuffer(byte[] bytes) throws IOException { if(bytes==null || bytes.length!=4) { throw new IllegalArgumentException("Illegal Byte Array"); } return ByteBuffer.wrap(bytes); } /** * Converts integer to Hexa-Decimal String */ public static final String intToHex(int i) { return Integer.toHexString(i); } /** * Converts integer to binary String */ public static final String intToBinary(int i) { return Integer.toBinaryString(i); } /** * Converts integer to Octal String */ public static final String intToOctal(int i) { return Integer.toOctalString(i); } /** * Converts Hexa-Decimal String to integer */ public static final int hexToInt(String s) { return Integer.parseInt(s,16); } /** * Converts Binary String to integer */ public static final int binaryToInt(String s) { return Integer.parseInt(s,2); } /** * Converts Octal String to integer */ public static final int octalToInt(String s) { return Integer.parseInt(s,8); } /** * Converts byte array to Hexa-Decimal String */ public static final String toHex(byte[] data) throws IOException { if(data.length!=4) { throw new IllegalArgumentException("Illegal Byte Array"); } return intToHex(getInt(data)); } /** * returns the hexa-decimal string for a given ByteBuffer */ public static final String toHex(ByteBuffer buffer) throws IOException { if(buffer==null || buffer.array().length!=4) { throw new IllegalArgumentException("Illegal Byte Buffer"); } return intToHex(getInt(buffer)); } /** * Converts byte array to Hexa-Decimal String */ public static final String toHex(byte[] data, int length) { if(length!=4) { throw new IllegalArgumentException("Illegal Length"); } StringBuffer buf = new StringBuffer(); for (int i = 0; i != length; i++) { int v = data[i] & 0xff; buf.append(digits.charAt(v >> 4)); buf.append(digits.charAt(v & 0xf)); } return buf.toString(); } /** * Converts integer to byte array */ public static final byte[] intToByteArray(int value) { return new byte[] { (byte)(value >>> 24), (byte)(value >>> 16), (byte)(value >>> 8), (byte)value}; } /** * Converts byte array to integer */ public static final int byteArrayToInt(byte [] b) { return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF); } public static int hex2decimal(String s) { String digits = "0123456789ABCDEF"; s = s.toUpperCase(); int val = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); int d = digits.indexOf(c); val = 16*val + d; } return val; } // precondition: d is a nonnegative integer public static String decimal2hex(int d) { String digits = "0123456789ABCDEF"; if (d == 0) return "0"; String hex = ""; while (d > 0) { int digit = d % 16; // rightmost digit hex = digits.charAt(digit) + hex; // string concatenation d = d / 16; } return hex; } public static String hexToString(String input, int groupLength) { StringBuilder sb = new StringBuilder(input.length() / groupLength); for (int i = 0; i < input.length() - groupLength + 1; i += groupLength) { String hex = input.substring(i, i + groupLength); sb.append((char) Integer.parseInt(hex, 16)); } return sb.toString(); } } </div>
试试其它关键字
处理整数
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3