久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

java:將二進制字符串轉換為int

java: convert binary string to int(java:將二進制字符串轉換為int)
本文介紹了java:將二進制字符串轉換為int的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試將幾個二進制字符串轉換回 int.但是它不會轉換我所有的二進制字符串,給我留下一個 java.lang.NumberFormatException 異常.這是我的帶有 3 個二進制字符串的測試代碼:

I'm trying to convert a couple of binary strings back to int. However it doesn't convert all my binary strings, leaving me a java.lang.NumberFormatException exception. Here is my test code with 3 binary string:

public class Bin {

    public static void main(String argvs[]) {
            String binaryString ;
            binaryString = Integer.toBinaryString(~0);
            //binaryString = Integer.toBinaryString(~1);
            //binaryString = "1010" ;
            int base = 2;
            int decimal = Integer.parseInt(binaryString, base);
            System.out.println("INPUT=" + binaryString + " decimal=" + decimal) ;
    }
}

如果我轉換1010"效果很好,但是當我嘗試轉換另外兩個之一時,我得到了異常.有人可以向我解釋這是為什么嗎?

If I convert the "1010" it works great, but when I try to convert one of the other two I get the exception. Can someone explain to me why this is ?

干杯

推薦答案

來自 http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#toBinaryString(int) :toBinaryString() 方法將其輸入轉換為無符號整數值是參數加上 232 如果參數為負數"的二進制表示.

From http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#toBinaryString(int) : the toBinaryString() method converts its input into the binary representation of the "unsigned integer value is the argument plus 232 if the argument is negative".

來自 http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int) :parseInt() 方法拋出 NumberFormatException 如果字符串表示的值不是 int 類型的值".

From http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int) : the parseInt() method throws NumberFormatException if "The value represented by the string is not a value of type int".

注意~0~1都是負數(分別為-1和-2),所以會轉換成232的二進制表示-1 和 232-2 分別不能用 int 類型的值表示,所以導致 NumberFormatException 你所看到的.

Note that both ~0 and ~1 are negative (-1 and -2 respectively), so will be converted to the binary representations of 232-1 and 232-2 respectively, neither of which can be represented in a value of type int, so causing the NumberFormatException that you are seeing.

這篇關于java:將二進制字符串轉換為int的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 激情欧美一区二区三区中文字幕 | 精品免费国产一区二区三区 | 五月网婷婷| 国产免费一区二区 | 国产精品欧美一区二区三区不卡 | av免费在线观看网站 | 欧美 日韩 国产 在线 | 色婷婷亚洲国产女人的天堂 | 亚洲国产精品人人爽夜夜爽 | 日韩精品成人一区二区三区视频 | 日韩av.com| 中文字幕日韩专区 | 精品一二 | 国产精品久久久久久久久久久久午夜片 | 久久久久亚洲精品中文字幕 | 一区在线免费视频 | 一级黄色淫片 | 亚洲在线视频 | 91人人视频在线观看 | 亚洲第一成人影院 | 国产精品自拍视频 | 久久99精品久久久久子伦 | 韩日一区二区三区 | 精品一区二区三区免费毛片 | 99久久精品免费看国产四区 | 国产高清视频 | 国产精品第2页 | 国产精品毛片一区二区在线看 | 日日操夜夜操天天操 | 日本黄色影片在线观看 | 国产精品免费视频一区 | 色爱综合网 | 国产精品福利视频 | 欧美一级免费看 | 欧美精品福利 | 日日干干 | 国产视频精品视频 | 黄一区二区三区 | 色爽女| 国产精品成人一区二区 | 中文字幕在线网 |