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 EBCDIC Interpreter for signed numerics.
023 * (see http://publib.boulder.ibm.com/infocenter/wmbhelp/v6r0m0/index.jsp?topic=/com.ibm.etools.mft.doc/ad06900_.htm)
024 * Strings are converted to and from EBCDIC bytes.
025 * Negatives will be prepended with "-"
026 * Unsigned numbers are interpreted as positive
027 * 
028 * @author nsmith@mxgroup.net
029 * @version $Revision$ $Date$
030 */
031public class SignedEbcdicNumberInterpreter implements Interpreter
032{
033    /** An instance of this Interpreter. Only one needed for the whole system */
034    public static final SignedEbcdicNumberInterpreter INSTANCE = new SignedEbcdicNumberInterpreter();
035
036    public void interpret(String data, byte[] targetArray, int offset) {
037        boolean negative = data.startsWith("-");
038        if (negative) {
039            ISOUtil.asciiToEbcdic(data.substring(1), targetArray, offset);
040            targetArray[offset + data.length() - 2] = (byte) (targetArray[offset + data.length() - 2] & 0xDF); 
041        } else {
042            ISOUtil.asciiToEbcdic(data, targetArray, offset);
043        }
044    }
045
046    public String uninterpret(byte[] rawData, int offset, int length) {
047        boolean negative = (byte) (rawData[offset + length - 1] & 0xF0) == (byte)0xD0;
048        rawData[offset + length - 1] = (byte) (rawData[offset + length - 1] | 0xF0);
049        return (negative ? "-" : "") + ISOUtil.ebcdicToAscii(rawData, offset, length);
050    }
051
052    public int getPackedLength(int nDataUnits)
053    {
054        return nDataUnits;
055    }
056}