Skip to content

Instantly share code, notes, and snippets.

@maheshgiri
Created September 4, 2017 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maheshgiri/86740297a62b58168cb7572273c97032 to your computer and use it in GitHub Desktop.
Save maheshgiri/86740297a62b58168cb7572273c97032 to your computer and use it in GitHub Desktop.
Convert Hex to Decimal by IEEE Standards
import android.util.Log;
import com.android.sustlabs.pojo.BinaryObj;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
/**
* Created by maheshgiri on 02/09/17.
*/
public class Utils {
private static HashMap<String, BinaryObj> stringBinaryObjHashMap = new LinkedHashMap<>();
private static List<Integer> resultBinaryList = new ArrayList<>();
public static void initBinaryMapping() {
stringBinaryObjHashMap.put("0", new BinaryObj(0, 0, 0, 0));
stringBinaryObjHashMap.put("1", new BinaryObj(0, 0, 0, 1));
stringBinaryObjHashMap.put("2", new BinaryObj(0, 0, 1, 0));
stringBinaryObjHashMap.put("3", new BinaryObj(0, 0, 1, 1));
stringBinaryObjHashMap.put("4", new BinaryObj(0, 1, 0, 0));
stringBinaryObjHashMap.put("5", new BinaryObj(0, 1, 0, 1));
stringBinaryObjHashMap.put("6", new BinaryObj(0, 1, 1, 0));
stringBinaryObjHashMap.put("7", new BinaryObj(0, 1, 1, 1));
stringBinaryObjHashMap.put("8", new BinaryObj(1, 0, 0, 0));
stringBinaryObjHashMap.put("9", new BinaryObj(1, 0, 0, 1));
stringBinaryObjHashMap.put("A", new BinaryObj(1, 0, 1, 0));
stringBinaryObjHashMap.put("B", new BinaryObj(1, 0, 1, 1));
stringBinaryObjHashMap.put("C", new BinaryObj(1, 1, 0, 0));
stringBinaryObjHashMap.put("D", new BinaryObj(1, 1, 0, 1));
stringBinaryObjHashMap.put("E", new BinaryObj(1, 1, 1, 0));
stringBinaryObjHashMap.put("F", new BinaryObj(1, 1, 1, 1));
}
public static double convert(String hexvalue) throws Exception{
resultBinaryList=new ArrayList<>();
for (int i = 0; i < hexvalue.length(); i++) {
Character hexval=hexvalue.charAt(i);
if(hexval.compareTo('4')>0){
//Log.d("UTILS", "convert: ");
}
for (String keyvalue : stringBinaryObjHashMap.keySet()) {
if (hexval.compareTo(keyvalue.charAt(0))==0) {
Log.d("UTILS", "convert: ");
resultBinaryList.add(stringBinaryObjHashMap.get(keyvalue).getB1());
resultBinaryList.add(stringBinaryObjHashMap.get(keyvalue).getB2());
resultBinaryList.add(stringBinaryObjHashMap.get(keyvalue).getB3());
resultBinaryList.add(stringBinaryObjHashMap.get(keyvalue).getB4());
}
}
}
double exponent =0;
double dec_bin=0;
for (int i = 1; i < 9; i++) {
dec_bin= (resultBinaryList.get(i)*(Math.pow(2,8-i)));
//console.log(i, dec_bin);
exponent=exponent+dec_bin;
}
exponent=exponent-127;
double mantissa=0;
for (int i = 9; i < 32; i++) {
dec_bin= (resultBinaryList.get(i)*(Math.pow(2,8-i)));
//console.log(i, dec_bin);
mantissa=mantissa+dec_bin;
}
double finalvalue= ((1+mantissa)*Math.pow(2,exponent));
if (resultBinaryList.get(0)==1){
finalvalue=-1*finalvalue;
}
return finalvalue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment