java in the Second, 8, 16, Conversion between decimal

sponsored links
Yesterday fanny asked me know method of java binary conversion, before he had had such a way, saying that the year I also wrote a class to count, super-stupid way, Meng calculation - -!

Ado, that is entered:

Hex decimal conversion:
Integer.toHexString (int i)
Octal decimal conversion
Integer.toOctalString (int i)
Binary decimal conversion
Integer.toBinaryString (int i)
Transformed into decimal hexadecimal
Integer.valueOf ("FFFF", 16). ToString ()
Transformed into decimal octal
Integer.valueOf ("876", 8). ToString ()
Decimal binary switch
Integer.valueOf ("0101", 2). ToString ()

What can be directly converted directly to 2,8,16 and 10 hex hex it?
java.lang.Integer class
parseInt (String s, int radix)
Use the second parameter specifies the base, the string parameter as a signed integer resolution.
examples from jdk:
parseInt ("0", 10) returns 0
parseInt ("473", 10) returns 473
parseInt ("-0", 10) returns 0
parseInt ("-FF", 16) returns -255
parseInt ("1100110", 2) returns 102
parseInt ("2147483647", 10) returns 2147483647
parseInt ("-2147483648", 10) returns -2147483648
parseInt ("2147483648", 10) throws a NumberFormatException
parseInt ("99", throws a NumberFormatException
parseInt ("Kona", 10) throws a NumberFormatException
parseInt ("Kona", 27) returns 411787

Hex conversion how to write (2, 8, 16) no algorithm
Integer.toBinaryString
Integer.toOctalString
Integer.toHexString

Example Two

public class Test (
public static void main (String args []) (

int i = 100;
String binStr = Integer.toBinaryString (i);
String otcStr = Integer.toOctalString (i);
String hexStr = Integer.toHexString (i);
System.out.println (binStr);

)

Example Two
public class TestStringFormat (
public static void main (String [] args) (
if (args.length == 0) (
System.out.println ("usage: java TestStringFormat <a number>");
System.exit (0);
)

Integer factor = Integer.valueOf (args [0]);

String s;

s = String.format ("% d", factor);
System.out.println (s);
s = String.format ("% x", factor);
System.out.println (s);
s = String.format ("% o", factor);
System.out.println (s);
)
)

Other methods:

Integer.toHexString (your 10 hex number);
Such as
String temp = Integer.toHexString (75);
Output temp on the 4b

/ / Enter a 10 hexadecimal numbers and convert it into a 16-band
import java.io. *;
public class toHex (

public static void main (String [] args) (

int input; / / store input data
/ / Create an instance of input string
BufferedReader strin = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Please enter an integer:");
String x = null;
try (
x = strin.readLine ();
) Catch (IOException ex) (
ex.printStackTrace ();
)
input = Integer.parseInt (x);
System.out.println ("Your input number is:" + input); / / output figures received from the keyboard

System.out.println ("it's 16 hex is:" + Integer.toHexString (input ));// with toHexString to convert 16 hex 10 hex
)
)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of java in the Second, 8, 16, Conversion between decimal

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

  • Great collection of java interview topics

    1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract does n

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries