Java Hex Calculate toHexString(byte b)

Here you can find the source of toHexString(byte b)

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(byte b) 

Method Source Code

//package com.java2s;
/*/*from   w w w.  j  a v a  2  s .c om*/
 * Copyright (c) 2010-2011 Brigham Young University
 * 
 * This file is part of the BYU RapidSmith Tools.
 * 
 * BYU RapidSmith Tools is free software: you may redistribute it 
 * and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 2 of 
 * the License, or (at your option) any later version.
 * 
 * BYU RapidSmith Tools is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 * 
 * A copy of the GNU General Public License is included with the BYU 
 * RapidSmith Tools. It can be found at doc/gpl2.txt. You may also 
 * get a copy of the license at <http://www.gnu.org/licenses/>.
 * 
 */

public class Main {
    public static String toHexString(byte b) {
        StringBuffer buf = new StringBuffer();
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int high = 0;
        int low = 0;

        high = ((b & 0xf0) >> 4);
        low = (b & 0x0f);
        buf.append(hexChars[high]);
        buf.append(hexChars[low]);

        return buf.toString();
    }

    public static String toHexString(byte[] block) {
        StringBuffer buf = new StringBuffer();
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int len = block.length;
        int high = 0;
        int low = 0;
        for (int i = 0; i < len; i++) {
            high = ((block[i] & 0xf0) >> 4);
            low = (block[i] & 0x0f);
            buf.append(hexChars[high]);
            buf.append(hexChars[low]);
        }
        return buf.toString();
    }

    public static String toHexString(int integer) {
        byte block[] = new byte[4];
        block[0] = (byte) ((integer >> 24) & 0x000000FF);
        block[1] = (byte) ((integer >> 16) & 0x000000FF);
        block[2] = (byte) ((integer >> 8) & 0x000000FF);
        block[3] = (byte) (integer & 0x000000FF);

        StringBuffer buf = new StringBuffer();
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int len = block.length;
        int high = 0;
        int low = 0;
        for (int i = 0; i < len; i++) {
            high = ((block[i] & 0xf0) >> 4);
            low = (block[i] & 0x0f);
            buf.append(hexChars[high]);
            buf.append(hexChars[low]);
        }
        return buf.toString();
    }

    /**
     * This changes a normal string into the hexadecimal string equivalent.  
     * It will add two hexadecimal characters for each character of the string.
     * @param s The string to be converted
     * @return The string s converted to hexadecimal
     */
    public static String toHexString(String s) {
        char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        StringBuffer sb = new StringBuffer(s.length() * 2);
        for (int i = 0; i < s.length(); i++) {
            sb.append(hexChar[(s.charAt(i) & 0xf0) >>> 4]);
            sb.append(hexChar[(s.charAt(i) & 0x0f)]);
        }

        return sb.toString();
    }
}

Related

  1. toHexStr(byte[] b)
  2. toHexStr(byte[] binary)
  3. toHexStream(byte[] bytes, String delimiter, boolean prefixEachValue, boolean upperCase)
  4. toHexString(byte a)
  5. toHexString(byte abyte0[], boolean spaceFlag)
  6. toHexString(byte b)
  7. toHexString(byte b)
  8. toHexString(byte b)
  9. toHexString(byte b)