001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso;
020
021/**
022 * Implements BCD Interpreter. Numeric Strings (consisting of chars '0'..'9' are converted
023 * to and from BCD bytes. Thus, "1234" is converted into 2 bytes: 0x12, 0x34.
024 * 
025 * @author joconnor
026 * @version $Revision$ $Date$
027 */
028public class HEXInterpreter implements Interpreter
029{
030    /** This HEXInterpreter sometimes adds a 0-nibble to the left. */
031    public static final HEXInterpreter LEFT_PADDED = new HEXInterpreter(true, false);
032    /** This HEXInterpreter sometimes adds a 0-nibble to the right. */
033    public static final HEXInterpreter RIGHT_PADDED = new HEXInterpreter(false, false);
034    /** This HEXInterpreter sometimes adds a F-nibble to the right. */
035    public static final HEXInterpreter RIGHT_PADDED_F = new HEXInterpreter(false, true);
036    /** This HEXInterpreter sometimes adds a F-nibble to the left. */
037    public static final HEXInterpreter LEFT_PADDED_F = new HEXInterpreter(true, true);
038
039    private boolean leftPadded;
040    private boolean fPadded;
041
042    /** Kept private. Only two instances are possible. */
043    private HEXInterpreter(boolean leftPadded, boolean fPadded) {
044        this.leftPadded = leftPadded;
045        this.fPadded = fPadded;
046    }
047
048    /**
049         * (non-Javadoc)
050         *
051     */
052    public void interpret(String data, byte[] b, int offset)
053    {
054        ISOUtil.str2hex(data, leftPadded, b, offset);
055        // if (fPadded && !leftPadded && data.length()%2 == 1)
056        //   b[b.length-1] |= (byte)(b[b.length-1] << 4) == 0 ? 0x0F : 0x00;
057        int paddedSize = data.length() >> 1;
058        if (fPadded && data.length()%2 == 1)
059            if (leftPadded)
060                b[offset] |= (byte) 0xF0;
061            else
062                b[offset+paddedSize] |= (byte) 0x0F;
063    }
064
065    /**
066         * (non-Javadoc)
067         *
068     */
069    public String uninterpret(byte[] rawData, int offset, int length)
070    {
071        return ISOUtil.hex2str (rawData, offset, length, leftPadded);
072    }
073
074    /**
075         * Each numeric digit is packed into a nibble, so 2 digits per byte, plus the
076     * possibility of padding.
077         *
078     */
079    public int getPackedLength(int nDataUnits)
080    {
081        return (nDataUnits + 1) / 2;
082    }
083}
084