package learn.app.bitoperation;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
public class SixteenBitToTenBit {
public static void main(String[] args) {
System.out.println(hexStringToInt("FF"));
System.out.println(computeTC(1.6));
}
/**
*
* 十六进制 字符串转整型
* @param hexString
* @return
*/
public static int hexStringToInt(String hexString){
int decimalNumber = 0;
for (int i = 0; i < hexString.length(); i++) {
char c = hexString.charAt(i);
int digit;
if (c >= '0' && c <= '9') {
digit = c - '0';
} else if (c >= 'A' && c <= 'F') {
digit = c - 'A' + 10;
} else if (c >= 'a' && c <= 'f') {
digit = c - 'a' + 10;
} else {
throw new IllegalArgumentException("Invalid hex string: " + hexString);
}
decimalNumber = (decimalNumber << 4) | digit;
}
return decimalNumber;
}
/**
* 计算 补码
* 计算方法: 位取反后 + 1
* @param value 源码对象
* @return
*/
public static String computeTC(Object value){
if (value == null){
return null;
}
if (value instanceof Integer v){
return String.valueOf(~v + 1);
}else if (value instanceof Long v){
return String.valueOf(~v + 1L);
}else if (value instanceof Float v){
return String.valueOf(v*(-1f) + 1f);
}else if (value instanceof Double v){
return String.valueOf(v*(-1d) + 1d);
}else {
return null;
}
}
/**
* 16进制编码数字工具类
*<p>Title: HexUtils</p>
*<p>Description: </p>
*@author yuhy
*@date 2019年6月26日
*/
public static class HexUtils {
/**
* 16进制数字字符集
*/
private static final String hexString="0123456789ABCDEF";
/**
* 转化字符串为十六进制编码
*/
public static String toHexString(String s){
StringBuilder str= new StringBuilder();
for (int i=0;i<s.length();i++){
int ch = s.charAt(i);
String s4 = Integer.toHexString(ch);
str.append(s4);
}
return str.toString();
}
/**
* 转化十六进制编码为字符串
*/
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, StandardCharsets.UTF_8);//UTF-16le:Not
}catch (Exception e1){
e1.printStackTrace();
}
return s;
}
/**
* 将字符串编码成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)));
//sb.append(hexString.charAt((bytes[i] & 0b11110000) >> 4));
//sb.append(hexString.charAt((bytes[i] & 0b00001111)));
}
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 baoS.toString();
}
/**
* 将字符串转换成16进制编码字符串
*<p>Title: ToHexString</p>
*<p>Description: </p>
*@param str
*@return
*/
public static String ToHexString(String str) {
StringBuilder str1 = new StringBuilder();
try {
byte[] b = str.getBytes("gbk");
int i = 0;
int max = b.length;
for (; i < max; i++) {
str1.append(Integer.toHexString(b[i] & 0xFF));
}
} catch (UnsupportedEncodingException e) {
System.out.println("异常信息ToHexString" + e.getMessage());
}
return str1.toString();
}
/**
* 16进制编码字符串转字节数组
*<p>Title: toByteArray</p>
*<p>Description: </p>
*@param hexString
*@return
*/
public static byte[] toByteArray(String hexString) {
hexString = hexString.toLowerCase();
final byte[] byteArray = new byte[hexString.length() / 2];
int k = 0;
for (int i = 0; i < byteArray.length; i++) {// 因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
byteArray[i] = (byte) (high << 4 | low);
k += 2;
}
return byteArray;
}
/**
* 字节数组转16进制编码字符串
*<p>Title: toHexString</p>
*<p>Description: </p>
*@param byteArray
*@return
*/
public static String toHexString(byte[] byteArray) {
String str = null;
if (byteArray != null && byteArray.length > 0) {
StringBuilder stringBuffer = new StringBuilder(byteArray.length);
for (byte byteChar : byteArray) {
stringBuffer.append(String.format("%02X", byteChar));
}
str = stringBuffer.toString();
}
return str;
}
/**
* 字节数组转字符串
*<p>Title: ByteArrayDecode</p>
*<p>Description: </p>
*@param bytes
*@return
*/
public static String ByteArray2String(byte[] bytes) {
String hexStr = toHexString(bytes);
return decode(hexStr);
}
public static void main(String[] args) {
String str = "你好asbc121f,smf,m,ewflwf健康的";
String sljzstr = encode(str);
System.out.println("字符串转16进制数字____"+sljzstr);
System.out.println("16进制数字转成字符串____"+decode(sljzstr));
System.out.println("字节数组转字符串:"+ByteArray2String(str.getBytes()));
//int a = 1<<4 | 2;
int a = 0b0001 << 4 | 2;//0001,0000
int b1 = 0b00000000000000000000000000000001 | 0b00000000000000000000000000000010;
int b = 1 | 0b0010;
System.out.println("a="+a);
System.out.println(("b="+b));
System.out.println("0b10000=" + 0b10000);
System.out.println("b1 = " + b1);
System.out.println(0x0f);
System.out.println(0xf0);
System.out.println(0b11110000);
String encode15 = encode("15");
System.out.println("encode('15')=" + encode15);
System.out.println("encode('15')decode后="+ decode(encode15));
}
}
}
java 16进制编码字符串与字符串转换方法总结
于 2019-06-27 11:22:48 首次发布

852

被折叠的 条评论
为什么被折叠?



