convertNumber

本文介绍Java中不同进制之间的转换方法,包括十进制与其他进制间的相互转换。通过Integer类的方法如toBinaryString(), toOctalString(), toHexString()等实现二进制、八进制、十六进制到十进制的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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. }  
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:cc="http://java.sun.com/jsf/composite" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"> <cc:interface/> <cc:implementation> <p:dialog header="周达成率记录" id="weekSummaryLogDia" widgetVar="weekSummaryLogDia" position="center" modal="true"> <p:dataTable id="summaryLog" widgetVar="summaryLog" value="#{wtWeekSummaryBean.showWeekSummaryLogs}" var="entity" currentPageReportTemplate="{startRecord}-{endRecord} of {totalRecords} records" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" emptyMessage="没有数据" rows="10" resizableColumns="true" style="width:450px;" rowHover="true" rowIndexVar="rowvar"> <p:column headerText="序号" style="width:15px;text-align: center;"> <h:outputText value="#{rowvar+1}"/> </p:column> <p:column headerText="周id" style="width:60px;text-align:center;"> <h:outputText value="#{entity.weekId}"/> </p:column> <p:column headerText="工号" sortBy="#{empNo}" style="width:60px;text-align:center;"> <h:outputText value="#{entity.empNo}"/> </p:column> <p:column headerText="姓名" width="50" style="text-align:center;"> <h:outputText value="#{entity.empName}"/> </p:column> <p:column headerText="网页工时" style="width:60px;text-align:right"> <h:outputText value="#{entity.webWt}"> <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/> </h:outputText> </p:column> <p:column headerText="OPT工时" style="width:60px;text-align:right"> <h:outputText value="#{entity.optWt}"> <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/> </h:outputText> </p:column> <p:column headerText="品质扣分" style="width:60px;text-align:right"> <h:outputText value="#{entity.deductWt}"> <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/> </h:outputText> </p:column> <p:column headerText="合计产出" style="width:60px;text-align:right"> <h:outputLabel value="#{entity.totalWt}"> <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/> </h:outputLabel> </p:column> <p:column headerText="实际出勤" style="width:60px;text-align:right"> <h:outputText value="#{entity.onDutyHour}"/> </p:column> <p:column headerText="出勤率" style="width:50px;text-align:right"> <h:outputText value="#{entity.onDutyRate}"> <f:convertNumber type="percent" maxFractionDigits="1"/> </h:outputText> </p:column> <p:column headerText="达标率" style="width:50px;text-align:right"> <h:outputText value="#{entity.uptoStandardRate}"> <f:convertNumber type="percent" maxFractionDigits="2" /> </h:outputText> </p:column> <p:column headerText="创建时间" style="display:none;"> <h:outputText value="#{entity.createTime}"> <f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss" timeZone="GMT+8"/> </h:outputText> </p:column> <p:column headerText="更新时间" style="display:none;"> <h:outputText value="#{entity.updateTime}"> <f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss" timeZone="GMT+8"/> </h:outputText> </p:column> </p:dataTable> </p:dialog> </cc:implementation> </html> 我JSF 碎片页面是这样的,可能别的项目调用后这边弹窗显示数据
最新发布
08-08
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:cc="http://java.sun.com/jsf/composite" xmlns:lkmcc="http://java.sun.com/jsf/composite/lkmComponent" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" xmlns:c="http://java.sun.com/jsp/jstl/core"> <cc:interface/> <cc:implementation> <p:dialog header="周达成率记录" id="weekSummaryLogDia" widgetVar="weekSummaryLogDia" position="center" modal="true"> <p:dataTable id="summaryLog" widgetVar="summaryLog" value="#{wtWeekSummaryBean.showWeekSummaryLogs}" var="entity" currentPageReportTemplate="{startRecord}-{endRecord} of {totalRecords} records" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" emptyMessage="没有数据" rows="10" resizableColumns="true" style="width:450px;" rowHover="true" rowIndexVar="rowvar"> <p:column headerText="序号" style="width:15px;text-align: center;"> <h:outputText value="#{rowvar+1}"/> </p:column> <p:column headerText="周id" style="width:60px;text-align:center;"> <h:outputText value="#{entity.weekId}"/> </p:column> <p:column headerText="工号" sortBy="#{empNo}" style="width:60px;text-align:center;"> <h:outputText value="#{entity.empNo}"/> </p:column> <p:column headerText="姓名" width="50" style="text-align:center;"> <h:outputText value="#{entity.empName}"/> </p:column> <p:column headerText="网页工时" style="width:60px;text-align:right"> <h:outputText value="#{entity.webWt}"> <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/> </h:outputText> </p:column> <p:column headerText="OPT工时" style="width:60px;text-align:right"> <h:outputText value="#{entity.optWt}"> <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/> </h:outputText> </p:column> <p:column headerText="品质扣分" style="width:60px;text-align:right"> <h:outputText value="#{entity.deductWt}"> <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/> </h:outputText> </p:column> <p:column headerText="合计产出" style="width:60px;text-align:right"> <h:outputLabel value="#{entity.totalWt}"> <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/> </h:outputLabel> </p:column> <p:column headerText="实际出勤" style="width:60px;text-align:right"> <h:outputText value="#{entity.onDutyHour}"/> </p:column> <p:column headerText="出勤率" sortBy="#{onDutyRate}" style="width:50px;text-align:right"> <h:outputText value="#{entity.onDutyRate}"> <f:convertNumber type="percent" maxFractionDigits="1"/> </h:outputText> </p:column> <p:column headerText="达标率" sortBy="#{uptoStandareRate}" style="width:50px;text-align:right"> <h:outputText value="#{entity.uptoStandardRate}"> <f:convertNumber type="percent" maxFractionDigits="2" /> </h:outputText> </p:column> <p:column headerText="创建时间" style="display:none;"> <h:outputText value="#{entity.createTime}"> <f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss" timeZone="GMT+8"/> </h:outputText> </p:column> <p:column headerText="更新时间" style="display:none;"> <h:outputText value="#{entity.updateTime}"> <f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss" timeZone="GMT+8"/> </h:outputText> </p:column> </p:dataTable> </p:dialog> </cc:implementation> </html> 我新写的jsf碎片 别的项目要引用 这样写有没有问题
08-08
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: DefaultTableModel cannot be resolved to a type DefaultTableModel cannot be resolved to a type at com.NumberConverterGUI.convertNumber(NumberConverterGUI.java:92) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2314) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:407) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6621) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3398) at java.desktop/java.awt.Component.processEvent(Component.java:6386) at java.desktop/java.awt.Container.processEvent(Container.java:2266) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4996) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4828) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4828) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714) at java.base/java.security.AccessController.doPrivileged(AccessController.java:400) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:98) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.base/java.security.AccessController.doPrivileged(AccessController.java:400) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值