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

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
)
)

分类:Java 时间:2010-04-02 人气:205
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.693 (s). 12 q(s)