Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Displaying files in hex

Posted on 2004-10-02
9
Medium Priority
?
360 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 59

Expert Comment

by:Julian Hansen
ID: 12211055
What language are you using?
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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 1000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
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 …
Progress

688 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