Java tutorial
//package com.java2s; /* * jPOS Project [http://jpos.org] * Copyright (C) 2000-2012 Alejandro P. Revilla * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * 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. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.util.BitSet; public class Main { public static BitSet int2BitSet(int value) { return int2BitSet(value, 0); } public static BitSet int2BitSet(int value, int offset) { BitSet bs = new BitSet(); String hex = Integer.toHexString(value); hex2BitSet(bs, hex.getBytes(), offset); return bs; } /** * Converts an ASCII representation of a Bitmap field * into a Java BitSet * @param b - hex representation * @param offset - starting offset * @param bitZeroMeansExtended - true for ISO-8583 * @return java BitSet object */ public static BitSet hex2BitSet(byte[] b, int offset, boolean bitZeroMeansExtended) { int len = bitZeroMeansExtended ? ((Character.digit((char) b[offset], 16) & 0x08) == 8 ? 128 : 64) : 64; BitSet bmap = new BitSet(len); for (int i = 0; i < len; i++) { int digit = Character.digit((char) b[offset + (i >> 2)], 16); if ((digit & (0x08 >> (i % 4))) > 0) bmap.set(i + 1); } return bmap; } /** * Converts an ASCII representation of a Bitmap field * into a Java BitSet * @param b - hex representation * @param offset - starting offset * @param maxBits - max number of bits (supports 8, 16, 24, 32, 48, 52, 64,.. 128 or 192) * @return java BitSet object */ public static BitSet hex2BitSet(byte[] b, int offset, int maxBits) { int len = maxBits > 64 ? ((Character.digit((char) b[offset], 16) & 0x08) == 8 ? 128 : 64) : maxBits; BitSet bmap = new BitSet(len); for (int i = 0; i < len; i++) { int digit = Character.digit((char) b[offset + (i >> 2)], 16); if ((digit & (0x08 >> (i % 4))) > 0) { bmap.set(i + 1); if (i == 65 && maxBits > 128) len = 192; } } return bmap; } /** * Converts an ASCII representation of a Bitmap field * into a Java BitSet * @param bmap - BitSet * @param b - hex representation * @param bitOffset - (i.e. 0 for primary bitmap, 64 for secondary) * @return java BitSet object */ public static BitSet hex2BitSet(BitSet bmap, byte[] b, int bitOffset) { int len = b.length << 2; for (int i = 0; i < len; i++) { int digit = Character.digit((char) b[i >> 2], 16); if ((digit & (0x08 >> (i % 4))) > 0) bmap.set(bitOffset + i + 1); } return bmap; } }