hex decoder : Hexadecimal « Data Type « Java






hex decoder

          
/*
 * 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

   
    
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Convert Decimal to Hexadecimal
2.Convert from decimal to hexadecimal
3.Convert from Byte array to hexadecimal string
4.Convert from decimal to hexadecimal with leading zeroes and uppercase
5.Hex encoder and decoder.
6.Decode hex string to a byte array
7.Returns the hexadecimal value of the supplied byte array
8.Convert byte array to Hex String
9.Convert the bytes to a hex string representation of the bytes
10.Hex decimal dump
11.Get Base64 From HEX
12.Converts a hex string to a byte array.
13.append hex digit
14.dump an array of bytes in hex form
15.Get byte array from hex string
16.Check if the current character is an Hex Char <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
17.Helper function that returns a char from an hex
18.Hex encoder/decoder implementation borrowed from BouncyCastle
19.Java Hex dump Library
20.Util for digesting and encoding bytes/strings.
21.Provide hex utility for converting bytes to hex string
22.Number in hexadecimal format are used throughout Freenet.
23.Convert to/from Hex string
24.This class allows a number to be easily formatted as a hexadecimal number. The representation uses 0-f.
25.This class provides methods for working with hexadecimal representations of data.
26.Hex Util