Solved

Displaying files in hex

Posted on 2004-10-02
9
350 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
[X]
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
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 57

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 90
Octopus Deploy: need a Time Event that fires every midnight 2 59
C# Windows Form Navigation - Total Beginner 9 96
Oracle programming for starter 14 76
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.
In this post we will learn different types of Android Layout and some basics of an Android App.
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 …

752 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