Reputation: 2958
Is it possible to decode/encode this without losing some value using 360 int to 1 byte
Im currently finding a way to reduce 360 to fit in 255 max value of a byte.
this is what i tried:
public static int getBearing(byte data){
return Integer.parseInt( Integer.toHexString(data & 0xff) , 16) << 1;
}
public static void main(String[] args){
for(int bearing = 0 ; bearing < 360 ; bearing++){
final byte bearingByte = (byte) (bearing >> 1);
System.out.printf("Bit {%s} , Original Bearing {%d} , Decrypted Bearing {%d} \n" , get8Bit(bearingByte) , bearing , getBearing(bearingByte));
}
}
and output is :
Bit {00000000} , Original Bearing {0} , Decrypted Bearing {0}
Bit {00000000} , Original Bearing {1} , Decrypted Bearing {0} <-- this should be 1
Bit {00000001} , Original Bearing {2} , Decrypted Bearing {2}
Bit {00000001} , Original Bearing {3} , Decrypted Bearing {2} <-- this should be 3
Bit {00000010} , Original Bearing {4} , Decrypted Bearing {4}
Bit {00000010} , Original Bearing {5} , Decrypted Bearing {4} <-- this should be 5
Bit {00000011} , Original Bearing {6} , Decrypted Bearing {6}
Bit {00000011} , Original Bearing {7} , Decrypted Bearing {6} <-- this should be 7
Bit {00000100} , Original Bearing {8} , Decrypted Bearing {8}
Bit {00000100} , Original Bearing {9} , Decrypted Bearing {8} <-- this should be 8
Seems like everything in decrypted is even integers.
Is there a formula or trick so that i could not lose some value?
Upvotes: 0
Views: 48
Reputation: 719238
Is it possible to decode/encode this without losing some value using 360 int to 1 byte.
No. It is impossible.
Any encoding from a larger domain (360 values) to a smaller one (256 values) must be be lossy1.
1 - One way to prove it is by application of the "pigeonhole principle".
Upvotes: 2