集册 Java实例教程 用与提供的十六进制字符串等效的位图进行回答

用与提供的十六进制字符串等效的位图进行回答

欢马劈雪     最近更新时间:2020-01-02 10:19:05

535
用与提供的十六进制字符串等效的位图进行回答


//package com.nowjava;

import java.util.BitSet;
/* 来 自 时 代 J a v a 公 众 号 - nowjava.com*/

public class Main {

    public static void main(String[] argv) throws Exception {

        String hex = "nowjava.com";

        System.out.println(hex2Bitset(hex));

    }


    /**

     * Answer with a bitmap equivalent to the hexadecimal string supplied

     * @param hex string representation of an ISO8583 bitmap

     * @return a bitset initialized from the input hex string

     * @throws IllegalArgumentException if the hex string is null, empty or not even-sized

     */

    static BitSet hex2Bitset(final String hex) {

        final BitSet result = new BitSet();

        if (hex == null) {

            throw new IllegalArgumentException(

                    "Hex string must be non-null");

        }

        if (hex.length() == 0 || hex.length() % 2 != 0) {

            throw new IllegalArgumentException(

                    "Hex string must be even-sized (length=" + hex.length()

                            + ")");

        }//n o w j a v a . c o m - 时代Java 提 供

        final int length = hex.length();

        int bytenum = (length / 2) - 1;

        for (int index = length; index >= 2; index -= 2) {

            final int bytevalue = Integer.valueOf(

                    hex.substring(index - 2, index), 16);

展开阅读全文