`

StringUtil

 
阅读更多
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); 
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics