• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

converting hexa decimal into int

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,


How to convert the hexadecimal value 0xbeef into int.
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you looking for manual integer conversion or methods of wrapper classes?
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer.decode() should do the trick, though I believe this is outside the scope of the SCJP exam.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!

0xbeef is already an int, an int literal to be precise. Only it is not written in decimals but in hexadecimals.


int i = 0xbeef;
System.out.println(i);

prints out the value in decimals.


Yours,
Bu.
 
sanla palati
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replys guys.But Burkhard, how can i know that 0xbeef is an interger. I am confused.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hexadecimal is not a separate data type. It is just a way to write numbers - in the base-16 numeric system instead of base-10 (decimal). When an integer literal in Java code starts with "0x", then it's in hexadecimal.

In addition to hexadecimal, you can write numbers in octal (the base-8 number system). A number is interpreted as octal if it starts with "0".
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanla asked


how can i know that 0xbeef is an interger



You're right, at a first glance 0xbeef looks more like something to eat than like an integer for many people.
Similarly, 0xCAFE looks more like something to drink than like the number 51966.

But you have to know about the other two ways of writing an integer.
Perhaps try this as a primer:



I'm terribly sorry that this output ends in a thirteen.
But hopefully you are not supersticious.
Ah, no, it doesn't end in a thirteen but in a nineteen!!!
Phew!

Yours,
Bu.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic