Solved

Displaying files in hex

Posted on 2004-10-02
9
321 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 52

Expert Comment

by:Julian Hansen
ID: 12211055
What language are you using?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 73
delphi parse string to params 3 100
Currency Conversion? 1 73
SCRIPT65535: Unexpected call to method or property access video html 5 explorer 3 46
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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.
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now