Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible Loss of Precision

Status
Not open for further replies.

dcusick

Technical User
Aug 9, 2000
271
0
0
US
Hey everyone... Hopefully you can help me out with this problem... I have a byte array, that I need to keep as a byte array. I'm trying to put an IP address into it. What happens is that as I put the first section of the IP into it, I get a compile error, "Possible Loss of Precision". The first part of the IP is 128, which is out of the limit for a byte variable, which only goes up to 127. I'm assuming I have to do some type of 2's compliment or something to the 128 in order to put it in correctly. Any ideas? Thanks in advance.

Doug
 
Hi Doug,

If you subtract 256 from any of the numbers that are greater or equal to 128, that would translate 0 to 255 into -128 to 127. You would have to add 256 again to negative number on retrieval.

scrat
 
Here's something else you can do. Put the IP address in a string and convert the string to a char array. Then store the elements of the char array in the byte array.

You have to cast them to suppress the compile errors, but it works because the unicode values for the chars 0-9 (and even the ".") are less than 128.

Here's an example to demonstrate:

String test = "123.456.789.012";
String newtest = "";
char[] ctest = test.toCharArray();
byte[] btest = new byte[ ctest.length ];
// Convert the chars to bytes and back into chars just to demonstrate...
for (int i=0; i<ctest.length; i++)
{
btest = (byte) ctest;
ctest = (char) btest;
}
newtest = new String( ctest );
System.out.println(&quot;Test string <&quot; + test + &quot;>&quot;);
System.out.println(&quot;After conversion <&quot; + newtest + &quot;>&quot;);
 
Assuming the range of numbers is from 0 to 255.
Simply cast your values into bytes. Then when you
wish to interpret the number correctly as an integer,
and them with 0xff, which will convert the byte to
an integer, then discard the 3 most significant bytes.
To understand this code, you'll need to be familiar with arithmetic promotions:
Code:
public class test {

  public static String binary(byte x) {
    StringBuffer s = new StringBuffer();
    byte mask = (byte)0x80;
    while(mask != 0) {
      s.append((((byte)(x&mask))!=0)?1:0);
      mask = (byte)((mask&0xff)>>>1);
    }
    return s.toString();
  }

  public static String binary(int x) {
    StringBuffer s = new StringBuffer();
    int mask = 0x80000000;
    while(mask != 0) {
      s.append((((x&mask))!=0)?1:0);
      mask >>>= 1;
    }
    return s.toString();
  }

  public static void main(String[] args) {
    int[] examples = { 0, 127, 128, 255 };
    byte b;
    for(int i = 0; i < examples.length ; i++) {
      b = (byte)examples[i];
      System.out.println(&quot;byte representation: &quot;+binary(b));
      System.out.println(&quot;byte converted back to int: &quot;+binary((int)b));
      System.out.println(&quot;byte converted, and 3 leftmost bytes discarded: &quot;+binary(b&0xff));
      System.out.println(&quot;byte value as a decimal: &quot;+(b&0xff));
    }
  }
}
Hope this helps :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top