Java Hex Calculate toHexWithMarker(byte b)

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

Description

to Hex With Marker

License

Open Source License

Parameter

Parameter Description
b byte

Return

String with Marker '0x' ahead

Declaration

static public String toHexWithMarker(byte b) 

Method Source Code

//package com.java2s;
/**//from   w w  w .  j  a  v a  2s  .co  m
 * Copyright (C) 2008 ITS of ETH Zurich, Switzerland, Sarah Windler Burri
 * <p>
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * <p>
 * This program 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.
 * <p>
 * See the GNU Lesser General Public License for more details. You should have
 * received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * @param b byte
     * @return String with Marker '0x' ahead
     */
    static public String toHexWithMarker(byte b) {
        StringBuilder sb = new StringBuilder();
        sb.append("0x").append(toHex(b));
        return sb.toString();
    }

    /**
     * @param b byte
     * @return String
     */
    static public String toHex(byte b) {
        String st = Integer.toHexString(b);
        return (st.length() == 1) ? "0" + st : st;
    }

    /**
     * @param dst array of byte
     * @return String representation
     */
    static String toString(byte[] dst) {
        int l = dst.length;
        StringBuilder sb = new StringBuilder(l);
        for (int i = 0; i < l; i++) {
            int b = dst[i];
            int ival = ((int) b) & 0xff;
            char c = (char) ival;
            sb.append(c);
        }
        return sb.toString();
    }
}

Related

  1. toHexStringUpperCase(byte[] b)
  2. toHexTable(byte[] byteSrc, int lengthOfLine)
  3. toHexText(byte[] bytes, int length)
  4. toHexUtil(int n)
  5. toHexValue(int number)