字符串,int,十六进制间转换
public class TypeConvert
{
. /* 字符串转byte[]
03. 这个方法转换后的结果是会多一些 48字符进来的就是代表的是0不知道为什么,但是可以只是取出指定的字符串就行了
04. */
. public static byte[] hexStringToBytes(String hexString)
{
if (hexString == null || hexString.equals(""))
{
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
.
. /* byte转short */
. public final static short getShort(byte[] buf, boolean asc, int len) {
. short r = ;
. if (asc)
. for (int i = len - ; i >= ; i--) {
. r <<= ;
. r |= (buf[i] & 0x00ff);
. }
. else
. for (int i = ; i < len; i++) {
. r <<= ;
. r |= (buf[i] & 0x00ff);
. }
.
. return r;
. }
.
. /* B2 -> 0xB2 */
. public static int stringToByte(String in, byte[] b) throws Exception {
. if (b.length < in.length() / ) {
. throw new Exception("byte array too small");
. }
.
. int j=;
. StringBuffer buf = new StringBuffer();
. for (int i=; i<in.length(); i++, j++) {
. buf.insert(, in.charAt(i));
. buf.insert(, in.charAt(i+));
. int t = Integer.parseInt(buf.toString(),);
. System.out.println("byte hex value:" + t);
. b[j] = (byte)t;
. i++;
. buf.delete(,);
. }
.
. return j;
. }
.
. /* byte to int */
. public final static int getInt(byte[] buf, boolean asc, int len) {
. if (buf == null) {
. throw new IllegalArgumentException("byte array is null!");
. }
. if (len > ) {
. throw new IllegalArgumentException("byte array size > 4 !");
. }
. int r = ;
. if (asc)
. for (int i = len - ; i >= ; i--) {
. r <<= ;
. r |= (buf[i] & 0x000000ff);
. }
. else
. for (int i = ; i < len; i++) {
. r <<= ;
. r |= (buf[i] & 0x000000ff);
. }
. return r;
. }
.
. /* int -> byte[] */
. public static byte[] intToBytes(int num) {
. byte[] b = new byte[];
. for (int i = ; i < ; i++) {
. b[i] = (byte) (num >>> ( - i * ));
. }
.
. return b;
. }
.
. /* short to byte[] */
. public static byte[] shortToBytes(short num) {
. byte[] b = new byte[];
.
. for (int i = ; i < ; i++) {
. b[i] = (byte) (num >>> (i * ));
. }
.
. return b;
. }
.
. /* byte to String */
. private static char findHex(byte b) {
. int t = new Byte(b).intValue();
. t = t < ? t + : t;
.
. if (( <= t) &&(t <= )) {
. return (char)(t + '');
. }
.
. return (char)(t-+'A');
. }
. public static String byteToString(byte b) {
. byte high, low;
. byte maskHigh = (byte)0xf0;
. byte maskLow = 0x0f;
.
. high = (byte)((b & maskHigh) >> );
. low = (byte)(b & maskLow);
.
. StringBuffer buf = new StringBuffer();
. buf.append(findHex(high));
. buf.append(findHex(low));
.
. return buf.toString();
. }
.
. /* short -> byte */
. public final static byte[] getBytes(short s, boolean asc) {
. byte[] buf = new byte[];
. if (asc) for (int i = buf.length - ; i >= ; i--) { buf[i] = (byte) (s & 0x00ff);
. s >>= ;
. }
. else
. for (int i = ; i < buf.length; i++) {
. buf[i] = (byte) (s & 0x00ff);
. s >>= ;
. }
. return buf;
. }
. /* int -> byte[] */
. public final static byte[] getBytes(int s, boolean asc) {
. byte[] buf = new byte[];
. if (asc)
. for (int i = buf.length - ; i >= ; i--) {
. buf[i] = (byte) (s & 0x000000ff);
. s >>= ;
. }
. else
. for (int i = ; i < buf.length; i++) {
. buf[i] = (byte) (s & 0x000000ff);
. s >>= ;
. }
. return buf;
. }
.
. /* long -> byte[] */
. public final static byte[] getBytes(long s, boolean asc) {
. byte[] buf = new byte[];
. if (asc)
. for (int i = buf.length - ; i >= ; i--) {
. buf[i] = (byte) (s & 0x00000000000000ff);
. s >>= ;
. }
. else
. for (int i = ; i < buf.length; i++) {
. buf[i] = (byte) (s & 0x00000000000000ff);
. s >>= ;
. }
. return buf;
. }
.
. /* byte[]->int */
. public final static int getInt(byte[] buf, boolean asc) {
. if (buf == null) {
. throw new IllegalArgumentException("byte array is null!");
. }
. if (buf.length > ) {
. throw new IllegalArgumentException("byte array size > 4 !");
. }
. int r = ;
. if (asc)
. for (int i = buf.length - ; i >= ; i--) {
. r <<= ;
. r |= (buf[i] & 0x000000ff);
. }
. else
. for (int i = ; i < buf.length; i++) {
. r <<= ;
. r |= (buf[i] & 0x000000ff);
. }
. return r;
. }
. /* byte[] -> long */
. public final static long getLong(byte[] buf, boolean asc) {
. if (buf == null) {
. throw new IllegalArgumentException("byte array is null!");
. }
. if (buf.length > ) {
. throw new IllegalArgumentException("byte array size > 8 !");
. }
. long r = ;
. if (asc)
. for (int i = buf.length - ; i >= ; i--) {
. r <<= ;
. r |= (buf[i] & 0x00000000000000ff);
. }
. else
. for (int i = ; i < buf.length; i++) {
. r <<= ;
. r |= (buf[i] & 0x00000000000000ff);
. }
. return r;
. }
.}
字符串,int,十六进制间转换的更多相关文章
- matlab学习笔记10_6 字符串与数值间的转换以及进制之间的转换
一起来学matlab-matlab学习笔记10 10_6 字符串与数值间的转换以及进制之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合 ...
- C/C++ 字符、字符串转十六进制(支持中文字符串转换)
#include <string> // std::string #include <sstream> // std::stringstream /** * #purpose ...
- 如何将int整型转换成String字符串类型
自动类型转换适用于兼容类型之间从小范围到大范围数据的转换. nt转换成String int i = 10; int b=1: System.out.pritnln(a + b); 里面靠近字符串,所以 ...
- 将int型数字转换成6位字符串,不足的时候,前面补0
将int型数字转换成6位字符串,不足的时候,前面补0 方法一: int num = 123; num.ToString("000000"); 方法二: int num = 123; ...
- 38th 字符串与 列表间的转换
字符串与 列表间的转换 如何利用字符串 'Life is short ,I use python'输出 :'python use I, short is Life' s = 'Life is shor ...
- C语言字符串和十六进制的相互转换方式
C语言的字符串操作并不像java,Csharp那样提供直接的方法,简单粗暴.所以,在转换的时候往往费力费时,近日做项目正好用到和java程序通讯,java发送过来的数据是十六进制数字组成的字符串,解析 ...
- Js字符串与十六进制的相互转换
开发过程中,字符串与十六进.二进制之间的相互转换常常会用到,尤其是涉及到中文的加密时,就需要把中文转换为十六进制.下面说说具体的转换方法. 1.字符串转换为十六进制 主要使用 charCodeAt() ...
- IP地址字符串与BigInteger的转换
/** * Copyright (c) 2010, 新浪网支付中心 * All rights reserved. * * Java IP地址字符串与BigInteger的转换, * ...
- 字符串、十六进制、byte数组互转
import java.io.ByteArrayOutputStream; public class HexUtil { /** * @param args */ public static void ...
随机推荐
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
- linux maven安装配置
1.Run the wget command from the dir you want to extract maven too. wget http://mirrors.cnnic.cn/apac ...
- 2015 "BestCoder Cup" Champion
这场比赛我没有参加,不过就算参加了也估计是被完虐.于是看着题解把大部分题目都搞了一遍. T1:Movie(hdu 5214) 题目大意: 给出N个区间,问能否选出3个互不相交的区间. N<=10 ...
- Rhel6-piranha配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...
- blocked file type by sharepoint 分类: Sharepoint 2015-07-05 07:45 6人阅读 评论(0) 收藏
o add or remove blocked file types by using Central Administration Verify that you have the followin ...
- MS Sql server 2008 学习笔记
数据库中常用的概念 Sql本身是一个服务器,没有界面,Management Studio 只是一个SQL Server管理工具而已,不是服务器. Sql server 在管理工具下面的服务SQL S ...
- Hinet 日本数据处理流程
---恢复内容开始--- 推荐网站: http://ju.outofmemory.cn/entry/138571 ridnet.py 将Hinet 的cnt 数据提取为sac数据,参考网站 http: ...
- Java中多态、抽象类和接口
1:final关键字(掌握) (1)是最终的意思,可以修饰类,方法,变量. (2)特点: A:它修饰的类,不能被继承. B:它修饰的方法,不能被重写. C:它修饰的变量,是一个常量. (3)面试相关: ...
- Java中二维数组与面向对象
1:二维数组(理解) (1)元素是一维数组的数组. (2)格式: A:数据类型[][] 数组名 = new 数据类型[m][n]; B:数据类型[][] 数组名 = new 数据类型[m][]; C: ...
- JDBCl链接中Statement
作用:创建的Statement对象执行SQL语句 (1)对象有Connection对象调用createStatement()方法创建 (2)有Statement对象调用executeUpdate()方 ...