-
Reading a files header, hex
Hi, i'm just wondering if anybody knows how to get the first few hex symbols of a file in java, for example if i input a pdf into my coding i want my program to output, e.g "25 46 44 38" does anybody know how to do this?
I have been able to print out the hex of a whole file but not managed to set a maximum read limit so that my code only takes a certain amount of values
-
Re: Reading a files header, hex
What code have you done so far. It would be easier to know what you are talking about with a "visual" of your process.
-
Re: Reading a files header, hex
Quote:
I have been able to print out the hex of a whole file but not managed to set a maximum read limit
It should be easy to stop reading on any given byte. Can you explain what the problem is with limiting the number of bytes read and displayed?
-
Re: Reading a files header, hex
-
Re: Reading a files header, hex
Quote:
This ccoding gets all the hex of a file but its just the 47 49 46 38 that i want
Change the while loop to a for loop that loops 4 times.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
-
Re: Reading a files header, hex
ive accidentally put it in as quotes but my coding isnt importat, i just need the bytes to work, i think its a bit more complicated that just a simple for loop, i think theres something extra that i am missing
-
Re: Reading a files header, hex
If a while loop that reads to the end of the file works for all the bytes in a file, replacing the while loop with a for loop can limit the number of bytes read and displayed.
-
Editing this code so that it takes a file instead of a string
-
Re: Editing this code so that it takes a file instead of a string
Quote:
problem is that i want to be able to open a file from a directory
Please explain what problem you are having? What does: from a directory" mean?
Are you having problems getting the path to the folder that contains the file?
Do you want the user to select it? See the JFileChooser class.
Quote:
read the file's first four hex digits
Are you asking how to read the first 4 bytes of a file and convert each byte to a 2 character String of hex digits?
Is this related to: http://www.javaprogrammingforums.com...html#post99670
-
Re: Editing this code so that it takes a file instead of a string
-
Re: Editing this code so that it takes a file instead of a string
Quote:
tried using a for loop on it so that it just loops to get the amount of bytes i want but ive had no luck with it,
Please post the code and explain what the problem with it was.
-
Re: Editing this code so that it takes a file instead of a string
the code is above in my previous post
-
Re: Editing this code so that it takes a file instead of a string
Can you isolate the code like the code in post#3 and post it here?
-
Re: Editing this code so that it takes a file instead of a string
-
Re: Editing this code so that it takes a file instead of a string
Quote:
tried using a for loop on it so that it just loops to get the amount of bytes i want but ive had no luck with it,
Where is the for loop?
-
Re: Editing this code so that it takes a file instead of a string
-
Re: Editing this code so that it takes a file instead of a string
Do you know how to write a for loop that will loop 4 times? Try using that kind of a loop to read and reformat the bytes that are read from the file instead of a while loop that goes until the end of the file.
Completely replace the while loop with a for loop.
-
Reading a file and comparing them to an array to get the file type - java
-
Re: Reading a file and comparing them to an array to get the file type - java
Quote:
need the coding to read into the file and get the hex that tells us what the files type is
That should be a small method that reads the first few bytes of a file and returns them.
Quote:
i need is to be able to put a string of hex that has been got from the file into the method
Same answer as above.
Where is the small method you have been working on to do this? All the code that has been posted has much more than is needed for reading the first few bytes of a file, formatting the bytes into a String and returning that String.
This thread is a continuation of your other threads:
http://www.javaprogrammingforums.com...ad-string.html
http://www.javaprogrammingforums.com...html#post99670
Please finish those other threads and don't start new ones for the same topic.
-
Re: Reading a file and comparing them to an array to get the file type - java
-
Re: Reading a file and comparing them to an array to get the file type - java
The advice is the same as the last 3 or 4 times: use a for loop to read the first 4 bytes from the file.
Get rid of the while() loop!
Try writing the method in several steps:
1) write a loop to read bytes as int and display them
2) convert the ints to a hex string
3) concat the hex Strings
4)return the String
-
Re: Reading a file and comparing them to an array to get the file type - java
i keep posting because i dont think i am explaining it right and i have tried the for loop thing and it wont work, from the coding i have provided in the last comment a for loop will not fit into this in place of the while loop
-
Re: Reading a file and comparing them to an array to get the file type - java
Quote:
i have tried the for loop thing and it wont work,
Post the code with the for loop that won't work.
The steps the code should take for step 1:
loop 4 times
read next byte
if byte is -1 quit -> the file is too short
print the byte
end loop
-
Re: Reading a file and comparing them to an array to get the file type - java
-
Re: Reading a files header, hex
Quote:
//not sure what goes here
Inside the loop are these 3 steps:
read next byte
if byte is -1 quit -> the file is too short
print the byte
The loop control varialbe: i will never be -1. It will count the number of loops: 0,1,2,3
There needs to be a new int variable defined that will receive the byte that is read from the file.