package com.cici.util;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
public class ConvertUtil {
/*Convert Hex String to byte[]*/
public static byte[] hexStringToBytes(String hex){
int len = (hex.length()/2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for(int i=0;i<len;i++){
int pos= i*2;
result[i] = (byte)(toByte(achar[pos])<<4 | toByte(achar[pos+1]));
}
return result;
}
private static int toByte(char c){
byte b = (byte)"0123456789ABCDEF".indexOf(c, 0);
return b;
}
/*Convert byte to Hex String*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/* byte -22 ---> int 234*/
public static int convertByte(byte b){
byte[] bytes = new byte[]{b};
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
int result = in.read();
// Binary bit Integer.toBinaryString(result);
return result;
}
/*invert char array*/
public static char[] invertCharArray(char[] chrs){
char[] chrsNew = new char[chrs.length];
for(int i=0;i<chrs.length;i++){
Character c = new Character(chrs[i]);
chrsNew[chrs.length-i-1] = c;
}
return chrsNew;
}
// calculate power of 2
public static int powerOf2(int powerNum){
int result =1;
for(int i=0;i<powerNum;i++){
result*=2;
}
return result;
}
//Example: "11100001" ==> 0xE1
public static String fromBits(String bitString){
char[] chrs = invertCharArray(bitString.toCharArray());
Integer resultInt =0;
for(int i=0;i<chrs.length;i++){
Integer in = new Integer(Character.getNumericValue(chrs[i]));
resultInt += in*powerOf2(i);
}
String s = Integer.toHexString(resultInt);
return s;
}
/* @Param : List<String>
* @Function : Convert from List to Array
* @Return : String[]
* */
public static String[] convertStringList(List<String> stringList){
String[] strs = new String[stringList.size()];
for(int i =0 ;i<stringList.size();i++){
String temp = "";
temp = stringList.get(i);
strs[i] = temp;
}
return strs;
}
// convert byte to standard hexString
public static String byteToBinayString(byte b) {
String binaryStr = "0000000" + Integer.toBinaryString(b);
binaryStr = binaryStr.substring(binaryStr.length() - 8, binaryStr.length());
return binaryStr;
}
public static void main(String[] args){
byte[] b = ConvertUtil.hexStringToBytes("EAFF");
for(byte bb :b){
System.out.println(bb);
}
}
}
package com.cici.util;
import java.io.ByteArrayInputStream;
import java.util.List;
public class ConvertUtility {
/*Convert Hex String to byte*/
public static byte hexStringToByte(String str){
Integer i = Integer.parseInt(str, 16);
return i.byteValue();
}
/*Convert byte to Hex String*/
public static String byteToHexString(byte src){
Byte b = new Byte(src);
Integer i = b.intValue();
return Integer.toHexString(i);
}
// convert byte to 8bit BinaryString
public static String byteToBinayString(byte b) {
String binaryStr = "0000000" + Integer.toBinaryString(b);
binaryStr = binaryStr.substring(binaryStr.length() - 8, binaryStr.length());
return binaryStr;
}
/* byte -22 ---> int 234*/
public static int convertByte(byte b){
byte[] bytes = new byte[]{b};
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
int result = in.read();
// Binary bit Integer.toBinaryString(result);
return result;
}
//Example: "11100001" ==> 0xE1
public static String fromBits(String bitString){
char[] chrs = invertCharArray(bitString.toCharArray());
Integer resultInt =0;
for(int i=0;i<chrs.length;i++){
Integer in = new Integer(Character.getNumericValue(chrs[i]));
resultInt += in*powerOf2(i);
}
String s = Integer.toHexString(resultInt);
return s;
}
/*invert char array*/
public static char[] invertCharArray(char[] chrs){
char[] chrsNew = new char[chrs.length];
for(int i=0;i<chrs.length;i++){
Character c = new Character(chrs[i]);
chrsNew[chrs.length-i-1] = c;
}
return chrsNew;
}
// calculate power of 2
public static int powerOf2(int powerNum){
int result =1;
for(int i=0;i<powerNum;i++){
result*=2;
}
return result;
}
/* @Param : List<String>
* @Function : Convert from List to Array
* @Return : String[]
* */
public static String[] convertStringList(List<String> stringList){
String[] strs = new String[stringList.size()];
for(int i =0 ;i<stringList.size();i++){
String temp = "";
temp = stringList.get(i);
strs[i] = temp;
}
return strs;
}
public static void main(String[] args){
/*Byte b =11;
String s = ConvertUtility.byteToBinayString(b);
String ss = Integer.toBinaryString(11);
System.out.println(s);
System.out.println(ss);*/
System.out.println(ConvertUtility.convertByte((byte) -22));
}
}
/*删除字符串末尾的空格*/
package com.cici.util;
public class StringUtil {
public static void main(String[] args) {
}
public static String deleTheEndSpace(String str){
char[] chr = str.toCharArray();
int i =0;
for(i=chr.length-1;i>=0;i--){
if(chr[i]!=' ') break;
}
str = str.substring(0,i+1);
System.out.println(str.length()+" "+str);
return str;
}
}
/* 测试类*/
package com.cici.util;
public class Test {
public static void main(String[] args) {
/*Hex String --- > Byte*/
Integer i = Integer.parseInt("EA",16);
System.out.println(i);
Byte b = i.byteValue();
System.out.println(b);
/*Bye --- > Hex String*/
Byte bb = -22;
Integer ii = bb.intValue();
String newII = Integer.toHexString(ii).substring(6, 8);
System.out.println(newII);
//public static Byte decode(String nm)
String i = Integer.toBinaryString(0x1);
System.out.println(i);
}
}
分享到:
相关推荐
StringUtil工具类java学习的好东西!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...
首先,`StringUtil.cpp`和`StringUtil.h`是C++中的源代码文件和头文件,它们定义了类`StringUtil`及其成员函数。在C++中,头文件通常用于声明类、函数、常量等,而源代码文件则包含了这些声明的实现细节。在这个案例...
`StringUtil`是一个常见的Java工具类,它包含了大量用于处理字符串的方法,可以极大地简化字符串操作,提高代码的可读性和效率。在Java开发中,我们经常会遇到对字符串进行各种操作的需求,如检查空值、分割、连接、...
通过分析`stringUtil.c`源代码和`stringUtil.h`头文件,我们可以了解一些关键知识点。 1. **自定义字符串数据结构**:在C语言中,字符串通常以字符数组的形式存在。在`stringUtil.h`中,可能定义了一个名为`String`...
StringUtil.java 2100+行代码 内容丰富
自己整理的StringUtil ,字符串处理工具类,很全面的方法,对象之间的数据转换
《AS3-StringUtil:深入解析字符串处理工具类》 在ActionScript 3(AS3)中,处理字符串是一项常见的任务,而`StringUtil`类则是一个非常实用的工具,它提供了许多方便的方法来优化和简化字符串操作。这篇内容将...
StringUtil工具类 是对String 对象封装操作
这里提到的"ArrayUtil+DateUtil+FileUtil+ReguUtil+StringUtil"是五个这样的工具包,它们分别针对数组操作、日期处理、文件操作、正则表达式匹配和字符串操作提供了一系列便利的方法。 1. **ArrayUtil**: - **...