Solved

Displaying files in hex

Posted on 2004-10-02
9
344 Views
Last Modified: 2010-04-17
In my application, I'm trying to display a file in hex format inside a textarea. How can I do that?

Also, how can I show the drive, path, filename in a previously created label? I tried the getAbsolutePath method and it don't work. Is there somethig else I can try?
0
Comment
Question by:HelpWithJava
9 Comments
 
LVL 1

Expert Comment

by:ch0c
ID: 12210386
This question actually has two questions in it. You should start a seperate question thread for the second one.

For the hex question, you can open the file in binary, then read in one byte at a time. Each byte is 8 bits, and each hex character is 4 bits. So there are two hex characters per byte.

First hex character = int((byte)/16)
Second hex character = (byte) (mod 16)

To convert from the resulting integer to a hex character, use an array (char hex[] = ['0','1','2',...,'D','E','F']). Then put both characters into the textarea and you're done!

Good luck.
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 12210787
If you use stl streams you can use std::hex to manipulate the contents to be displayed as hex values
0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 12211055
What language are you using?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:HelpWithJava
ID: 12211112
sorry ... I thought I was in the "Java" section on this site. I am using the Java platform and I need to know how to display a file in hex format.
0
 
LVL 1

Expert Comment

by:ch0c
ID: 12212625
The solution I proposed above should still work in Java.

String hex = new String[16];
hex = "0123456789ABCDEF";

...

//Convert character to hex
dhex1 = byte/16;
dhex2 = byte % 16;

Character hex1 = new Character(hex(dhex1));
Character hex2 = new Character(hex(dhex2));

Try this and see if it works. Good luck.
0
 

Author Comment

by:HelpWithJava
ID: 12219989
I guess I'm dumb or something because I'm not getting it. How can I make a file display in hex using the code below?


public void loadFile()
{
      int result = fileChooser.showOpenDialog(FileDumper.this);
      File selectFile = fileChooser.getSelectedFile();
      if(selectFile != null && result == JFileChooser.APPROVE_OPTION)
      try{
            FileInputStream fis = new FileInputStream(file);
            FileChannel fc = fis.getChannel();
            ByteBuffer bb = ByteBuffer.allocate((int)fc.size());
            fc.read(bb);
            fileOpen.setText(new String(bb.array()));
            fc.close();
            fis.close();
            fileChooser.showOpenDialog(null);
                     }catch(IOException e) {
            System.out.println(e);
            }
}
0
 
LVL 1

Accepted Solution

by:
ch0c earned 250 total points
ID: 12223375
Sorry, I don't know Java very well. But I'll try to explain a bit better with pseudocode:

String hex = new String[16];
hex = "0123456789ABCDEF";

Declare finalStr and initialize it to be empty
Open file 'test.txt' (in binary mode if applicable)

While next character of 'test.txt' is not EOF do
     Read one char from 'test.txt' and call it 'byte'
     dhex1 = byte/16;
     dhex2 = byte % 16;

     Character hex1 = new Character(hex(dhex1));
     Character hex2 = new Character(hex(dhex2));

     Add 'hex1' and 'hex2' to the end of finalStr
End while
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question