Java Byte Array Encode encode(byte[] rawData)

Here you can find the source of encode(byte[] rawData)

Description

encode

License

LGPL

Declaration

public static String encode(byte[] rawData) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.UnsupportedEncodingException;

public class Main {
    private static String charset = "ISO-8859-1";

    public static String encode(String sourceText) throws UnsupportedEncodingException {
        return encode(sourceText.getBytes(getCharset()));
    }/*from ww  w .  j av a 2  s .  c  o m*/

    public static String encode(byte[] rawData) {
        StringBuilder hexText = new StringBuilder();
        String initialHex = null;
        int initHexLength = 0;

        for (int i = 0; i < rawData.length; i++) {
            int positiveValue = rawData[i] & 0x000000FF;
            initialHex = Integer.toHexString(positiveValue);
            initHexLength = initialHex.length();
            while (initHexLength++ < 2) {
                hexText.append("0");
            }
            hexText.append(initialHex);
        }
        return hexText.toString();
    }

    public static String encode(byte rawData) {
        StringBuilder hexText = new StringBuilder();
        String initialHex = null;
        int initHexLength = 0;

        int positiveValue = rawData & 0x000000FF;

        initialHex = Integer.toHexString(positiveValue);
        initHexLength = initialHex.length();
        while (initHexLength++ < 2) {
            hexText.append("0");
        }
        hexText.append(initialHex);

        return hexText.toString();
    }

    public static String getCharset() {
        return charset;
    }
}

Related

  1. encode(byte[] data)
  2. encode(byte[] data)
  3. encode(byte[] in, int len)
  4. encode(byte[] input)
  5. encode(byte[] message)
  6. encode(byte[] source)
  7. encode(String prefix, byte[] buf, ZipOutputStream zos, File[] files)
  8. encodeAsUtf8(byte[] bytes)
  9. encodeBitString(byte[] in, OutputStream os)