views:

79

answers:

1

I would like to call the Java class file from compiling the following code:

import java.io.*;

public class hex_to_dec { 
    private BufferedReader bufferedReader;
    private BufferedWriter bufferedWriter;

    public hex_to_dec (String stringPath, String stringPath_dec)
    {
        try
        {
            bufferedReader = new BufferedReader(new FileReader(stringPath));
            bufferedWriter = new BufferedWriter(new FileWriter(stringPath_dec, false));
        } catch (Exception e) {
            System.out.println("Error in opening file." + e);
        }
    }
    public void Parse() 
    {
        try {
            String tempLine;
            int temp;           
            while((tempLine = bufferedReader.readLine()) != null) {
                String[] tempBytes = tempLine.split(" ");
                temp = Integer.valueOf(tempBytes[0], 16);
                tempBytes[0] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[2], 16); 
                tempBytes[2] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[3], 16);  
                tempBytes[3] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[5], 16);
                tempBytes[5] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[6], 16);
                tempBytes[6] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[8], 16);
                tempBytes[8] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[9], 16);
                tempBytes[9] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[11], 16);
                tempBytes[11] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[12], 16);
                tempBytes[12] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[14], 16);
                tempBytes[14] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[15], 16);
                tempBytes[15] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[17], 16);
                tempBytes[17] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[18], 16);
                tempBytes[18] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[20], 16);
                tempBytes[20] = String.valueOf(((byte) temp));
                temp = Integer.valueOf(tempBytes[21], 16);
                tempBytes[21] = String.valueOf((temp));
                temp = Integer.valueOf(tempBytes[23], 16);
                tempBytes[23] = String.valueOf(((byte) temp));

                            for (int i = 0; i < tempBytes.length; i++)
                {
                    bufferedWriter.append(tempBytes[i] + " ");
                }
                bufferedWriter.append("\n");                
            }
            bufferedWriter.flush();
        } catch (Exception e) {
            System.err.println("Error:" + e);
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) { 
        // TODO Auto-generated method stub
        hex_to_dec data = new hex_to_dec(
                "C:\\Documents and Settings\\Admin\\My Documents\\MATLAB\\tests\\rssi_2\\trimmed\\s5_node12",
                "C:\\Documents and Settings\\Admin\\My Documents\\MATLAB\\tests\\rssi_2\\trimmed_dec\\s5_node12"); 
        data.Parse();
    }  
}

However, it requires an argument, and I don't know how to pass arguments into calling this command cleaning in bash. Also, I would like to be able to parse through a directory to call this function recursively through all the text files under the subdirectories of a selected directory. What's the easiest way of achieving this?

Thanks in advance! Hope this is not too demanding.

+4  A: 

I think you have a couple of steps here, first is to change the main to utilize args. You should check args.length to make sure a source file is specified, for example:

(warning: untested java from a C programmer)

public static void main(String[] args)
{
  if (args.length == 1)
  {
    hex_to_dec data = new hex_to_dec(args[0], args[0] + ".dec");
    data.Parse();
  }
}

Once the class accepts an argument, you will want to compile it.

javac hex_to_dec.java

Once it is compiled, you can use a script to recursively handle a directory.

#!/bin/sh
find . | xargs -L 1 java hex_to_dec

Note that if your goal is to convert a file of hex numbers to decimal, using java and bash is probably overkill. You could accomplish this using a single shell script like:

#!/bin/sh
find . -type f | while read filename
do

  # skip the file if it is already decoded
  if [ "${filename%.dec}" != "${filename}" -o -z "${filename}" ]
  then
    continue
  fi

  (
    # read the file, line by line
    cat "${filename}" | while read line
    do
      line=$(
        echo "${line}"                   |
        sed -e "s/[[:space:]]\{1,\}/;/g" | # split the line by spaces
        tr '[:lower:]' '[:upper:]')        # convert lower to uppercase

       echo "ibase=16; ${line}"          | # format the line for bc
        bc                               | # convert hex to dec
        tr "\n" " "                        # rejoin the output to a line

      echo ""                              # add the new line
    done
  ) > "${filename}.dec"
done
Brandon Horsley
The first element of `args` is `args[0]`, not `args[1]` (just like in C).
mobrule
Thanks, I updated the answer to reflect this, however in C argv[0] is the binary name, not the first parameter as you suggested, this starts at argv[1] - hence my mistake.
Brandon Horsley
You can redirect the output of a `while` loop without creating a subshell. And you can read a file into the while loop without piping `cat`. `while ...; do ...; done < input > output` Also, if this was Bash (which the OP tagged the question), you can do things like `base=16; hexnum='aa'; decnum=$(($base#$hexnum))` to convert hex to decimal. Both Bash and `sh` can split on spaces.
Dennis Williamson
Thanks for the info, I never thought about redirecting to and from the while loop. Good to know. I am not even sure that's what OP is trying to do, but if so, spawning java for each file seems excessive. Hopefully he can come up with something better out of these comments.
Brandon Horsley
If I were to use the straight bash option, how do I pass filename parameters into the bash script that you have suggested?
stanigator
The `find` recursively scans your current directory looking for files, you don't have to manually specify them. Just change directory to the appropriate location, and execute the script. Note that the script is not thoroughly tested, and Dennis Williamson had some good suggestions for improvement.
Brandon Horsley