Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:46:10+00:00 2026-06-18T12:46:10+00:00

We are really stuck on this topic, this is the only code we have

  • 0

We are really stuck on this topic, this is the only code we have which converts a file into hex but we need to open a file and then for the java code to read the hex and extract certain bytes (e.g. the first 4 bytes for the file extension:

import java.io.*;
public class FileInHexadecimal
{        
    public static void main(String[] args) throws Exception 
    {                
     FileInputStream fis = new FileInputStream("H://Sample_Word.docx");                
     int i = 0;                
     while ((i = fis.read()) != -1) {                       
        if (i != -1) {                                
        System.out.printf("%02X\n ", i);
     } 
    }  
    fis.close();    
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:46:11+00:00Added an answer on June 18, 2026 at 12:46 pm

    Do not confuse internal and external representation – what you do when converting to hex is that you only create a different representation of the same bytes.

    There is no need to convert to hex if you just want to read some bytes from the file – just read them. For example, to read the first four bytes, you can use something like

    byte[] buffer = new byte[4];
    FileInputStream fis = new FileInputStream("H://Sample_Word.docx");  
    int read = fis.read(buffer);
    if (read != buffer.length) {
        System.out.println("Short file!");
    }
    

    If you need to read data from an arbitrary position within the file, you might want to check RandomAccessFile instead of using a stream. RandomAccessFile allows to set the position where to start reading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this question is pretty basic, but I'm really stuck. I have a
I'm really stuck on this problem. Basically, I have a Windows 2008r2 server which
I have been stuck on this and don't really know how to go about
There might be some very simple answer to this, but i am really stuck
I'm really stuck on this. I'm trying to have the background image of a
I have been really stuck on this problem. My validation for my model fails
There have been a couple of questions very close to this topic, but none
I'm sorry if this comes through as a noob question, but I'm really stuck
I'm really stuck on this project that I'm doing. Below is my code. I
Ok, this question may be dumb, but I'm really stuck with this. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.