String.format() and hex numbers in Java - java

I'm trying to figure out why String.format() is behaving the way it does.
Context: Systems programming class, writing an assembler.
There is a 5 character hex field in the object file, which I am creating from a value.
Tried using: String.format("%05X", decInt);
This works as intended for positive numbers
(11 -> 0000B)
However it fails for negative numbers
(-1 -> FFFFFFFF instead of FFFFF)
I suppose I could just take a substring of the last 5 characters, but I would still like to figure out why it behaves this way.

The width used in format is always a minimum width. In this case, instead of using sub string operations I would suggest:
String.format("%05X", decInt & 0xFFFFF);

Format width only works to create a minimum number of digits, and only has effect on leading zeroes.
Instead of substring, you could use a bit mask:
String.format("%05X", decInt & 0x0FFFFF)
By the way, 11 -> 0000B, not 0000A as claimed in your question.

Related

Which number has more trailing zeros in binary representation

Given two numbers, A and B, I am wondering what the most efficient way to determine which of them has more trailing zeros (in binary representation) using Java would be.
I could determine the number of trailing zeros for both of them individually, but I don't know if this is the best approach or if there is some binary magic that could do it better.
Note: the numbers can be very large, I need to use BigInteger.
Because you're using BigInteger, you can use BigInteger#getLowestSetBit to determine the number of zero bits to the right of the rightmost one bit.
System.out.println(BigInteger.valueOf(32).getLowestSetBit());
Output:
5
Note: this method returns -1 if the number contains no one bits (i.e. 0).

format numbers in java to remove decimals and add decimals

I am looking at Java code which will perform following two operations:-
If I am sending value 10.00, my method should format it to 1000 and
If I am getting 1000 in response from 3rd party system, I need to convert 1000 to 10.00 in my another method. Simply whatever decimal value (2 digit's after decimal) I'm sending need to remove decimals and whatever Non-decimal Value I'm receiving need to convert to decimals (2 digits after decimals)
I tried to write the code but it's not working expected. Can anyone please guide me? I'm using JDK 7.
You were supposed to provide your own code which doesn't work, so we can help you figure it out and make it work, not posting a problem and let us do all the work.
Input string and convert it to char array with toCharArray() method. Rewrite the array without the dot for the first part. For the second part do the same thing except you insert dot on position you want.
BTW your question is not clear enough, how does the algorithm work, what happens if you get number 15, or 1555...does it translate to 0.15 and 15.55?

Unify 10^x power in a sequence of number

I have a sequence of numbers like this:
1.687155E21
3.981457E19
0.5532155E21
3.018843E21
2.0532155E21
4.5532155E21
3.1637913E19
My problem is how to convert the two numbers which ends with 10^19 to be like the others (10^21). Because after this unification i need to trunc the number to print only something like 3.5.
In C/C++ i know how to work with precision, but in Java I haven't got any idea.
Divide all your number by / 1e19, round to as many decimal digits you want:
168.7155
3.981457
55.32155
301.8843
205.32155
455.32155
3.1637913
Use the Formatter Class to bring them into the desired scientific notation (java.util.Formatter)
I'd suggest something similar as Tomasz Nurkiewicz did, but instead of dividing by 1E19 divide by 1E21, convert them to strings with the required precision using Formatter (see the comment of count0) but not as scientific format, but as a general one. In the end just add E21 to those strings. In the end you should get (I hope, I got the idea correctly)
1.687155E21
0.03981457E21
0.5532155E21
3.018843E21
2.0532155E21
4.5532155E21
0.031637913E21
Can't you just multiply the E19 numbers by 10 ^ 2 = 100?
After they have been normalized to E21, you should be able to divide all of them by 10^21 (if they're floats), and they will all be in the range of 0-9.999...

Generation if a unique id of size less than 11 bytes from a string

i am developing a piece of code to generate a unique hexadecimal value from an input string. The output size must be less than 11 bytes which comes as requirement.Can someone please give me an insight into this. I have done the string to binary conversion and then the hexagonal mapping which produces a combination of alphanumeric characters but the size is always greater tha 11 bytes. I also need to regenerate the input from this unique id..Is that possible.....
Thanks in adavance
If your result must be absolutely unique and your input can be any length, then your task is impossible.
Think of it that way: how many different combinations of 11 bytes are there? 25611 (or 211*8=288).
That's a big number, right? Yes, but it's not big enough.
For simplicities sake we'll talk about ASCII strings only, so we have 128 different values (in reality there are many more possibilities for a character in a Java String, but the principle stays the same. For simplicities sake we also ignore that a \0 character in a String is kind of unlikely).
Now, there are 12813 different 13-character ASCII strings. That's 27*13 or 291 different combinations. Obviously you can't have a unique id out of 288 possible ids for 291 different strings.
Less than 11 bytes means maximum 10 bytes.
8^10 is 1073741824.
2^80 is a huge number.
So if you take your hexvalue, and take it modulo that number, you should fit into the 10 bytes. Convert the remainder back to hex.
Regenerating the input will not be possible. If your input is allowed to be longer than 11 bytes, it will not be possible. That would be an endless compression.

need help with parseInt

how can i get for example the integer codeInt=082 from String code='A082'
i have tried this:
int codeInt = Integer.parseInt(code.substring(1,4));
and i get codeInt=82 ,it leaves the first 0 but i want the full code '082'.
i thought of parseInt(String s, int radix) but i don't know how .
any help will be appreciated .
thanks.
An integer just stores a number. The numbers 82 and 082 (and 0082 and 000000082 for that matter) are exactly the same (unless you put them into source code in some languages in that manner, then you'll get a compiler error)1.
If you desperately need the leading zero, then you should either leave it as a string, or format the number appropriately for output later.
1 Due to the C designers having the ingenious idea that writing octal constants with a preceding zero would be cool. As if something like 0o123 would have been that hard to implement once you already got 0xf00 ...
The number 82 and 082 and 0082 is mathematically the same number, and is represented by the same sequence of bits. You can't encode the number of leading zeroes in an int (although you can certainly print it with whatever format you choose).
Note also that the number 082 is different from the Java literal 082, which is an (invalid) octal literal.
int i = 010;
System.out.println(i); // this prints 8
082 is not an integer. It's a string representing the integer 82. If you require leading zeros to be left untouched, you will need to work with strings. If you only need it to print 082, you can use java.text.MessageFormat or System.out.format() or other, similar solutions to print it that way.
If you want 0000123 then you need to threat a variable as a String instead of Integer. Simply: 123 is equal to 000123 and 0123 and 0000...1 billion zeros here...000123.
But if you just want to display a number with fixed length then use System.out.format().

Categories