zoukankan      html  css  js  c++  java
  • java中 8进制 10进制 2进制 16进制 相互转换

    1. 十进制转成十六进制:   
    2.   
    3. Integer.toHexString(int i)   
    4.   
    5. 十进制转成八进制   
    6.   
    7. Integer.toOctalString(int i)   
    8.   
    9. 十进制转成二进制   
    10.   
    11. Integer.toBinaryString(int i)   
    12.   
    13. 十六进制转成十进制   
    14.   
    15. Integer.valueOf("FFFF",16).toString()   
    16.   
    17. 八进制转成十进制   
    18.   
    19. Integer.valueOf("876",8).toString()   
    20.   
    21. 二进制转十进制   
    22.   
    23. Integer.valueOf("0101",2).toString()   
    24.   
    25.   
    26.   
    27. 有什么方法可以直接将2,8,16进制直接转换为10进制的吗?   
    28.   
    29. java.lang.Integer类   
    30.   
    31. parseInt(String s, int radix)   
    32.   
    33. 使用第二个参数指定的基数,将字符串参数解析为有符号的整数。   
    34.   
    35. examples from jdk:   
    36.   
    37. parseInt("0"10) returns 0   
    38.   
    39. parseInt("473"10) returns 473   
    40.   
    41. parseInt("-0"10) returns 0   
    42.   
    43. parseInt("-FF"16) returns -255   
    44.   
    45. parseInt("1100110"2) returns 102   
    46.   
    47. parseInt("2147483647"10) returns 2147483647   
    48.   
    49. parseInt("-2147483648"10) returns -2147483648   
    50.   
    51. parseInt("2147483648"10throws a NumberFormatException   
    52.   
    53. parseInt("99",throws a NumberFormatException   
    54.   
    55. parseInt("Kona"10throws a NumberFormatException   
    56.   
    57. parseInt("Kona"27) returns 411787   
    58.   
    59.   
    60.   
    61. 进制转换如何写(二,八,十六)不用算法   
    62.   
    63. Integer.toBinaryString   
    64.   
    65. Integer.toOctalString   
    66.   
    67. Integer.toHexString   
    68.   
    69.   
    70.   
    71.   
    72.   
    73. 例二   
    74.   
    75.   
    76.   
    77. public class Test{   
    78.   
    79. public static void main(String args[]){   
    80.   
    81.   
    82.   
    83. int i=100;   
    84.   
    85. String binStr=Integer.toBinaryString(i);   
    86.   
    87. String otcStr=Integer.toOctalString(i);   
    88.   
    89. String hexStr=Integer.toHexString(i);   
    90.   
    91. System.out.println(binStr);   
    92.   
    93.   
    94.   
    95. }   
    96.   
    97.   
    98.   
    99.   
    100.   
    101.   
    102.   
    103. 例二   
    104.   
    105. public class TestStringFormat {   
    106.   
    107. public static void main(String[] args) {   
    108.   
    109. if (args.length == 0) {   
    110.   
    111. System.out.println("usage: java TestStringFormat <a number>");   
    112.   
    113. System.exit(0);   
    114.   
    115. }   
    116.   
    117.   
    118.   
    119. Integer factor = Integer.valueOf(args[0]);   
    120.   
    121.   
    122.   
    123. String s;   
    124.   
    125.   
    126.   
    127. s = String.format("%d", factor);   
    128.   
    129. System.out.println(s);   
    130.   
    131. s = String.format("%x", factor);   
    132.   
    133. System.out.println(s);   
    134.   
    135. s = String.format("%o", factor);   
    136.   
    137. System.out.println(s);   
    138.   
    139. }   
    140.   
    141. }   
    142.   
    143.   
    144.   
    145.   
    146.   
    147.   
    148.   
    149. 其他方法:   
    150.   
    151.   
    152.   
    153. Integer.toHexString(你的10进制数);   
    154.   
    155. 例如   
    156.   
    157. String temp = Integer.toHexString(75);   
    158.   
    159. 输出temp就为 4b   
    160.   
    161.   
    162.   
    163.   
    164.   
    165.   
    166.   
    167. //输入一个10进制数字并把它转换成16进制   
    168.   
    169. import java.io.*;   
    170.   
    171. public class toHex{   
    172.   
    173.   
    174.   
    175. public static void main(String[]args){   
    176.   
    177.   
    178.   
    179. int input;//存放输入数据   
    180.   
    181. //创建输入字符串的实例   
    182.   
    183. BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));   
    184.   
    185. System.out.println("请输入一个的整数:");   
    186.   
    187. String x=null;   
    188.   
    189. try{   
    190.   
    191. x=strin.readLine();   
    192.   
    193. }catch(IOException ex){   
    194.   
    195. ex.printStackTrace();   
    196.   
    197. }   
    198.   
    199. input=Integer.parseInt(x);   
    200.   
    201. System.out.println ("你输入的数字是:"+input);//输出从键盘接收到的数字   
    202.   
    203.   
    204.   
    205. System.out.println ("它的16进制是:"+Integer.toHexString(input));//用toHexString把10进制转换成16进制   
    206.   
    207. }   
    208.   
    209. }  
  • 相关阅读:
    [MySQL系列] SELECT STRAIGHT_JOIN优化join查询技巧
    [MySQL系列] 使用STRAIGHT_JOIN 优化inner join查询排序索引问题
    [Golang系列] GOFLY在线客服-代码块和作用域-GO语言实现开源独立部署客服系统
    [Laravel系列] 解决laravel中paginate()与distinct() count语句错误问题
    [Laravel系列] 框架中增加记录access log的日志中间件
    [Laravel系列] 解决Laravel中NotFoundHttpException异常
    [Golang系列] GOFLY在线客服-使用golang中的全局变量-GO语言实现开源独立部署客服系统
    [前端]GOFLY在线客服-使用vh、vw使div元素充满屏幕-GO语言实现开源独立部署客服系统
    [前端] GOFLY在线客服-使用css overflow-y属性实现超出高度出滚动条-GO语言实现开源独立部署客服系统
    GOFLY在线客服-使用reconnect-websocket.js实现断线自动重连机制-GO语言实现开源独立部署客服系统
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6469984.html
Copyright © 2011-2022 走看看