0

I try to reverse my binary string.Any solution?

  private OnClickListener btnConvListener = new OnClickListener() {

    public void onClick(View v) {

        try{

        String ag=edittext1.getText().toString();

        HexToBinary(ag);

        } catch (Exception e) {

            Toast.makeText(getBaseContext(), "Not insert data!",Toast.LENGTH_SHORT).show();

            }
    }

};

  void HexToBinary(String Hex) {

    int i = Integer.parseInt(Hex, 16);
    String Bin = Integer.toBinaryString(i);//Converts int to binary
    text1.setText(Bin);

        //Bit reversal method....

        int reversedNum = Integer.reverse(i);

        text2.setText(reversedNum);

       }

This function converts string Hex to string Binary...but i want an extra output to opposite LSB->MSB... I test it but i have not output....i have exception from try/catch...error not input data...why? Shows only the original binary...not the reverse...

user2342687
  • 227
  • 1
  • 6
  • 17

1 Answers1

0

You can use

Integer.reverse(int i);

before you convert to a string. See the api documentation http://developer.android.com/reference/java/lang/Integer.html#reverse%28int%29

HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
  • I update my code....i test it but i have not output....i have exception from try/catch...error not input data...why? – user2342687 May 22 '13 at 16:18
  • I guess you need to do `text2.setText(Integer.toBinaryString(reversedNum));` instead of `text2.setText(reversedNum);` assuming text2 is something like a text field. – HexAndBugs May 22 '13 at 16:26
  • No known package when getting value for resource number 0x08000000 – user2342687 May 22 '13 at 16:31
  • Thanks.Work...but when input hex 85...output 10000101 reverse 101000010000000000000000000000 – user2342687 May 22 '13 at 16:40
  • If you want to remove the trailing zeros, you could try calling `replaceAll("0*$", "")` on the resulting string - e.g. `text2.setText((Integer.toBinaryString(reversedNum)).replaceAll("0*$", ""));` – HexAndBugs May 22 '13 at 22:01