字符串、十六进制、byte数组互转
- import java.io.ByteArrayOutputStream;
- public class HexUtil {
- /**
- * @param args
- */
- public static void main(String[] args) {
- String str = "12345678";
- String hexStr = encode(str);
- System.out.println("字符串->十六进制:" + hexStr);
- System.out.println("十六进制->字符串:" + decode(hexStr));
- System.out.println("十六进制->byte数组:" + HexString2Bytes(hexStr));
- }
- public static String stringToHexString(String strPart) {
- String hexString = "";
- for (int i = 0; i < strPart.length(); i++) {
- int ch = (int) strPart.charAt(i);
- String strHex = Integer.toHexString(ch);
- hexString = hexString + strHex;
- }
- return hexString;
- }
- 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; i < bytes.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; i < bytes.length(); i += 2)
- baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
- .indexOf(bytes.charAt(i + 1))));
- return new String(baos.toByteArray());
- }
- private 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;
- }
- public static byte[] HexString2Bytes(String src) {
- byte[] ret = new byte[6];
- byte[] tmp = src.getBytes();
- for (int i = 0; i < 6; ++i) {
- ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
- }
- return ret;
- }
- }
字符串、十六进制、byte数组互转的更多相关文章
- Golang十六进制字符串和byte数组互转
Golang十六进制字符串和byte数组互转 需求 Golang十六进制字符串和byte数组互相转换,使用"encoding/hex"包 实现Demo package main i ...
- 二进制样式的字符串与byte数组互转函数示例
开发时用到的方法,记录下: /// <summary> /// 测试方法 /// </summary> private void TestFun() { Response.Wr ...
- 字符串与byte数组转换
string weclome=""; byte[] data = new byte[1024]; //字符串转byte数组 data = Encoding.ASCII.GetByt ...
- java中 16进制字符串 与普通字符串 与 byte数组 之间的转化
方法依赖commons-codec包 maven的引入方式如下 <dependency> <groupId>commons-codec</groupId> < ...
- 16进制字符串和byte数组进行相互转换\将10进制转换为任意进制
16进制字符串和byte数组进行相互转换 简介 1个byte对应8个bit,16进制使用4个bit,所以一个byte转成16进制,占用两位. JAVA代码 private static final c ...
- 字符串和byte数组的相互转化
关于byte[]数组转十六进制字符串: public static String getHexString(byte[] b) throws Exception { String result = & ...
- int跟byte[]数组互转的方法,整数 + 浮点型
整数: int转byte数组 public static byte[] intToBytes2(int n){ ]; ;i < ;i++) { b[i]=(-i*)); } return b; ...
- Java中字符串和byte数组之间的相互转换
1.将字符转换成byte数组 String str = "罗长"; byte[] sb = str.getBytes(); 2.将byte数组转换成字符 byte[] b={(by ...
- 图片和byte[]数组互转
一.图片转成byte[]数组. import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io ...
随机推荐
- Laravel_1 安装
1>http://www.golaravel.com/post/install-and-run-laravel-5-x-on-windows/ 2>http://www.golaravel ...
- 实习之vim基本学习
最近实习学到了写vim的基本用法,记录一下 批量注释 ctrl+v进入列模式,按“I”进入插入模式,按// #等在每行开头插入注释,esc 批量去除注释 ctrl + v 进入列模式,按“x”即可. ...
- Windows phone 之Interaction.Triggers的使用
两个步骤:1.添加以下两个程序集System.Windows.InteractivityMicrosoft.Expression.Interactions 2.添加xmlns:i="clr- ...
- Bad owner or permissions on .ssh/config
Bad owner or permissions on $HOME/.ssh/config The ssh with RHEL 4 is a lot more anal about security ...
- js 中特殊形势的函数-匿名函数的应用
javascript中的匿名函数,那什么叫做匿名函数? 匿名函数就是没有函数名称:演示代码: <script> function(x,y){ return x+y //这个就是一个匿名函数 ...
- SSO单点登录的实现原理
单点登录在现在的系统架构中广泛存在,他将多个子系统的认证体系打通,实现了一个入口多处使用,而在架构单点登录时,也会遇到一些小问题,在不同的应用环境中可以采用不同的单点登录实现方案来满足需求.我将以我所 ...
- HMM模型
通过前几时断续的学习,发现自己对HMM模型的了解还只停留在皮毛,导致在学习CRF模型并将其与最大熵模型.HMM.MEMM做比较时感觉很吃力,所以又花了两天时间使劲看了遍HMM,发现了解得确实深刻了很多 ...
- OC语言-02面向对象的三大特性
01封装 #import <Foundation/Foundation.h> @interface Student : NSObject { //@public 成员变量尽量不使用 int ...
- Word 2016 test
Word 2016 test
- Maven入门详解以及Eclisp的集成
1.首先要安装Maven到操作系统上 Maven的下载页面:http://maven.apache.org/download.html Maven跟Tomcat很像,下载下来后直接解压在指定的目录就安 ...