久久久久久久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(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产精品久久久久永久免费观看 | 国产在线麻豆精品入口 | 久久久久久久综合 | 99精品欧美一区二区三区综合在线 | 国产精品中文字幕在线观看 | 在线观看av网站 | 国产精品99视频 | www.午夜| 日韩欧美在线一区 | 欧美日韩国产一区二区三区 | 国产a视频 | 成人免费观看视频 | 亚洲欧美日韩精品久久亚洲区 | 久久久久国产精品一区二区 | 国产三区视频在线观看 | 免费国产黄网站在线观看视频 | 日韩av成人在线 | 国产999精品久久久久久 | 99视频在线 | 日本在线网址 | 欧美精品一区二区三区在线 | 午夜爽爽爽男女免费观看 | 精品国产乱码久久久久久老虎 | 日本高清在线一区 | 四虎永久影院 | 日韩一区二区黄色片 | 久久久久久久久久久久久久国产 | 九九热视频这里只有精品 | 亚洲精品久久久 | 亚洲综合大片69999 | 国产精品久久久久久久久久99 | 黄色av观看 | 免费成人国产 | 久久免费高清视频 | 99成人免费视频 | 日韩网站免费观看 | 欧美在线成人影院 | 可以在线看的黄色网址 | 亚洲va在线va天堂va狼色在线 | 国产精品国产三级国产aⅴ中文 | 综合久 |