屹砾

屹砾技术博客,记录生活点滴。
Email/QQ/MSN/GTalk: eli.wuhan@gmail.com

留言簿

积分与排名

Growing & Life

JavaSE & JavaEE

Linux & Unix

时事点评

阅读排行榜

评论排行榜

二写:将基本数据类型转换成十六进制字符串

之前就写了一个将基本数据类型转换成十六进制字符串的工具类,回头看了一下感觉有点复杂,今天写了一个易懂的,而且性能也不错。
package eli.util;

/**
 * 将数字转换成十六进制数字的工具类。
 * <p>
 * 相比{
@link HexUtil}的代码,这个类更易于理解,性能差异却很小。
 * 
 * 
@author <a href="mailto:eli.wuhan@gmail.com">屹砾</a>
 
*/

public class HexConvert {
    
protected static final char[] CHAR = '0''1''2''3''4''5''6',
            
'7''8''9''A''B''C''D''E''F' }
;

    
protected static String toHex(long value, int bytes) {
        
int max = (bytes << 1- 1;
        
char[] chars = new char[bytes << 1];
        
for (int i = (bytes << 1- 1; i >= 0; i--{
            chars[max 
- i] = CHAR[(int) ((value >> (4 * i)) & 0xF)];
        }

        
return new String(chars);
    }


    
public static String toHex(byte value) {
        
return toHex(value, 1);
    }


    
public static String toHex(char value) {
        
return toHex(value, 2);
    }


    
public static String toHex(short value) {
        
return toHex(value, 2);
    }


    
public static String toHex(int value) {
        
return toHex(value, 4);
    }


    
public static String toHex(long value) {
        
return toHex(value, 8);
    }


    
public static String toHex(byte[] value) {
        StringBuffer buf 
= new StringBuffer();
        
for (int i = 0; i < value.length; i++{
            buf.append(toHex(value[i]));
        }

        
return buf.toString();
    }


    
public static String toHex(char[] value) {
        StringBuffer buf 
= new StringBuffer();
        
for (int i = 0; i < value.length; i++{
            buf.append(toHex(value[i]));
        }

        
return buf.toString();
    }


}

posted on 2008-06-23 20:50 屹砾 阅读(45) 评论(0)  编辑  收藏 所属分类: JavaSE


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 
相关链接:
网站导航: