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 17 of 17

Thread: char.toHexString();

  1. #1
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Cool char.toHexString();

    Dear All:

    I am wondering why you can do:

     
    Integer.toHexString(letter1);

    But you can not do

     
    Character.toHexString(letter1);

    I tried changing the "C" in Character to "c" and still no net beans, so do I have to go through an intermediate step of converting the character to an Integer.

    I think the hole point is to find the Hexadecimal value of the character.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: char.toHexString();

    Because toHexString() is a static method of the Integer class, not of the Character class. Check out the API.

    It's sort of similar to why you can do Character.toUpperCase(int) but not Integer.toUpperCase(int). It's simply a matter of to which class those methods belong.

    Java Platform SE 6
    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
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Red face Re: char.toHexString();

    Dear KevinWorkman:

    That makes sense, I can not use the method of one class on another class.

    I made changes to my program:

    public class LetterConversion{
    public static void main(String [] args)
    {
     
    char letter1 = 'A', letter2 = 'B', letter3 = 'C';
     
    System.out.println("So far we have: " + letter1 +" " + letter2 + " " + letter3);
     
    int convert1 = letter1, convert2 = letter2, convert3 = letter3;
     
    System.out.println("Now we have changed the Charicters to there Integer counterparts: \n" 
    + convert1 + " " + convert2 + " " + convert3);
     
    System.out.println("Now we use the \"Integer.toHexString()\" command, and we get!");
     
    Integer.toHexString(convert1);
    Integer.toHexString(convert2);
    Integer.toHexString(convert3);
     
     
    System.out.println("Letter a is now: " + convert1);
    System.out.println("Letter b is now: " + convert2);
    System.out.println("Letter c is now: " + convert3);
    }}

    Why bother using: Integer.toHexString

    if the line: int Convert = letter1

    Will give the same results.

    A = 65
    B = 66
    C = 67

    Or am I missing something in my code?
    Whats the point and why? lol

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: char.toHexString();

    Did you look at the API? That method doesn't change the int you pass into it (it actually can't do that for multiple reasons). Instead, that method returns a String. Store that String and print that instead of printing the original value.
    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. The Following User Says Thank You to KevinWorkman For This Useful Post:

    SPACE MONKEY (February 19th, 2011)

  6. #5
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Talking Re: char.toHexString();

    Dear KevinWorkman:

    So very sorry to ask this, but what is the API?

  7. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: char.toHexString();

    API means Application Programming Interfaces

    The Java API is the set of classes included with the Java Development Environment. It is like the official documentation for a class.

    Please see - Java Platform SE 6
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  8. The Following User Says Thank You to JavaPF For This Useful Post:

    SPACE MONKEY (February 19th, 2011)

  9. #7

    Default Re: char.toHexString();

    Ditto on what people have answered so far. Also, keep in mind that mapping a character to
    an integer or hex string isn't as straightforward as it seems. These days, most programs
    have to deal with non-English languages such as Chinese, etc., and sometimes a single
    "character" gets mapped to multiple bytes. You might try looking through
    Charset (Java Platform SE 6)
    for more details.

  10. The Following User Says Thank You to eluctari For This Useful Post:

    SPACE MONKEY (February 19th, 2011)

  11. #8
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: char.toHexString();

    Dear All:

    Thanks for your help.

    The reason I am delving into this is because I am reading Ivor Hortons "Beginning Java 2 JDK 5th Edition"

    Can anyone tell me, is this book using at least the most recent "MAJOR" rewrite of Java? Or am I going to come a cropper with some of the stuff in there?

    Thanks again to all of you, and now I am going to read the API.

    Cheers.

    The SPACE MONKEY.

  12. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: char.toHexString();

    Quote Originally Posted by SPACE MONKEY View Post
    The reason I am delving into this is because I am reading Ivor Hortons "Beginning Java 2 JDK 5th Edition"

    Can anyone tell me, is this book using at least the most recent "MAJOR" rewrite of Java? Or am I going to come a cropper with some of the stuff in there?
    From wikipedia: J2SE 5.0 entered its end-of-life on April 8, 2008 and is no longer supported by Sun as of November 3, 2009.

    The most recent version is Java SE 6 Update 24.

    Your book won't contain anything that's necessarily wrong, but it wouldn't hurt to use a more recent version.

    Java version history - 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!

  13. The Following User Says Thank You to KevinWorkman For This Useful Post:

    SPACE MONKEY (February 24th, 2011)

  14. #10
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: char.toHexString();

    Dear All:

    Thanks for the help, the AIP is very helpful. I have to now try and understand how it is structured so I can find stuff easier, or just google it.

    This is the code that works. I am concerned because I am not sure why I was told the .
    Integer.toHexString(convert1);
    would not work.
    from the AIP the information type on the Left Hand Side (LHS) indicated a double value was required, so I thought I had to convert to a double. Now I have no idea what the information on the LHS column is there for. No doubles where killed in the making of this line of code.
    String sletter1 = Integer.toHexString(convert1);
    So if someone can explain that to me it would be useful.



     
    public class LetterConversion{
    public static void main(String [] args)
    {
    //From page 62 hortons, got a who JDK 5th. 
    char letter1 = 'A', letter2 = 'B', letter3 = 'C';
     
    System.out.println("So far we have: " + letter1 +" " + letter2 + " " + letter3);
     
    // Converting the characters into int.
    int convert1 = letter1, convert2 = letter2, convert3 = letter3;
     
    System.out.println("Now we have changed the Charicters to there Integer counterparts: " \n
    + convert1 + " " + convert2 + " " + convert3);
     
    //converting the int to Hex String 
    System.out.println("Now we use the \"Integer.toHexString()\" command, and we get!");
    String sletter1 = Integer.toHexString(convert1); 
    String sletter2 = Integer.toHexString(convert2);
    String sletter3 = Integer.toHexString(convert3);
     
    System.out.println("Letter a is now: " + sletter1);
    System.out.println("Letter b is now: " + sletter2);
    System.out.println("Letter c is now: " + sletter3);
    }}

    One of the lessons I am learning is the distinction between primitive types and not so primitive types like Strings. I am not there yet but I know that the difference exists.

    I am assuming the String, not being primitive needs/has methods that are required for Strings to be manipulated, where as conversions from "Char" to "Int" are so much simpler. What type of type "Char" is I am not yet sure.

    I am editing this post, because I now know I have no idea what I am talking about with the previous two sentences.

    Thanks again for all your help. I am going back to look at my original code to compare.
    Last edited by SPACE MONKEY; February 24th, 2011 at 12:45 PM.

  15. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: char.toHexString();

    I'm not sure what your question is. Integer.toHexString() takes an int and returns a String. Where do you see that a double is required?
    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!

  16. #12
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: char.toHexString();

    When I was looking at the API (AIP?) there are two columns, the on on the left has either, bite, or long, or String, then in the column on the right is the method and it's requirements.
    That is where I saw the long, in the column on the left.

    Thanks.


  17. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: char.toHexString();

    Well, Long is yet another class in the API, but that still doesn't explain where you got the double requirement. Double is another class (these are all primitive wrapper classes).
    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!

  18. #14
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: char.toHexString();

    Dear KevinWorkman:

    In the "Filed Summary" or the "Method Summary" section of the page linked below there is a value in the left hand side column, I am wondering what is the significance of that column.

    Java Platform SE 6

    Thanks.

    The SPACE MONKEY

    Also I think I have worn this thread out, thanks for the help, it was a good yarn. lol

  19. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: char.toHexString();

    I'm not really sure what you're asking. That just links to the main part of the API. The column on the left provides links to all of the java packages and classes.

    Are you talking about a specific class in the API?
    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!

  20. #16
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: char.toHexString();

    Dear KevinWorkman:

    Sorry about the link, it seems you are always directed to the start. I wanted to know for say, java, lang, Maths. In the "Method Summary" table. There are two columns, what is the significance of the value in the left hand side column (for any entry).

    Thanks.

    SPACE MONKEY.

  21. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: char.toHexString();

    If you want to provide a link to a specific class or function, I'd recommend using your browser's "open in new tab" or "copy link location" feature with that class or method. Then you'll get something like this:Math (Java Platform SE 6)

    But if I understand your question, the column on the left contains the type of field or method. Basically, it tells you how that field or method was declared, what type of variable it is (or what type a method returns), whether the method is static, abstract, or something else, etc.
    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. [SOLVED] Why do i need to take 48? char to int.
    By Scotty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 29th, 2010, 10:23 PM
  2. How to take a char value in scanner
    By andaji in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 02:50 AM
  3. coverting to Char?
    By JavaNoob82 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 16th, 2010, 07:52 AM
  4. [SOLVED] Char cannot be dereferenced
    By zukipuu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2010, 10:13 PM
  5. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 02:18 PM