简体   繁体   中英

Converting decimal to binary and then binary to unknown format?

I am using an API which gives me result in 32 bit(4 byte) data.I need to decode this value to another format to use it.

An example of data could be:

 645 --> (00000000 00000000 00000010 10000101) --> (0, 0, 2, 133) 

I will receive 645 from API which I need to first convert to binary format and then to an unknown format which I am not able to understand.I am able to convert "645" to binary format using the below code :

   public static void toBinary(String value){
       int i = Integer.parseInt(value);
       System.out.println(Integer.toBinaryString(i));
   }

output : 100000000000000000

But how do I get it to (0,0,2,133) to use it in my application.Could anyone please help me telling which format is this?

尝试这个

String s = "(" + (n >> 24 & 0xFF) + ", " + (n >> 16 & 0xFF)+ ", " + (n >> 8 & 0xFF)+ ", " + (n & 0xFF) + ")";

First of all, 645 in base 2 is 1010000101. (Your code gives an incorrect result for some reason).

Once you realize that, I think it's fairly simple to see that the "unknown format" can be reached by simply breaking the binary value into chunks of 8 bits (adding 0's at the beginning where necessary) to give your intermediate binary format, and then converting each chunk (byte) into decimal (base 10).

00000000 in base 10 is 0. 00000010 in base 10 is 2. 10000101 in base 10 is 133.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM