Converting Long Object to String in Java - java

Let's say i have this Long in Java:
Long x=0b01101L;
and i want to convert it into a String using this:
String str1 = Long.toBinaryString(x);
Is there a way to keep the first 0 inside the String?
What i mean is can this:
System.out.println(str1);
print 01101 and not 1101?

This is not possible. When you declare Long x = 0b01101L;, you create a long instance, which contains no information about the String representation of the value it contains. Yes, it equals 13 in decimal, but it also equals 十三 in traditional Chinese/Japanese writing, and it also equals 10 + 3, and so on.
If you want to convert it to a String padded to a certain number of zeroes, you can use String.format("%16s", Integer.toBinaryString(x)).replace(' ', '0'), but you must know in advance how many digits you want to be printed, as this will always return 16 digits.
However, this information is not encoded in a Long (or long). If you need to keep this information, declare your variable as a String xString = "01101";, and convert it to a long using Long.valueOf(xString, 2); when you need to do numerical operations.

Related

How to convert string to number in java ? But i am not looking for hashCode as it wont give unique number. at least i want math logic for same

Hi I want to convert string to some unique number in java.
Exmple: "Production-0-1" to 100021
"Process-23-30" to 12310
And all return number has to be unique.
I dont wanted to use hashCode as they can return duplicate like "Aa" and "BB" has same has code.
Let me know math logic to create this is no method available.
String random = "Production-0-1";
String bi = new BigInteger(random.getBytes("UTF-8")).toString();
BigInteger numBig = new BigInteger(bi);
System.out.println(numBig);
Based on #markspace comments, I tried the following and every time it produces random unique number but beware if you have a very large String and a limited memory space then the output may go out of bound.

Using String.format to remove particular digits from an integer

Say we have a number
int number = 1234
and I want to use formatting to extract the final two digits as a string 34. How can one accomplish that?
I attempted:
String extraction = String.format("%d", number)
but this simply copies the entire number. I'm new to the syntax used by the formatter and can't seem to figure out the syntax that can go inside the characters of a number (or a String, for that matter) and pull them out. I've found a solution using charAt methods but am particularly curious about whether it's possible to accomplish it using formatting techniques.
Going via a String is inefficient and unnecessary to extract the last two digits of an integer.
Simply:
int lastTwoDigits = number % 100;
If you do want to go via a String, you can use:
String s = Integer.toString(number);
s = s.substring(s.length() - 2);
int lastTwoDigits = Integer.parseInt(s);
(note that this handles -ve numbers slightly differently to the first suggestion).

Whats going on behind the scenes with method - public static long parseLong(String s,int radix)

I need to generate a consistent unique Long based on the name of the package. Instead of using "Convert string to long" in Eclipse I think I can achieve the same task at run time by using method public static long parseLong(String s,int radix) ?
I think I need to use something like -
Long.parseLong("Hazelnut", 36) returns 1356099454469L
Which I got from question - How to convert String to long in Java?
Why do I need to set radix to 36 when converting a String that contains characters ?
Well, you're basically to treat it as a number in base 36. So for example, the string "012" would mean 2 + 1 * 36 + 0 * 362. When you run out of digits, you go to letters - so "ABC" would mean 12from 'C' + 11from 'B' * 36 + 10from 'A' * 362.
If you understand how hex works, it's the same except using all the characters in the Latin alphabet.
It'll fail for anything not in 0-9, A-Z, a-z though - and it'll also fail for reasonably long strings; long only works up to 263 which you'll get past reasonably quickly in base 36. "Hazelnut12345" fails, for example. Oh, and this is case-insensitive, so the value for "foo" is the same as for "FOO" - does that fail your uniqueness requirement?
Fundamentally you've only got 264 long values to play with, so unless your package names are pretty restricted you're not going to work out a unique mapping.

Trouble creating a long

long timeStamp = 1312561140157;
gives me an error saying that the litter is too large of type int
I know I can set a long to System.currentTimeMillis() and this number i used is an output from the currentTimeMillis().
Anyone know how to make a literal work when it is this big?
Use this long timeStamp = 1312561140157L. Just append L to the end of the number.
When you create a long you need put an 'L' character at the end of the number.
example:
long timeStamp = 1312561140157L;
You can use lowercase or uppercase 'L', but generally you should use uppercase because l can be easily confused with 1

Java long to binary

something I thought was simple ends up not so much.
I need to convert a long number to binary.
For example:
String b = Integer.toBinaryString(1028);
the output is 10000000100
but when I use Integer.toBinaryString(2199023255552); it does not work. Of course the number is too big for this function and I can't find one that does convert from long.
Any suggestions?
Thank you.
Add an L to indicate its a long<1> and use the Long class<2>:
Long.toBinaryString(2199023255552L);
<1> Constants in java are considered ints unless you specify otherwise.
<2> Integer.toBinaryString() receives an int as parameter, not long.

Categories

Resources