Convert a bitset object to a hexadecimal string, as required by ISO8583 bitmap (hex) format, of the specified length (right-passed with "00" if required) - Java java.util

Java examples for java.util:BitSet

Description

Convert a bitset object to a hexadecimal string, as required by ISO8583 bitmap (hex) format, of the specified length (right-passed with "00" if required)

Demo Code


//package com.java2s;
import java.util.BitSet;

public class Main {
    /**//from w w  w  .  jav a2s.c  om
     * Convert a bitset object to a hexadecimal string, as required by ISO8583 bitmap (hex)
     * format, of the specified length (right-passed with "00" if required)
     * @param bitset    bitmap to convert
     * @param minLength minimum required for the resulting hex string
     * @return hexadecimal string version of the <code>bitset</code>supplied
     */
    static String bitset2Hex(final BitSet bitset, final int minLength) {
        final StringBuilder result = new StringBuilder();
        for (int bytenum = 0; bytenum < minLength / 2; bytenum++) {
            byte v = 0;
            for (int bit = 0, mask = 0x80; mask >= 0x01; bit++, mask /= 2) {
                if (bitset.get((bytenum * 8) + bit)) {
                    v |= mask;
                }
            }
            result.append(String.format("%02X", v));
        }
        while (result.length() < minLength) {
            result.append("00");
        }
        return result.toString();
    }
}

Related Tutorials