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.

Results 1 to 10 of 10

Thread: Strange problem printing Unicode characters

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Strange problem printing Unicode characters

    Help! My students are trying to implement the Huffman coding algorithm and
    one of them is having a bizarre error that I can't figure out. Here's the
    problem code segment:

    int tmpDec = convertBinToDec(tmpSub);
    char tmpChar = (char)tmpDec;
    System.out.print(tmpChar);
    pr.print(tmpChar);



    The specific problem is that one of his calls to convertBinToDec() is
    returning the decimal value 135, which codes to an unprintable control
    character. For some reason, the program is converting that to character
    value = 63, which is the question mark (?). Obviously, this results in an
    incorrectly encoded message.

    I didn't see this happen in my program, but maybe my test file never hit
    any of these unprintable control characters. Can someone please explain to
    me what's going on, and better yet, how to fix it?

    Thanks!
    Roger
    Last edited by sophist42; April 29th, 2011 at 08:46 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Strange problem printing Unicode characters

    If you want help, you'll have to provide an SSCCE that demonstrates the problem. This should only be a few lines in a main method of a bare-bones class.

    And we're not going to copy anything to your personal email. We aren't your underlings, and quite frankly, requesting we do something to make your life easier is rude and will decrease your chances of getting help.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strange problem printing Unicode characters

    Sorry about the last part. I was in a hurry and had copied and pasted a message to a listserv. My bad.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Strange problem printing Unicode characters

    That's fine, but I guess in that case you should read this: The problems with cross-posting

    But like I said, we can't really help you until you show us an SSCCE. More specifically, what tmpSub?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strange problem printing Unicode characters

    Quote Originally Posted by KevinWorkman View Post
    If you want help, you'll have to provide an SSCCE that demonstrates the problem. This should only be a few lines in a main method of a bare-bones class.
    How about this?
    public class WhatIsTheBug
    {
    public static void main(String[] args)
    {
    int tmpDec = 135; // that's the problem value
    char tmpChar = (char)tmpDec;
    System.out.println(tmpChar); // why does this print '?' when that char val = 63?
    }
    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Strange problem printing Unicode characters

    What would you expect it to print anyway?

    To get a better understanding of what's going on, I suggest you run something like this:

    public class Main {
    	public static void main(String[] args)
    	{			
    		for(int i = 0; i < 200; i++){
    			System.out.println(i + ": " + (char)i);
    		}
    	}
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strange problem printing Unicode characters

    Maybe that's my question. In Huffman coding, after you convert the original characters to their Huffman codes and generate a long binary string (like "11011010010001"), you then need to break up that string into eight-bit chunks and translate those chunks back into their corresponding character values.

    It seems like if one of those character values happens to be a non-printing control character (like 135), then the program punts and converts it into some printable character like '?' that doesn't really match. I wonder, is there any way of getting the actual Unicode character into the file? Otherwise that seems like a major roadblock to implementing the algorithm.

    This is my first time trying this project with my students, so I haven't had to deal with some of these problems before. Thanks for all your replies and also for tolerating my newbish behavior on the forums. FWIW, I've read the rules and guidelines article now and I think I'm trying to follow all the rules. The only other place I've posted this question is to the AP Computer Science teachers listserv.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Strange problem printing Unicode characters

    What encoding are you trying to use? Apparently, converting from an int to a char by casting uses the default of ISO-8859-1, which doesn't seem to have a value for 135: ISO/IEC 8859-1 - Wikipedia, the free encyclopedia
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Strange problem printing Unicode characters

    Quote Originally Posted by KevinWorkman View Post
    What encoding are you trying to use? Apparently, converting from an int to a char by casting uses the default of ISO-8859-1, which doesn't seem to have a value for 135: ISO/IEC 8859-1 - Wikipedia, the free encyclopedia
    *blush* I have to admit, I never thought to use anything other than the default encoding. You're right, apparently 8859-1 doesn't have encodings for some hexadecimal values, including 135 (0x87).

    Hmm. I suppose that the only solution here is to read and write the file bit by bit, instead of trying to convert those chunks into encoded characters. I'll try that.

    Thanks!

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Strange problem printing Unicode characters

    The first answer in this posting gives a pretty good explanation and possible solution: conversion - Converting int to char in java - Stack Overflow
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Problem with printing out collinear lines!
    By rkrajnov in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 25th, 2011, 05:32 PM
  2. combobox unicode problem
    By trexi in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: January 18th, 2011, 10:06 AM
  3. Strange launch problem using command prompt
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 15th, 2010, 07:52 AM
  4. Strange problem with drawing string
    By Asido in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2010, 03:38 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM