1 2 3 Previous Next 40 Replies Latest reply: Apr 20, 2007 3:44 PM by 807606 Go to original post RSS
      • 30. Re: Converting Integer to an 8 bit representation
        807606
        yes, that's right... look at the javadoc for the write method for the DataOutputStream.

        write
        public void write(int b)
        throws IOExceptionWrites the specified byte (the low eight bits of the argument b) to the underlying output stream. If no exception is thrown, the counter written is incremented by 1.
        Implements the write method of OutputStream.


        the value 256 in binary is 100000000... on a little endian system, the lower 8 bits are stored as 00000001.
        • 31. Re: Converting Integer to an 8 bit representation
          807606
          yes, that's right... look at the javadoc for the
          write method for the DataOutputStream.

          write
          public void write(int b)
          throws IOExceptionWrites the specified
          byte (the low eight bits of the argument b) to the
          underlying output stream. If no exception is thrown,
          the counter written is incremented by 1.
          mplements the write method of OutputStream.


          the value 256 in binary is 100000000... on a little
          endian system, the lower 8 bits are stored as
          00000001.
          I would need to create a method like the above?
          • 32. Re: Converting Integer to an 8 bit representation
            807606
            This code works for me. It writes out all the two byte char values to a file,
            then reads them back and verifies they have the right value. Maybe your
            trouble with WinHex is that you have trouble interpreting what it shows you.
            import java.io.*;
            
            public class StreamExample {
                public static void main(String[] args) throws IOException {
                    File file = new File("test.dat");
            
                    DataOutputStream out = new DataOutputStream(
                        new BufferedOutputStream(
                        new FileOutputStream(file)));
                    for(int i = 0; i < 0x10000; ++i)
                        out.writeChar((char)i);
                    out.flush();
                    out.close();
            
                    DataInputStream in = new DataInputStream(
                        new BufferedInputStream(
                        new FileInputStream(file)));
            
                     for(int i = 0; i < 0x10000; ++i) {
                         char ch = in.readChar();
                         if (ch != (char)i) {
                             System.out.println("disagreement on " + i);
                         }
                     }
                     if (in.read() != -1)
                         System.out.println("extra data found in file");
                }
            }
            Conclusion: writeChar/readChar handle 2 byte values correctly.
            • 33. Re: Converting Integer to an 8 bit representation
              807606
              no the above is the description of the write() method from a DataOutputStream <---- it's what you're using right now, and it explains why you see the value 1 in the byte.
              • 34. Re: Converting Integer to an 8 bit representation
                800282
                >
                Here is the code i use to output the values
                ...
                I meant something I can compile and run to replicate the behavior you described.
                • 35. Re: Converting Integer to an 8 bit representation
                  800282
                  ...
                  Conclusion: writeChar/readChar handle 2 byte values
                  correctly.
                  Ah, thank you for the trouble Dr. Jamf. I was just planning on posting a little demo myself.
                  • 36. Re: Converting Integer to an 8 bit representation
                    807606
                    ...
                    Conclusion: writeChar/readChar handle 2 byte
                    values
                    correctly.
                    Ah, thank you for the trouble Dr. Jamf. I was just
                    planning on posting a little demo myself.
                    Extra credit: why did I use an int in my first loop? Why not write:
                    for(char i = 0; i < 0x10000; ++i)
                    • 37. Re: Converting Integer to an 8 bit representation
                      800282
                      ...
                      Extra credit: why did I use an int in my first loop?
                      Why not write:

                      for(char i = 0; i < 0x10000; ++i)
                      Ha, a tricky one: the overflow will cause i < 0x10000 to always return true.
                      ; )
                      • 38. Re: Converting Integer to an 8 bit representation
                        807606
                        Ha, a tricky one: the overflow will cause i < 0x10000 to always return true.
                        [url #" style="display: block; background-image: url('http://www.jorgechaclan.com/duke/duke-thumbs.gif'); width: 50px; height: 50px] 
                        • 39. Re: Converting Integer to an 8 bit representation
                          807606
                          This code works for me. It writes out all the two
                          byte char values to a file,
                          then reads them back and verifies they have the right
                          value. Maybe your
                          trouble with WinHex is that you have trouble
                          interpreting what it shows you.
                          import java.io.*;
                          
                          public class StreamExample {
                          public static void main(String[] args) throws
                          IOException {
                          File file = new File("test.dat");
                          DataOutputStream out = new DataOutputStream(
                          new BufferedOutputStream(
                          new FileOutputStream(file)));
                          for(int i = 0; i < 0x10000; ++i)
                          out.writeChar((char)i);
                          out.flush();
                          out.close();
                          DataInputStream in = new DataInputStream(
                          new BufferedInputStream(
                          new FileInputStream(file)));
                          for(int i = 0; i < 0x10000; ++i) {
                          char ch = in.readChar();
                          if (ch != (char)i) {
                          System.out.println("disagreement on " + i);
                          }
                          }
                          if (in.read() != -1)
                          System.out.println("extra data found in
                          file");
                          }
                          /code]
                          Conclusion: writeChar/readChar handle 2 byte values
                          correctly.
                          So if i use writeChar/readChar it should handle my 256+ value?
                          • 40. Re: Converting Integer to an 8 bit representation
                            807606
                            >>
                            Here is the code i use to output the values
                            ...
                            I meant something I can compile and run to replicate
                            the behavior you described.
                            Ok here is my entire program.

                            I pass the input and output files on the command line
                            import java.io.*;
                            import java.util.*;
                            public class Test
                            {
                                 
                                 public static void main(String [] args)
                                 {
                                      try{
                                           Test encode = new Test();
                                           DataInputStream In = new DataInputStream(new FileInputStream(args[0]));
                                           DataOutputStream Out = new DataOutputStream(new FileOutputStream(args[1]));
                                           Hashtable<String, Integer> dictionary = new Hashtable<String, Integer>();
                            
                                           //Populate the dicionary with characters
                                           for(int i = 0; i < 256; i++)
                                          {
                                                String valOne = (char)i+"";
                                                int valTwo = i;
                                               dictionary.put(valOne, valTwo);
                                           }
                                           // Do encoding
                                           encode.encode(In, Out, dictionary);
                                           //close the Input file
                                           In.close();
                                      }
                                      catch(IOException e){
                                           e.printStackTrace();
                                      }
                                 }
                            
                                 public void encode(DataInputStream In, DataOutputStream Out, Hashtable<String, Integer> dictionary)throws IOException
                                 {
                                      try{
                                           int dictionarySize = 256;
                                           int reader = 0;
                                           String addVal="";
                                           int writeVal = 0;
                                           short myVal =0;
                                           String firstVal= (char)In.read()+"";
                                           while(In.available()>0)
                                           {
                                                reader=In.read();
                                                if(dictionary.containsKey(firstVal + (char)reader)){
                                                     firstVal = firstVal + (char)reader;
                                                }
                                                else
                                                {
                                                     writeVal = dictionary.get(firstVal);
                                                     if(writeVal > 255){
                                                          myVal = (short)writeVal;
                                                          Out.writeShort(myVal);
                                                     }
                                                     else{
                                                          Out.write(writeVal);
                                                     }
                                                     addVal = firstVal + (char)reader;
                                                     dictionary.put(addVal, dictionarySize);
                                                     firstVal = (char)reader+"";
                                                     dictionarySize++;
                                                }
                                           }
                                           writeVal = dictionary.get(firstVal);
                                           if(writeVal > 255){
                                                myVal = (short)writeVal;
                                                Out.writeShort(myVal);
                                           }
                                           else{
                                                Out.write(writeVal);
                                           }
                                           Out.flush();
                                           Out.close();
                                      } 
                                      catch(IOException e){
                                           e.printStackTrace();
                                      }     
                                 }
                            }
                            1 2 3 Previous Next