Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Displaying files in hex

Posted on 2004-10-02
9
340 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 55

Expert Comment

by:Julian Hansen
ID: 12211055
What language are you using?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow

808 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