JAVA 十六进制与字符串的转换

2016-02-19 11:42 97 1 收藏

每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注图老师可以让你学习更多的好东西,下面为大家推荐JAVA 十六进制与字符串的转换,赶紧看过来吧!

【 tulaoshi.com - 编程语言 】

toHexString
public static String toHexString(int i)以十六进制的无符号整数形式返回一个整数参数的字符串表示形式。
如果参数为负,那么无符号整数值为参数加上 232;否则等于该参数。将该值转换为十六进制(基数 16)的无前导 0 的 ASCII 数字字符串。如果无符号数的大小值为零,则用一个零字符 '0' ('u0030') 表示它;否则,无符号数大小的表示形式中的第一个字符将不是零字符。用以下字符作为十六进制数字:
0123456789abcdef
这些字符的范围是从 'u0030' 到 'u0039' 和从 'u0061' 到 'u0066'。如果希望得到大写字母,可以在结果上调用 String.toUpperCase() 方法:
Integer.toHexString(n).toUpperCase()
参数:
i - 要转换成字符串的整数。
返回:
用十六进制(基数 16)参数表示的无符号整数值的字符串表示形式。
// 转化字符串为十六进制编码
public static String toHexString(String s)
{
String str="";
for (int i=0;is.length();i++)
{
int ch = (int)s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
}
// 转化十六进制编码为字符串
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i baKeyword.length; i++)
{
try
{
baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
s = new String(baKeyword, "utf-8");//UTF-16le:Not
}
catch (Exception e1)
{
e1.printStackTrace();
}
return s;
}
// 转化十六进制编码为字符串
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i baKeyword.length; i++)
{
try
{
baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
s = new String(baKeyword, "utf-8");//UTF-16le:Not
}
catch (Exception e1)
{
e1.printStackTrace();
}
return s;
}
public static void main(String[] args) {
System.out.println(encode("中文"));
System.out.println(decode(encode("中文")));
}
/*
* 16进制数字字符集
*/
private static String hexString="0123456789ABCDEF";
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(String str)
{
//根据默认编码获取字节数组
byte[] bytes=str.getBytes();
StringBuilder sb=new StringBuilder(bytes.length*2);
//将字节数组中每个字节拆解成2位16进制整数
for(int i=0;ibytes.length;i++)
{
sb.append(hexString.charAt((bytes[i]&0xf0)4));
sb.append(hexString.charAt((bytes[i]&0x0f)0));
}
return sb.toString();
}
/*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static String decode(String bytes)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
//将每2位16进制整数组装成一个字节
for(int i=0;ibytes.length();i+=2)
baos.write((hexString.indexOf(bytes.charAt(i))4 |hexString.indexOf(bytes.charAt(i+1))));
return new String(baos.toByteArray());
}
第二种方法:
将指定byte数组以16进制的形式打印到控制台
代码如下:

package com.nantian.iclient.atm.sdb;

public class Util {
public Util() {
}

/**
* 将指定byte数组以16进制的形式打印到控制台
* @param hint String
* @param b byte[]
* @return void
*/
public static void printHexString(String hint, byte[] b) {
System.out.print(hint);
for (int i = 0; i b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase() + " ");
}
System.out.println("");
}

/**
*
* @param b byte[]
* @return String
*/
public static String Bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}

/**
* 将两个ASCII字符合成一个字节;
* 如:"EF"-- 0xEF
* @param src0 byte
* @param src1 byte
* @return byte
*/
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
_b0 = (byte)(_b0 4);
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
byte ret = (byte)(_b0 ^ _b1);
return ret;
}

/**
* 将指定字符串src,以每两个字符分割转换为16进制形式
* 如:"2B44EFD9" -- byte[]{0x2B, 0x44, 0xEF, 0xD9}
* @param src String
* @return byte[]
*/
public static byte[] HexString2Bytes(String src){
byte[] ret = new byte[8];
byte[] tmp = src.getBytes();
for(int i=0; i8; i++){
ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
}
return ret;
}

}

来源:https://www.tulaoshi.com/n/20160219/1598324.html

延伸阅读
1.创建对象   对于java程序中的 字符串直接常量 ,JVM会使用一个字符串池来保存它们。当第一次使用某个字符串直接常量时,JVM会将它放入字符串池中进行缓存。在一般情况下,字符串池中的字符串对象不会被垃圾回收。当程序再次需要使用该字符串时,无需重新创建一个新的字符串就可以直接让引用变量直接指向字符串中已有的字符串。而 使用n...
代码如下: package com.gpdi.action; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class WordsStatistics {     class Obj {         int count ;     ...
题目:定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。如把字符串abcdef左旋转2位得到字符串cdefab。请实现字符串左旋转的函数。要求时间对长度为n的字符串操作的复杂度为O(n),辅助内存为O(1)。 分析:如果不考虑时间和空间复杂度的限制,最简单的方法莫过于把这道题看成是把字符串分成前后两部分,通过旋转操作把这...
(1).String 和StringBuffer对象的创建! (2).String类的常用方法:       length(), charAt(), getchars(), equals(),starsWith(), endsWith(), indexOf(), laseIndex(), substring(),contact()       replace(), valueOf(). (3).StringBuffer类的常用方法: &nbs...
标签: Web开发
代码如下: /** * 截取字符串 len为字节长度 * @param str * @param len * @return * @throws UnsupportedEncodingException */ public static String getLimitLengthString( String str,int len){ try{ int counterOfDoubleByte = 0; byte[] b = str.getBytes("gb2312"); if(b.length = len) return str; for(int i = 0; i len...

经验教程

406

收藏

29
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部