Results 1 to 2 of 2
Thread: need help displaying hex values
- 03-05-2011, 10:45 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
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
- 03-05-2011, 10:51 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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
-
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
How to get values for unselected checkbox values
By sarath13 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 01-07-2011, 08:54 AM -
Displaying array values in jstl page
By keshaba in forum Advanced JavaReplies: 0Last Post: 04-23-2010, 10:00 AM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks