Convert String Sang BigInteger Trong Java - Deft Blog
Có thể bạn quan tâm
Mục lục
- 1 Convert String (hệ thập phân) sang BigInteger
- 2 Convert String (khác hệ thập phân) sang BigInteger
- 3 Tóm lược
BigInteger thường được dùng để biểu diễn các số nguyên có giá trị lớn vượt ra ngoài giới hạn của kiểu số int nguyên thủy. Tuy nhiên BigInteger vẫn cho phép thực thi các phép toán cộng, trừ, nhân, chia thông qua các method được xây dựng sẵn.
Vì BigInteger dùng để biểu diễn số nguyên có giá trị lớn nên để khởi tạo một BigInteger object thì chúng ta phải thông thường chúng ta phải biểu diễn giá trị này dưới dạng String và sau đó chuyển sang BigInteger.
Convert String (hệ thập phân) sang BigInteger
Để convert String biểu diễn số nguyên ở hệ thập phân sang BigInteger chúng ta có thể sử dụng BigInteger(String value) constructor.
String inputString = "1234567"; BigInteger result = new BigInteger(inputString);Convert String (khác hệ thập phân) sang BigInteger
Đối với các String biểu diễn một số nguyên không phải hệ thập phân thì chúng ta không thể sử dụng BigInteger(String value) constructor. Ví dụ đoạn code sau sẽ gây ra NumberFormatException.
String inputString = "290f98"; new BigInteger(inputString); assertEquals("878", result.toString());Để giải quyết trường hợp này chúng ta có thể sử dụng BigInteger(String value, int radix) constructor:
String inputString = "290f98"; BigInteger result = new BigInteger(inputString, 16); assertEquals("2690968", result.toString());Trong trường hợp này, chúng ta đang chỉ định radix là 16 để chuyển đổi hệ thập lục phân thành số thập phân.
Ngoài ra còn một cách khác đó là chuyển chuổi sang byte[] và sử dụng BigIntenger(byte [] bytes) constructor.
byte[] inputStringBytes = inputString.getBytes(); BigInteger result = new BigInteger(inputStringBytes); assertEquals("290f98", new String(result.toByteArray()));Tóm lược
Như vậy qua bài viết ngắn này chúng ta đã biết cách convert String sang BigInteger trong Java.
Nguồn tham khảo
https://www.baeldung.com/java-string-to-biginteger
Prev Article Next Article 0 0 votes Article Rating Subscribe Login Notify of new follow-up comments new replies to my comments

Bài viết liên quan
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More
In case of sale of your personal information, you may opt out by using the link Do Not Sell My Personal Information
Accept Decline Cookie Settings I consent to the use of following cookies: Necessary Marketing Analytics Preferences Unclassified Cookie Declaration About Cookies Necessary (0) Marketing (0) Analytics (0) Preferences (0) Unclassified (0) Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies. We do not use cookies of this type. Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers. We do not use cookies of this type. Analytics cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously. We do not use cookies of this type. Preference cookies enable a website to remember information that changes the way the website behaves or looks, like your preferred language or the region that you are in. We do not use cookies of this type. Unclassified cookies are cookies that we are in the process of classifying, together with the providers of individual cookies. We do not use cookies of this type. Cookies are small text files that can be used by websites to make a user's experience more efficient. The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies we need your permission. This site uses different types of cookies. Some cookies are placed by third party services that appear on our pages. Cookie SettingsDo you really wish to opt-out? Close
Cancel Confirm wpDiscuz0Would love your thoughts, please comment.x()x| ReplyInsertTừ khóa » ép Kiểu String Sang Char Trong Java
-
Convert String Sang Char Trong Java - CodeGym
-
Chuyển đổi Char Sang String Trong Java - Freetuts
-
Làm Thế Nào để Chuyển đổi / Phân Tích Cú Pháp Từ String Sang Char ...
-
Phương Thức ToCharArray Trong Java String - VietTuts
-
Chuyển đổi Char Sang String Trong Java - Bài Tập Java Có Lời Giải Cơ ...
-
Ép Kiểu Trong Java | How Kteam
-
Chuyển đổi Char Qua String Trong Java - Hướng Dẫn Java
-
Chuyển đổi Char Thành Chuỗi Trong Java - TutorialCup
-
Ép Kiểu Dữ Liệu Trong Java
-
Chuỗi String Trong Java
-
Cách Chuyển Kiểu String Sang Int Trong C/C++ - Lập Trình Không Khó
-
Chuyen String Sang Int Java | âm-nhạ
-
Ép Kiểu Dữ Liệu Trong Java - Lập Trình Từ Đầu
-
Ép Kiểu Trong Java - Giải Mã Thông Tin Về Java Type Conversion