Java Hex Calculate toHex(byte[] buffer)

Here you can find the source of toHex(byte[] buffer)

Description

Converts an array of bytes into a String representing each byte as an unsigned hex number.

License

Apache License

Parameter

Parameter Description
buffer array of bytes to convert

Return

generated hex string

Declaration

private static String toHex(byte[] buffer) 

Method Source Code

//package com.java2s;
/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//from   w  w  w.j a  v  a2  s  .c  om
 */

public class Main {
    /**
     * Converts an array of bytes into a String representing each byte as an
     * unsigned hex number.
     * 
     * @param buffer array of bytes to convert
     * @return generated hex string
     */
    private static String toHex(byte[] buffer) {
        StringBuffer sb = new StringBuffer();
        String s = null;
        for (int i = 0; i < buffer.length; i++) {
            s = Integer.toHexString((int) buffer[i] & 0xff);
            if (s.length() < 2) {
                sb.append('0');
            }
            sb.append(s);
        }
        return sb.toString();
    }
}

Related

  1. toHex(byte[] b)
  2. toHex(byte[] b)
  3. toHex(byte[] b)
  4. toHex(byte[] b, int off, int len, String separator)
  5. toHEX(byte[] ba)
  6. toHex(byte[] buffer)
  7. toHex(byte[] bytes)
  8. toHex(byte[] bytes)
  9. toHex(byte[] bytes)