屹砾

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

留言簿

积分与排名

ELi - 屹砾网络

JavaSE & JavaEE

JavaTesting

Linux & Unix

OpenSource

收藏链接

时事点评

阅读排行榜

评论排行榜

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

以下是一个将基本数据类型转换成十六进制字符串的源码。是不是有点与众不同呢?
package eli.encrypt;

/**
 * 将基本数据类型转换成十六进制字符串。
 * @author <a href="http://eli.blogjava.net/">屹砾</a><a
 *         href="mailto:eli.wuhan@gmail.com">邮箱</a>
 * @version created at: Jan 3, 2008 10:15:08 PM
 */

public class HexKit {
    public static char[] toHex(byte in) {
        char[] out = new char[2];
        int h = 0;
        h = in >> 4 & 0xF;
        out[0= (char) (h > 9 ? h + 0x37 : h + 0x30);
        h = in & 0xF;
        out[1= (char) (h > 9 ? h + 0x37 : h + 0x30);
        return out;
    }


    public static String toHex(byte[] in) {
        StringBuffer buf = new StringBuffer();
        for (byte i : in) {
            buf.append(toHex(i));
        }

        return buf.toString();
    }


    public static String toHex(short in) {
        byte[] b = new byte[2];
        for (int i = 0; i < 2; i++{
            b[i] = (byte) ((in >> (1 - i) * 8& 0xFF);
        }

        return toHex(b);
    }


    public static String toHex(short[] in) {
        StringBuffer buf = new StringBuffer();
        for (short i : in) {
            buf.append(toHex(i));
        }

        return buf.toString();
    }


    public static String toHex(int in) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++{
            b[i] = (byte) ((in >> (3 - i) * 8& 0xFF);
        }

        return toHex(b);
    }


    public static String toHex(int[] in) {
        StringBuffer buf = new StringBuffer();
        for (int i : in) {
            buf.append(toHex(i));
        }

        return buf.toString();
    }


    public static String toHex(long in) {
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i++{
            b[i] = (byte) ((in >> (7 - i) * 8& 0xFF);
        }

        return toHex(b);
    }


    public static String toHex(long[] in) {
        StringBuffer buf = new StringBuffer();
        for (long i : in) {
            buf.append(toHex(i));
        }

        return buf.toString();
    }

}

posted on 2008-01-03 22:28 屹砾 阅读(1225) 评论(0)  编辑  收藏 所属分类: JavaSE


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-06-16 22:40 编辑过
 
 
相关链接:
网站导航: