Why Is the Range of Bytes -128 to 127 in Java

How can i send a byte daha which is greater than 127 in Java?

Just send (byte) 0xa0. Yes, as a Java byte it will be negative, but it will still have the right bit pattern, which is all the RFID will care about.

Basically, you rarely need to think of bytes as numbers as such - it's more usual to think of them as sequences of bits, which happen to map to numbers.

For example, suppose you have a file consisting of a single byte, with the bits 11111111. If you read the data for that in Java, you'll get a byte of -1. If you read the data for that in C#, you'll get a byte of 255. It's the same data, just viewed in a slightly different way.

how to read bytes bigger than 127 in java?

byte b = -66;
int i = b & 0xff;

Using byte in java

You are casting the integer variable a into a byte variable being stored in an array. However the variable you are printing is a which still is an integer.

Size of integers?

That's right, bytes come in sizes of 8 bits each1, and you usually can't subdivide them.

1 Usually (for pedants and troglodytes).

How big a performance difference will byte have over int?

Will this have a significant improvement in performance in the end? If not, at what point (i.e. number of variables in question or calculations per second) will the performance increase be noticeable?

It won't increase performance. In fact, I'd expect calculations to be slower if you use byte instead of int. A typical native instruction set doesn't support byte-wise arithmetic. Instead it will be implemented using 32 bit instructions, and extra instructions will be needed to convert between 8 and 32 bit quantities.

The only situation where I would expect you to see a performance improvement would be if you were doing large-scale operations on arrays of numbers. That's due to the fact that a large array of bytes occupied 1/4 the memory of a large array of ints (with the same length). Since the limiting factor for large-scale is memory bandwidth, you will see a performance improvement. (There will also be secondary performance benefits, due to the effect of reducing memory demand on the GC and on the virtual memory system.)

Note that you do not get this for individual byte fields or local variables, because the JVM aligns these on word boundaries.

Going from 127.0.0.1 to 2130706433, and back again

String to int:

int pack(byte[] bytes) {
int val = 0;
for (int i = 0; i < bytes.length; i++) {
val <<= 8;
val |= bytes[i] & 0xff;
}
return val;
}

pack(InetAddress.getByName(dottedString).getAddress());

Int to string:

byte[] unpack(int bytes) {
return new byte[] {
(byte)((bytes >>> 24) & 0xff),
(byte)((bytes >>> 16) & 0xff),
(byte)((bytes >>> 8) & 0xff),
(byte)((bytes ) & 0xff)
};
}

InetAddress.getByAddress(unpack(packedBytes)).getHostAddress()

Byte arrays and strings in java

It depends on your Charset.defaultCharset(). That determines how the bytes are interpreted. Probably the negative values are a non-canonical way of representing codepoints.

(see this great answer: https://stackoverflow.com/a/7934397/461499)

Re-interpreting the getBytes() to a String will then be the canonical way and will return true

    System.out.println(Charset.defaultCharset()); //UTF-8 here :)

byte arr[] = new byte[] {56, 99, 87, 77, 73, 90, 105, -23, -52, -85, -9, -55, -115, 11, -127, -127};
String s= new String(arr);
System.out.println(s);
// [56, 99, 87, 77, 73, 90, 105, -17, -65, -67, -52, -85, -17, -65, -67, -55, -115, 11, -17, -65, -67, -17, -65, -67]

byte arr2[] = new byte[] {56, 99, 87, 77, 73, 90, 105, -17, -65, -67, -52, -85, -17, -65, -67, -55, -115, 11, -17, -65, -67, -17, -65, -67};
System.out.println(Arrays.toString(s.getBytes()));
System.out.println(Arrays.equals(arr, s.getBytes())); // returns false

String s2= new String(arr2);
System.out.println(Arrays.toString(s2.getBytes()));
System.out.println(Arrays.equals(arr2, s2.getBytes())); // returns true


Related Topics



Leave a reply



Submit