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 26

Thread: How to I possibly compress a java string to say byte space?

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How to I possibly compress a java string to say byte space?

    jhkjhjk
    Last edited by redbull; May 9th, 2015 at 04:41 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    I'm not sure I quite understand what you want, do you have a code example?

    As a side note I doubt you'd need to worry about compressing 10 lines of string data.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    nlkjlkj
    Last edited by redbull; May 9th, 2015 at 04:41 PM.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    How about create a byte array the same size as the paragraph and simply put the lower order bytes of the chars into the byte array, and then save it as a non-text file.

    --- Update ---

    Heck, String has a getBytes() method. Have you tried working with this?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,179
    Thanks
    63
    Thanked 2,577 Times in 2,547 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    Where is the code that outputs the unicode? Where is the unicode text written? How did you copy and paste here the unicode values shown in your post?

    When I compile and execute the program I get the text not unicode.

    What does this mean:
    When you convert the string to unicode
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    So your program takes a file input which you have to be able to parse, write to an output file (presumably smaller than the original input file), then be able to re-construct the original content and display it?

    A few questions:

    1. Is unicode output support a requirement? If it isn't, you can quickly chop your data size in half by switching to an 8-bit encoding like ASCII.

    2. Does your input file look exactly like what you posted? That file only has ~100 actual characters. You can use various functions in the Character class to parse the string "\u0063" into the actual unicode character.

    3. I doubt you're being asked to implement a fully-fledged compression algorithm. If your professor is looking for use of an actual compression algorithm, take a look at the java.util.zip package. It has implementations of the zip and gzip algorithms which you can call to compress/decompress files.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    lkjlkj
    Last edited by redbull; May 9th, 2015 at 04:42 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,179
    Thanks
    63
    Thanked 2,577 Times in 2,547 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    unicode was supplied by the teacher
    I am confused about what the program is doing.
    You said:
    Finally, the unicode is:
    \u0063\u006C...

    Is that the contents of a text file that you are supposed to read and convert to a Java String?
    \u0063 to c
    \u006C to l
    etc
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    jgjhghg
    Last edited by redbull; May 9th, 2015 at 04:42 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,179
    Thanks
    63
    Thanked 2,577 Times in 2,547 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    This is what I have to print out
    You could put all of the text into a single String. Use the line separator character in the String at the places where you want a new line to begin.

    I have to make the paragraph no more than 655 bytes, with a total of 793 bytes for the whole entire program.
    So the total text of the program outside of the paragraph has to be 138 bytes?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    hkjhjkh
    Last edited by redbull; May 9th, 2015 at 04:42 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,179
    Thanks
    63
    Thanked 2,577 Times in 2,547 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    Seems a big waste of time. What is supposed to be learned in this exercise?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    kjhkjhj
    Last edited by redbull; May 9th, 2015 at 04:42 PM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,179
    Thanks
    63
    Thanked 2,577 Times in 2,547 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    How are you learning compression by retyping the lines of a program?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    hkjh
    Last edited by redbull; May 9th, 2015 at 04:42 PM.

  16. #16
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    Norm is commenting on the assignment itself, not your questions. I agree that this looks like a pointless exercise.

    Are you counting bytes of source code, or bytes of the compiled program?

    I just compiled a pretty much as bare-bones program I that will work by reading an uncompressed text file in and the printing it out and that's an 1008 byte binary.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,179
    Thanks
    63
    Thanked 2,577 Times in 2,547 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    changed the "args" to "a", saving 3 bytes
    Putting all the code on one line will save the lineend characters for each line.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    jlkjlkj
    Last edited by redbull; May 9th, 2015 at 04:42 PM.

  19. #19
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    That's rather silly to count the bytes of the source file (that's the .java file, the .class file is the compiled binary). It encourages bad programming practices like single letter variable names, no tabbing, no comments, and no line separators. Even taking all of these things out the source file is 860 characters long.

    I cannot pass in any inputs for this. Here's what I was thinking. Convert this string to ascii, and compress that ascii code (because there may be repeating characters). Then place this compressed ascii code in a java program, and somehow find a class to uncompress it??? That way, this should not take up too much space.
    I wouldn't bother, Java source files are usually UTF-8 encoded, meaning if you only use characters in the lower 128 ASCII charset you'll get the exact same file either way (that's the case here).

    The only way I can think of to get your source file smaller is to use an external file, then your java program opens that file and prints it out.

  20. #20
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    hkjhkj
    Last edited by redbull; May 9th, 2015 at 04:43 PM.

  21. #21
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    When is your assignment due? I would recommend asking a member of the teaching staff if you're able to. These seem like rather silly requirements, and they might shed some light on what path you're expected to take.

    Having an externally compressed text file won't help much because then you'll need to have Java code which will decompress it. If you do end up compressing it, leave the text "as-is" so you don't have to do an extra step converting hex to a character code. The best solution I can think of is reading from an uncompressed text file and printing it out.

  22. #22
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    hlkj
    Last edited by redbull; May 9th, 2015 at 04:43 PM.

  23. #23
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    There's no way to directly store an image in a java source file, besides you're storing the same data either way. The character 'A' has a value of 65, which is 1 byte regardless of if it's interpreted as a pixel or as an UTF-8 character.

  24. #24
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    jlkjkl
    Last edited by redbull; May 9th, 2015 at 04:43 PM.

  25. #25
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    jlkjlkj
    Last edited by redbull; May 9th, 2015 at 04:43 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. how can i compress a video file for example an avi file in java?
    By vigneshwaran in forum Java Theory & Questions
    Replies: 1
    Last Post: January 15th, 2013, 01:39 AM
  2. [SOLVED] [Asking] Convert String to Byte Array
    By ardisamudra in forum Java Theory & Questions
    Replies: 8
    Last Post: October 30th, 2012, 09:58 AM
  3. Compress my Code!
    By aStudentofJava in forum Loops & Control Statements
    Replies: 23
    Last Post: March 4th, 2012, 05:14 AM
  4. Java Program Loop Possibly needed
    By mikec2500 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 25th, 2011, 12:35 AM
  5. [SOLVED] utf-16 byte[] to string conversion
    By Gerhardl in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2010, 06:06 AM

Tags for this Thread