Results 1 to 2 of 2
  1. #1
    intrepid604 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default need help displaying hex values

    Below is a program which loads a file and then displays a binary representation of its contents. I need to modify it to display the hex representation. I think I understand most of the code, but I don't get what the following excerpt is about:

    value = value << 1;
    if (bit == '1')
    value = value | mask;

    which is found in the writebit() method in the code below. Could anyone help me to understand this? Am I right to assume that modifying this chunk is the key to solving this problem?


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class Exercise19_20 extends JFrame {
      private JTextField jtfFilename = new JTextField();
      private JButton jbtSave = new JButton("Save the bits to the file");
      private JTextArea jtaBits = new JTextArea();
      
      public Exercise19_20() {
        JPanel panel1 = new JPanel(new BorderLayout());
        panel1.add(new JLabel("Enter a file"), BorderLayout.WEST);
        panel1.add(jtfFilename, BorderLayout.CENTER);
    
        add(panel1, BorderLayout.NORTH);
        jtaBits.setLineWrap(true);   
        add(new JScrollPane(jtaBits));
        add(jbtSave, BorderLayout.SOUTH);
        
        jtfFilename.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            try {
              FileInputStream in = new FileInputStream(jtfFilename.getText().trim());
              
              String s = "";
              int value;
              while ((value = in.read()) != -1) {
                s += getBits(value);
              }
              
              in.close();
              jtaBits.setText(s);
            }
            catch (IOException ex) {
              ex.printStackTrace();
            }
          }
        });
        
        jbtSave.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            try {
              BitOutputStream output = new BitOutputStream(new File(jtfFilename.getText().trim()));
              output.writeBit(jtaBits.getText().trim());
              output.close();    
            } 
            catch (IOException ex) {
              ex.printStackTrace();
            }
          }
        });
      }
      
      public static String getBits(int value) {
        String result = "";
        
        int mask = 1;
        for (int i = 7; i >= 0; i--) {
          int temp = value >> i;
          int bit = temp & mask;
          result = result + bit;
        }
        return result;
      }
      
      public static class BitOutputStream {
        private FileOutputStream output;
        private int value;
        private int count = 0;
        private int mask = 1; // The bits are all zeros except the last one
        
        public BitOutputStream(File file) throws IOException {
          output = new FileOutputStream(file);
        }
        
        public void writeBit(char bit) throws IOException {
          count++;
          value = value << 1;
          
          if (bit == '1') 
            value = value | mask;
          
          if (count == 8) {
            output.write(value);
            count = 0;
          }
        }
        
        public void writeBit(String bitString) throws IOException {
          for (int i = 0; i < bitString.length(); i++)
            writeBit(bitString.charAt(i));
        }
        
        /** Write the last byte and close the stream. If the last byte is not full, right-shfit with zeros */
        public void close() throws IOException {     
          if (count > 0) {
            value = value << (8 - count);
            output.write(value);
          }
          
          output.close();
        }   
      }
      
      /** Main method */
      public static void main(String[] args) {
        JFrame frame = new Exercise19_20();
        frame.setTitle("Exercise19_20");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setVisible(true);
      }
    }

    Thanks for any help

  2. #2
    oriom is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Its something to do with shifting bits and performing an OR operation

    I found this link which might help: http://download.oracle.com/javase/tu...bolts/op3.html

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  2. How to get values for unselected checkbox values
    By sarath13 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-07-2011, 09:54 AM
  3. Displaying array values in jstl page
    By keshaba in forum Advanced Java
    Replies: 0
    Last Post: 04-23-2010, 11:00 AM
  4. Replies: 0
    Last Post: 05-22-2008, 11:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 02:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •