Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 29

Thread: Reading a files header, hex

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default 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.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading a files header, hex

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading a files header, hex

    edited

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading a files header, hex

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Editing this code so that it takes a file instead of a string

    edited

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Editing this code so that it takes a file instead of a string

    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.

    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
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Editing this code so that it takes a file instead of a string

    edited

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Editing this code so that it takes a file instead of a string

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Editing this code so that it takes a file instead of a string

    the code is above in my previous post

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Editing this code so that it takes a file instead of a string

    edited

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Editing this code so that it takes a file instead of a string

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    tomb1992 (March 4th, 2013)

  17. #16
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Editing this code so that it takes a file instead of a string

    edited

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    tomb1992 (March 4th, 2013)

  20. #18
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Reading a file and comparing them to an array to get the file type - java

    edited

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading a file and comparing them to an array to get the file type - java

    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.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #20
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading a file and comparing them to an array to get the file type - java

    edited

  23. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  24. #22
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  25. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading a file and comparing them to an array to get the file type - java

    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
    If you don't understand my answer, don't ignore it, ask a question.

  26. #24
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading a file and comparing them to an array to get the file type - java

    edited

  27. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading a files header, hex

    //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
        if (i != -1) {
    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.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. java GUI , reading in files
    By zerocool18 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 25th, 2013, 07:23 PM
  2. Reading files and writing files
    By ProgrammerNewbie in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 3rd, 2012, 12:13 AM
  3. reading .txt files
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 8th, 2012, 12:47 PM
  4. Reading In Then Writing Out Massive Files
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 28th, 2011, 07:00 PM
  5. Reading many files using a scanner
    By jayjames90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 22nd, 2009, 04:35 PM

Tags for this Thread