⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isoutil.java

📁 POS is a Java&#174 platform-based, mission-critical, ISO-8583 based financial transaction library/fr
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param b - byte array     * @param offset  - starting position     * @param len     * @return String representation     */    public static String hexString(byte[] b, int offset, int len) {        StringBuffer d = new StringBuffer(len * 2);        len += offset;        for (int i=offset; i<len; i++) {            char hi = Character.forDigit ((b[i] >> 4) & 0x0F, 16);            char lo = Character.forDigit (b[i] & 0x0F, 16);            d.append(Character.toUpperCase(hi));            d.append(Character.toUpperCase(lo));        }        return d.toString();    }    /**     * bit representation of a BitSet     * suitable for dumps and debugging     * @param b - the BitSet     * @return string representing the bits (i.e. 011010010...)     */    public static String bitSet2String (BitSet b) {        int len = b.size();        len = (len > 128) ? 128: len;        StringBuffer d = new StringBuffer(len);        for (int i=0; i<len; i++)            d.append (b.get(i) ? '1' : '0');        return d.toString();    }    /**     * converts a BitSet into a binary field     * used in pack routines     * @param b - the BitSet     * @return binary representation     */    public static byte[] bitSet2byte (BitSet b)    {        int len = (((b.length()+62)>>6)<<6);        byte[] d = new byte[len >> 3];        for (int i=0; i<len; i++)             if (b.get(i+1))                 d[i >> 3] |= (0x80 >> (i % 8));        if (len>64)            d[0] |= 0x80;        if (len>128)            d[8] |= 0x80;        return d;    }        /**     * converts a BitSet into a binary field     * used in pack routines     * @param b - the BitSet     * @param bytes - number of bytes to return     * @return binary representation     */        public static byte[] bitSet2byte (BitSet b, int bytes)    {        int len = bytes * 8;                byte[] d = new byte[bytes];        for (int i=0; i<len; i++)             if (b.get(i+1))                 d[i >> 3] |= (0x80 >> (i % 8));        //TODO: review why 2nd & 3rd bit map flags are set here???         if (len>64)            d[0] |= 0x80;        if (len>128)            d[8] |= 0x80;        return d;    }    /**     * Converts a binary representation of a Bitmap field     * into a Java BitSet     * @param b - binary representation     * @param offset - staring offset     * @param bitZeroMeansExtended - true for ISO-8583     * @return java BitSet object     */    public static BitSet byte2BitSet         (byte[] b, int offset, boolean bitZeroMeansExtended)    {        int len = bitZeroMeansExtended ?            ((b[offset] & 0x80) == 0x80 ? 128 : 64) : 64;        BitSet bmap = new BitSet (len);        for (int i=0; i<len; i++)             if (((b[offset + (i >> 3)]) & (0x80 >> (i % 8))) > 0)                bmap.set(i+1);        return bmap;    }    /**     * Converts a binary representation of a Bitmap field     * into a Java BitSet     * @param b - binary representation     * @param offset - staring offset     * @param maxBits - max number of bits (supports 64,128 or 192)     * @return java BitSet object     */    public static BitSet byte2BitSet (byte[] b, int offset, int maxBits) {        int len = maxBits > 64 ?            ((b[offset] & 0x80) == 0x80 ? 128 : 64) : maxBits;        if (maxBits > 128 &&             b.length > offset+8 &&             (b[offset+8] & 0x80) == 0x80)        {            len = 192;        }         BitSet bmap = new BitSet (len);        for (int i=0; i<len; i++)             if (((b[offset + (i >> 3)]) & (0x80 >> (i % 8))) > 0)                bmap.set(i+1);        return bmap;    }    /**     * Converts a binary 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 byte2BitSet (BitSet bmap, byte[] b, int bitOffset)    {        int len = b.length << 3;        for (int i=0; i<len; i++)             if (((b[i >> 3]) & (0x80 >> (i % 8))) > 0)                bmap.set(bitOffset + 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 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;    }    /**     * @param   b       source byte array     * @param   offset  starting offset     * @param   len     number of bytes in destination (processes len*2)     * @return  byte[len]     */    public static byte[] hex2byte (byte[] b, int offset, int len) {        byte[] d = new byte[len];        for (int i=0; i<len*2; i++) {            int shift = i%2 == 1 ? 0 : 4;            d[i>>1] |= Character.digit((char) b[offset+i], 16) << shift;        }        return d;    }    /**     * @param s source string (with Hex representation)     * @return byte array     */    public static byte[] hex2byte (String s) {        if (s.length() % 2 == 0) {            return hex2byte (s.getBytes(), 0, s.length() >> 1);        } else {            throw new RuntimeException("Uneven number("+s.length()+") of hex digits passed to hex2byte.");        }    }    /**     * format double value     * @param amount    the amount     * @param fieldLen  the field len     * @return a String of fieldLen characters (right justified)     */    public static String formatDouble(double d, int len) {        String prefix = Long.toString((long) d);        String suffix = Integer.toString (            (int) ((Math.round(d * 100f)) % 100) );        try {            if (len > 3)                prefix = ISOUtil.padleft(prefix,len-3,' ');            suffix = ISOUtil.zeropad(suffix, 2);        } catch (ISOException e) {            // should not happen        }        return prefix + "." + suffix;    }    /**     * prepare long value used as amount for display     * (implicit 2 decimals)     * @param l value     * @param len display len     * @return formated field     * @exception ISOException     */    public static String formatAmount(long l, int len) throws ISOException {        String buf = Long.toString(l);        if (l < 100)            buf = zeropad(buf, 3);        StringBuffer s = new StringBuffer(padleft (buf, len-1, ' ') );        s.insert(len-3, '.');        return s.toString();    }    /**     * XML normalizer     * @param s source String     * @param canonical true if we want to normalize \r and \n as well     * @return normalized string suitable for XML Output     */    public static String normalize (String s, boolean canonical) {        StringBuffer str = new StringBuffer();        int len = (s != null) ? s.length() : 0;        for (int i = 0; i < len; i++) {            char ch = s.charAt(i);            switch (ch) {                case '<':                     str.append("&lt;");                    break;                case '>':                     str.append("&gt;");                    break;                case '&':                     str.append("&amp;");                    break;                case '"':                     str.append("&quot;");                    break;                case '\r':                case '\n':                     if (canonical) {                        str.append("&#");                        str.append(Integer.toString((int) (ch & 0xFF)));                        str.append(';');                        break;                    }                    // else, default append char                default:                     if (ch < 0x20) {                        str.append("&#");                        str.append(Integer.toString((int) (ch & 0xFF)));                        str.append(';');                    }                    else if (ch > 0xff00) {                        str.append((char) (ch & 0xFF));                    } else                        str.append(ch);            }        }        return (str.toString());    }    /**     * XML normalizer (default canonical)     * @param s source String     * @return normalized string suitable for XML Output     */    public static String normalize (String s) {        return normalize (s, true);    }    /**     * Protects PAN, Track2, CVC (suitable for logs).     *     * <pre>     * "40000101010001" is converted to "400001____0001"     * "40000101010001=020128375" is converted to "400001____0001=0201_____"     * "123" is converted to "___"     * </pre>     * @param s string to be protected      * @return 'protected' String     */    public static String protect (String s) {        StringBuffer sb = new StringBuffer();        int len   = s.length();        int clear = len > 6 ? 6 : 0;        int lastFourIndex = -1;        if (clear > 0) {            lastFourIndex = s.indexOf ('=') - 4;            if (lastFourIndex < 0) {                lastFourIndex = s.indexOf ('^') - 4;                if (lastFourIndex < 0)                     lastFourIndex = len - 4;            }        }        for (int i=0; i<len; i++) {            if (s.charAt(i) == '=')                clear = 1;  // use clear=5 to keep the expiration date            else if (s.charAt(i) == '^') {                lastFourIndex = 0;                clear = len - i;            }            else if (i == lastFourIndex)                clear = 4;            sb.append (clear-- > 0 ? s.charAt(i) : '_');        }        return sb.toString();    }    public static int[] toIntArray(String s) {        StringTokenizer st = new StringTokenizer (s);        int[] array = new int [st.countTokens()];        for (int i=0; st.hasMoreTokens(); i++)             array[i] = Integer.parseInt (st.nextToken());        return array;    }    public static String[] toStringArray(String s) {        StringTokenizer st = new StringTokenizer (s);        String[] array = new String [st.countTokens()];        for (int i=0; st.hasMoreTokens(); i++)             array[i] = st.nextToken();        return array;    }    /**     * Bitwise XOR between corresponding bytes     * @param op1 byteArray1     * @param op2 byteArray2     * @return an array of length = the smallest between op1 and op2     */    public static byte[] xor (byte[] op1, byte[] op2) {        byte[] result = null;        // Use the smallest array        if (op2.length > op1.length) {            result = new byte[op1.length];        }        else {            result = new byte[op2.length];        }        for (int i = 0; i < result.length; i++) {            result[i] = (byte)(op1[i] ^ op2[i]);        }        return  result;    }    /**     * Bitwise XOR between corresponding byte arrays represented in hex     * @param op1 hexstring 1     * @param op2 hexstring 2     * @return an array of length = the smallest between op1 and op2     */    public static String hexor (String op1, String op2) {        byte[] xor = xor (hex2byte (op1), hex2byte (op2));        return hexString (xor);    }    /**     * Trims a byte[] to a certain length

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -