I'm an android self learner.I want to convert character to its corresponding binary.For example I'm giving the character 'A' (its ASCII is 65 and binary of 65 is 1000001) I want to get answer as 1000001. Thanks in advance..
Here is the my code
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Stack;
public class MainActivity extends AppCompatActivity implements TextWatcher,View.OnClickListener
{
EditText txtDecimal;
TextView txtBinary;
Button btnAbout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDecimal=(EditText)findViewById(R.id.txtDecimal);
txtBinary=(TextView)findViewById(R.id.txtval2);
//txtdec=(TextView)findViewById(R.id.txtDecimal);
txtDecimal.addTextChangedListener(this);
btnAbout=(Button)findViewById(R.id.button1);
btnAbout.setOnClickListener(this);
}
public void beforeTextChanged(CharSequence sequence,int start,int count,int after)
{
}
public void afterTextChanged(Editable editable)
{
}
public void onTextChanged(CharSequence sequence,int start,int before,int count) {
calculate(2, txtBinary); // for base 2 (binary)
}
public void calculate(int base,TextView txtView)
{
if(txtDecimal.getText().toString().trim().length()==0)
{
txtView.setText("");
return;
}
try
{
Stack<Object> stack=new Stack<Object>();
int number=Integer.parseInt(txtDecimal.getText().toString());
while (number>0)
{
int remainder=number%base; // find remainder
if(remainder<10)
// for remainder smaller than 10
{
stack.push(remainder);
// push remainder in stack
}
number/=base;
}
StringBuffer buffer=new StringBuffer();
while (!stack.isEmpty())
{
buffer.append(stack.pop().toString());
}
txtView.setText(buffer.toString());
}
catch (Exception e)
{
txtView.setText(e.getMessage());
}
}
public void onClick(View view)
// to display Information in a dialog box
{
// create a dialog box
AlertDialog.Builder builder=new AlertDialog.Builder(this);
// to allow cancelling the dialog box
builder.setCancelable(true);
// set title
builder.setTitle("About NumberSystemConverter");
// set message
builder.setMessage("Made by HARI");
// display dialog box
builder.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
By this code i can convert only numbers.I want to convert characters too.By this code if I,m giving A it shows me that "invalid int a".I want to work both characters and numbers.plz help me...