集册 Java实例教程 将字节数组转换为一个简明、可读的字符串,适合在*Stat类的to string方法中使用。

将字节数组转换为一个简明、可读的字符串,适合在*Stat类的to string方法中使用。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

472
将字节数组转换为一个简明、可读的字符串,适合在*Stat类的to string方法中使用。

/*-

 * See the file LICENSE for redistribution information.

 *

 * Copyright (c) 2001,2007 Oracle.  All rights reserved.

 *

 * $Id: DbUtil.java,v 12.5 2007/05/17 15:15:42 bostic Exp $

 */
 /**
 n  o  w  j  a  v  a . c o m 提供 
 **/

//package com.nowjava;


public class Main {

    /**

     *  Convert a byte array to a concise, readable string suitable

     *  for use in toString methods of the *Stat classes.

     *

     * @return    Description of the Return Value

     */

    public static String byteArrayToString(byte[] barr) {

        if (barr == null) {

            return "null";

        }


        StringBuffer sb = new StringBuffer();

        int len = barr.length;/* 来自 nowjava.com*/

        for (int i = 0; i < len; i++) {

            sb.append('x');

            int val = (barr[i] >> 4) & 0xf;

            if (val < 10) {

                sb.append((char) ('0' + val));

            } else {

                sb.append((char) ('a' + val - 10));

            }

            val = barr[i] & 0xf;

            if (val < 10) {

                sb.append((
展开阅读全文