Datatype for the number - java

How should I change the geometry datatype to String in Java?

Use BigInteger.
BigInteger bi = new BigInteger("01060000000100000...", 16);

You could use BigInteger. It has a constructor taking a String and a radix as an argument.
So:
new BigInteger(theString, 16);

Related

Convert.ToInt64 equivalent in java

I try the following code in c# and it give me the result as follow:
long dec1 = Convert.ToInt64("B62FD56EFD5B375D", 16);
result : -531879796222753398
I am trying to do this in java, but I always get NumberFormatException, because there are alphanumeric inside the String. What I code in java is:
Long.parseLong("B62FD56EFD5B375D", 16);
May I know what is the equivalent of this in java?
You can use Long.parseUnsignedLong in Java to get the same result.
long result = Long.parseUnsignedLong("B62FD56EFD5B375D", 16);
Maximum value is 9,223,372,036,854,775,807 (inclusive) for a long value. When the value B62FD56EFD5B375D parsed it is 13,127,946,111,482,018,682 which is unable to hold in a long value.
So instead use BigInteger.
long dec1 = new BigInteger("B62FD56EFD5B375D", 16).longValue();
you can try with BigInteger
BigInteger value = new BigInteger(hex, 16);

Does number.charAt() work on hexadecimals too?

Currently working with Java and wondering how to convert a string that represents hexadecimals into integers? Will number.charAt() suffice for this?
You could use Integer.parseInt(String, int) where the second argument is the radix.
int v = Integer.parseInt("A1", 16);
if you actually need to parse hex digits you could use Character.digit(char, int) where the second argument is (again) a radix
int v = Character.digit('a', 16);

Datatype to store 20 digit number

I have a number of 20 digit, which datatype will support to store this number? I have tried long, double but I 'm getting out of range.
Number = 48565664968483514466
Then I have to convert this number to Base36 to generate the barcode.
BigInteger:
The BigInteger class allocates as much memory as it needs to hold all the bits of data it is asked to hold and also provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.
Declare it as
BigInteger bi1 = new BigInteger("12345678900123");
To convert your number in base 36:
BigInteger number = new BigInteger("48565664968483514466");
String numberInBase36 = number.toString(36);
System.out.println(numberInBase36);
when I'm trying to use new BigInteger(number), I'm getting literal of type int is out of range
The syntax you are looking for is
BigInteger n = new BigInteger("48565664968483514466");
with the numeric literal as a String, since the primitive integer literals cannot hold a number this large.
BigInteger i = new BigInteger("48565664968483514466");

How do you convert a binary string like "10010010101" to a BigInteger decimal representation?

I know that integers have Integer.parseInt(string, 2); but when the value of the decimal is very large, how can we use BigInteger in this case?
BigInteger(giantBinaryString, 2);
There's a constructor for that.
Use the constructor BigInteger(String s)
documentation: http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html
For a binary integer represented as a string, use
BigInteger(binaryString, 2);
In this case you can construct a BigInteger from a string which
denotes a very very large integer. This is the purpose of BigInteger.
If you don't use BigInteger it will overflow your int variable.
You can do this by calling.
BigInteger b = new BigInteger(str, radix);
where you pass in radix = 2 or radix = 10 or whatever
you need (seems you need 2 in your case) and str is the value.
Then you can use
b.toString(radix);
to output your BigInteger value in whatever radix you want.
I think you are looking for this BigInteger(binaryString, 2);

How to convert BigInteger to String in java

I converted a String to BigInteger as follows:
Scanner sc=new Scanner(System.in);
System.out.println("enter the message");
String msg=sc.next();
byte[] bytemsg=msg.getBytes();
BigInteger m=new BigInteger(bytemsg);
Now I want my string back. I'm using m.toString() but that's giving me the desired result.
Why? Where is the bug and what can I do about it?
You want to use BigInteger.toByteArray()
String msg = "Hello there!";
BigInteger bi = new BigInteger(msg.getBytes());
System.out.println(new String(bi.toByteArray())); // prints "Hello there!"
The way I understand it is that you're doing the following transformations:
String -----------------> byte[] ------------------> BigInteger
String.getBytes() BigInteger(byte[])
And you want the reverse:
BigInteger ------------------------> byte[] ------------------> String
BigInteger.toByteArray() String(byte[])
Note that you probably want to use overloads of String.getBytes() and String(byte[]) that specifies an explicit encoding, otherwise you may run into encoding issues.
Use m.toString() or String.valueOf(m). String.valueOf uses toString() but is null safe.
Why don't you use the BigInteger(String) constructor ? That way, round-tripping via toString() should work fine.
(note also that your conversion to bytes doesn't explicitly specify a character-encoding and is platform-dependent - that could be source of grief further down the line)
You can also use Java's implicit conversion:
BigInteger m = new BigInteger(bytemsg);
String mStr = "" + m; // mStr now contains string representation of m.
When constructing a BigInteger with a string, the string must be formatted as a decimal number. You cannot use letters, unless you specify a radix in the second argument, you can specify up to 36 in the radix. 36 will give you alphanumeric characters only [0-9,a-z], so if you use this, you will have no formatting. You can create: new BigInteger("ihavenospaces", 36)
Then to convert back, use a .toString(36)
BUT TO KEEP FORMATTING:
Use the byte[] method that a couple people mentioned. That will pack the data with formatting into the smallest size, and allow you to keep track of number of bytes easily
That should be perfect for an RSA public key crypto system example program, assuming you keep the number of bytes in the message smaller than the number of bytes of PQ
(I realize this thread is old)
To reverse
byte[] bytemsg=msg.getBytes();
you can use
String text = new String(bytemsg);
using a BigInteger just complicates things, in fact it not clear why you want a byte[]. What are planing to do with the BigInteger or byte[]? What is the point?
String input = "0101";
BigInteger x = new BigInteger ( input , 2 );
String output = x.toString(2);
//How to solve BigDecimal & BigInteger and return a String.
BigDecimal x = new BigDecimal( a );
BigDecimal y = new BigDecimal( b );
BigDecimal result = BigDecimal.ZERO;
BigDecimal result = x.add(y);
return String.valueOf(result);
https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html.
Every object has a toString() method in Java.

Categories