Found 34494 Articles for Programming

Parsing and Formatting a Byte Array into Binary in Java

Samual Sam
Updated on 29-Jun-2020 05:44:09

3K+ Views

Set a BigInteger object.BigInteger one;Now, create a ByteArray.byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 }; one = new BigInteger(byteArr);For Binary, we have used 2 as the toString() method parameter.String strResult = one.toString(2);The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       // BigInteger object       BigInteger one;       byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 };       one = new BigInteger(byteArr);       String strResult = one.toString(2);       System.out.println("ByteArray to Binary = "+strResult);    } }OutputByteArray to Binary = 10000000000000000

Parsing and Formatting a Big Integer into Octal in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:44:57

198 Views

Firstly, take two BigInteger objects and set values.BigInteger one, two; one = new BigInteger("99");Now, parse BigInteger object “two” into Octal.two = new BigInteger("1100", 8); String str = two.toString(8);Above, we have used the following constructor. Here, radix is set as 8 for Octal. for both BigInteger constructor and toString() method.BigInteger(String val, int radix)This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       one = new ... Read More

Parsing and Formatting a Big Integer into Binary in Java

Samual Sam
Updated on 29-Jun-2020 05:45:49

1K+ Views

Firstly, take two BigInteger objects and set values.BigInteger one, two; one = new BigInteger("99"); two = new BigInteger("978");Now, parse BigInteger object “two” into Binary.two = new BigInteger("1111010010", 2); String str = two.toString(2);Above, we have used the following constructor. Here, radix is set as 2 for Binary for both BigInteger constructor and toString() method.BigInteger(String val, int radix)This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;     ... Read More

Parse hexadecimal string to create BigInteger in Java

Samual Sam
Updated on 29-Jun-2020 05:46:43

1K+ Views

To parse the hexadecimal string to create BigInteger, use the following BigInteger constructor and set the radix as 16 for Hexadecimal.BigInteger(String val, int radix)This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.In the below example, we have set the BigInteger and radix is set as 16 −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       String hexStr = "290f98";       one = new BigInteger("250");       // parsing       two = ... Read More

Parse decimal string to create BigInteger in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:47:22

236 Views

To parse the decimal string to create BigInteger, just set the decimal string in the BigInteger.Here is our BigInteger.BigInteger one; String decStr = "687879"; one = new BigInteger(decStr);Let us see another example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       String decStr = "4373427";       one = new BigInteger("250");       two = new BigInteger(decStr);       System.out.println("Result (BigInteger) : " +one);       System.out.println("Result (Parsing Decimal String) : " +two);    } }OutputResult (BigInteger) : 250 Result (Parsing Decimal String) : 4373427

Parse Octal string to create BigInteger in Java

Samual Sam
Updated on 29-Jun-2020 05:48:05

114 Views

To parse the octal string to create BigInteger, use the following BigInteger constructor and set the radix as 8 for Octal −BigInteger(String val, int radix)This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger.In the below example, we have set the BigInteger and radix is set as 8.BigInteger one, two; one = new BigInteger("12"); two = new BigInteger("4373427", 8);The following is the complete example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("12"); ... Read More

Java Program to retrieve the current bits in a byte array in two’s-complement form

karthikeya Boyini
Updated on 29-Jun-2020 05:49:20

227 Views

Use the toByteArray() method to return a byte array containing the two's-complement representation of this BigInteger. The byte array will be in big-endian byte-order: the most significant byte is in the zeroth element.The following is an example to retrieve the current bits in a byte array in two’s-complement form.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       // BigInteger objects       BigInteger bi1, bi2;       byte b1[] = { 0x1, 0x00, 0x00 };       bi1 = new BigInteger(b1);       b1 = bi1.toByteArray();     ... Read More

Format Month in MM format in Java

Samual Sam
Updated on 29-Jun-2020 05:50:56

815 Views

The MM format is for months in two-digits 01, 02, 03, 04, etc. Here, we will use the following.SimpleDateFormat("MM");Let us see an example −// displaying month in MM format SimpleDateFormat simpleformat = new SimpleDateFormat("MM"); String strMonth = simpleformat.format(new Date()); System.out.println("Month in MM format = "+strMonth)Above, we have used the SimpleDateFormat class, therefore the following package is imported −import java.text.SimpleDateFormat;The following is an example −Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = ... Read More

Make the first letter caps and the rest lowercase in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:30:03

844 Views

The following is an example −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "laptop";       System.out.println("Original String = " +str);       // letter one       String strOne = str.substring(0,1).toUpperCase();       // remaining letters       String strTwo = str.substring(1).toLowerCase();       System.out.println("Resultant String = "+strOne + strTwo);    } }OutputOriginal String = laptop Resultant String = Laptop

Left pad a String in Java with zeros

Samual Sam
Updated on 29-Jun-2020 05:30:58

421 Views

The following is our string −String str = "Tim";Now take a StringBuilder object −StringBuilder strBuilder = new StringBuilder();Perform left padding and extend the string length. We have set it till 20, that would include the current string as well. The zeros that will be padded comes on the left. Append the zeros here −while (strBuilder.length() + str.length() < 10) { strBuilder.append('0'); }The following is an example −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Tim";       StringBuilder strBuilder = new StringBuilder();       while (strBuilder.length() + str.length() ... Read More

Advertisements