Hex.java Source code

Java tutorial

Introduction

Here is the source code for Hex.java

Source

/*
 * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Modified by Tomas Gustavsson
 */

import java.io.*;
import java.math.BigInteger;

/**
 * This class implements a hex decoder, decoding a string with hex-characters into
 * the binary form.
 *
 * @version $Id: Hex.java,v 1.4 2004/04/15 07:28:36 hamgert Exp $
 *
 */
public class Hex {

    static private final char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
            'f' };

    /**
     * Encodar binrt till hex
     *
     *@param dataStr bin-representation av data
     *@return Hex-representation av data
     **/
    public static String encode(byte[] dataStr) {
        StringWriter w = new StringWriter();
        for (int i = 0; i < dataStr.length; i++) {
            int b = dataStr[i];
            w.write(hex[((b >> 4) & 0xF)]);
            w.write(hex[((b >> 0) & 0xF)]);
        }
        return w.toString();
    }

    /**
     * Decodar hex till binrt
     *
     *@param dataStr Strng innehllande hex-representation av data
     *@return byte[] innhllande binr representation av data
     **/
    public static byte[] decode(String dataStr) {

        if ((dataStr.length() & 0x01) == 0x01)
            dataStr = new String(dataStr + "0");
        BigInteger cI = new BigInteger(dataStr, 16);
        byte[] data = cI.toByteArray();

        return data;
    } //decode

    public static void main(String[] args) {
        if (args.length != 3) {
            System.out.println("Usage: HexStrToBin enc/dec <infileName> <outfilename>");
            System.exit(1);
        }
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            InputStream in = new FileInputStream(args[1]);
            int len = 0;
            byte buf[] = new byte[1024];
            while ((len = in.read(buf)) > 0)
                os.write(buf, 0, len);
            in.close();
            os.close();

            byte[] data = null;
            if (args[0].equals("dec"))
                data = decode(os.toString());
            else {
                String strData = encode(os.toByteArray());
                data = strData.getBytes();
            }

            FileOutputStream fos = new FileOutputStream(args[2]);
            fos.write(data);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    } //main

} // Hex