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.
Related
I have a big number in a database; in this case, 10,000,000,000. Whenever I use that information for something, like sending a message with it, instead of 10,000,000,000, it says 1E10, and I really do not want that.
Can I avoid that in any way?
If I go to the database, the value is 10,000,000,000.
It's the same number, just represented in scientific notation.
Since you don't describe how you are storing the value, you can use DecimalFormat#getNumberInstance to help format it to one that doesn't contain the scientific notation.
double foo = 10000000000L;
System.out.println(foo);
System.out.println(DecimalFormat.getIntegerInstance().format(foo));
This outputs:
1.0E10
10,000,000,000
I have the following simple piece of code which is intended to detect that a given IPv4 address indeed only has numeric values (that is after the dots have been stripped):
import edu.gcc.processing.exceptions.net.IPAddressNumericException;
//Get the IP address
String address = "239.255.255.255";
//Check to see if this is a number
try {
String IPNumbers = address.replace(".", "");
Integer.parseInt(IPNumbers);
} catch (NumberFormatException e) {
System.out.print(e.getMessage());
}
For some reason, the NumberFormatException is fired, and I get this error:
For input string: "239255255255"
Could someone please help me understand this? The parseInt() method works on smaller numbers, such as 127001.
Thank you for your time.
try using Long.parseLong(IPNumbers)
Why not use a regular expression, or break it down into its components by using split(".")?
There are other limitations besides needing to consist solely of digits. For example:
666.666.666.666
This will parse just fine, but is an... unlikely IP.
Breaking it up into its parts lets you determine (a) that it has four parts, and (b) that each part actually makes sense in the context of an IP address.
You could also use an InetAddress implementation, or InetAddressValidator from Apache Commons.
For very large integers you may want to use the BigInteger class (or BigDecimal), as the values may exceed the limits of Integer.
Integer Limits:
Minimum : -2147483648
Maximum : 2147483647
Using BigInteger:
string s = "239255255255";
BigInteger yourNumber = new BigInteger(s);
239255255255 is too big to be held by an Integer. Try using a BigInteger or a BigDecimal.
Range of an Integer is -2,147,483,648 to 2,147,483,647, the number you have above mentioned is not included in these range, so use long , the range of long primitive type is in between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.
So for your case its better to use Long.parseLong() function to convert such large number to long type.
The logical step up from an integer is a long
This is basically what I am trying to do
// ... some code, calculations, what have you ...
long timeToAdd = returnTimeToAddInLongFormat();
// lets output the long type now, and yes i need the width and precision.
System.out.printf("Time to add: %13.10ld", timeToAdd);
I've read most of the google searches around the topic and think I understand how to do it conceptually, but the JRE keeps throwing me a UnknownFormatConversionException and telling me my input size modifier l doesnt work.
Is there another way to do this, or did I miss something small?
Java treats all integer values as d, there is no ld. Even byte and BigInteger is a d type. It also assumes integers have no decimal places. If you want to show 10 zeros, you can convert to double first and use f
I have a large string (an RSS Article to be more precise) and I want to get the word in a specific startIndex and endIndex. String provides the substring method, but only using ints as its parameters. My start and end indexes are of type long.
What is the best way to get the word from a String using start and end indexes of type long?
My first solution was to start trimming the String and get it down so I can use ints. Didn't like where it was going. Then I looked at Apache Commons Lang but didn't find anything. Any good solutions?
Thank you.
Update:
Just to provide a little more information.
I am using a tool called General Architecture for Text Engineering (GATE) which scans a String and returns a list of Annotations. An annotation holds a type of a word (Person, Location, etc) and the start and end indexes of that word .
For the RSS, I use ROME, which reads an RSS feed and contains the body of the article in a String.
There is no point doing this on a String because a String can hold at 2^31 - 1 characters. Internally the string's characters are held in a char[], and all of the API methods use int as the type for lengths, positions and offsets.
The same restriction applied to StringBuffer or StringBuilder; i.e. an int length.
A StringReader is backed by a String, so that won't help.
Both CharBuffer and ByteBuffer have the same restriction; i.e. an int length.
A bare array of a primitive type is limited to an int length.
In short, you are going to have to implement your own "long string" type that internally holds its characters in (for example) an array of arrays of characters.
(I tried a Google search but I couldn't spot an existing implementation of long strings that looked credible. I guess there's not a lot of call for monstrously large strings in Java ...)
By the way, if you anticipate that the strings are never going to be this large, you should just convert your long offsets to int. A cast would work, but you might want to check the range and throw an exception if you ever get an offset >= 2^31.
A String is backed by a char[], and arrays can only be indexed with ints (and can consequently only hold 231 characters). If you have long indexes, just cast them to ints - if they're larger than Integer.MAX_VALUE, your program is broken.
You'd better use a java.io.Reader. This class supports the methods skip(long n) and read(char[] cbuf). But please note they return a long (how many bytes were skipped / read), so you need to call those methods in a loop.
Probably it would be better not to use String but StringReader.
Sorry if my question sounds dumb. But some time small things create big problem for you and take your whole time to solve it. But thanks to stackoverflow where i can get GURU advices. :)
So here is my problem. i search for a word in a string and put 0 where that word occur.
For example : search word is DOG and i have string "never ever let dog bite you" so the string
would be 000100 . Now when I try to convert this string into INT it produce result 100 :( which is bad. I also can not use int array i can only use string as i am concatinating it, also using somewhere else too in program.
Now i am sure you are wondering why i want to convert it into INT. So here my answer. I am using 3 words from each string to make this kind of binary string. So lets say i used three search queries like ( dog, dog, ever ) so all three strings would be
000100
000100
010000
Then I want to SUM them it should produce result like this "010200" while it produce result "10200" which is wrong. :(
Thanks in advance
Of course the int representation won't retain leading zeros. But you can easily convert back to a String after summing and pad the zeros on the left yourself - just store the maximum length of any string (assuming they can have different lengths). Or if you wanted to get even fancier you could use NumberFormat, but you might find this to be overkill for your needs.
Also, be careful - you will get some unexpected results with this code if any word appears in 10 or more strings.
Looks like you might want to investigate java.util.BitSet.
You could prefix your value with a '1', that would preserve your leading 0's. You can then take that prefix into account you do your sum in the end.
That all is assuming you work through your 10 overflow issue that was mentioned in another comment.
Could you store it as a character array instead? Your using an int, which is fine, but your really not wanting an int - you want each position in the int to represent words in a string, and you turn them on or off (1 or 0). Seems like storing them in a character array would make more sense.