Monday, July 28, 2014

Convert String to int to String base on various radix

This example show how to 
  • Convert String (user enter in EditText) to integer base on radix, using parseInt(String string) and parseInt(String string, int radix).
  • Convert from integer back to String base on various radix using  toString(int i), toString(int i, int radix), toBinaryString(int i), toOctalString(int i) and toHexString(int i).


package com.example.androidstringformat;

import java.util.Locale;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
 
 RadioButton radioDefault, radioBinary, radioOctal, radio10, radioHex, radio32;
 EditText edit1;
 TextView textOut;
 Button buttonPrint;
 
 Spinner spAvailableLocale;
 Locale[] availableLocales;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        radioDefault = (RadioButton)findViewById(R.id.optDefault);
        radioBinary = (RadioButton)findViewById(R.id.optBinary);
        radioOctal = (RadioButton)findViewById(R.id.optOctal);
        radio10 = (RadioButton)findViewById(R.id.opt10);
        radioHex = (RadioButton)findViewById(R.id.optHex);
        radio32 = (RadioButton)findViewById(R.id.opt32);
        
        edit1 = (EditText)findViewById(R.id.edit1);
     textOut = (TextView)findViewById(R.id.printout);
     buttonPrint = (Button)findViewById(R.id.buttonprint);
     
     buttonPrint.setOnClickListener(onClickListener);

    }
    
    OnClickListener onClickListener =
     new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    String strIn = edit1.getText().toString();

    if(!strIn.equals("")){

     int intIn = 0;
     
     try{
      if(radioDefault.isChecked()){
       intIn = Integer.parseInt(strIn);
      }else if(radioBinary.isChecked()){
       intIn = Integer.parseInt(strIn, 2);
      }else if(radioOctal.isChecked()){
       intIn = Integer.parseInt(strIn, 8);
      }else if(radio10.isChecked()){
       intIn = Integer.parseInt(strIn, 10);
      }else if(radioHex.isChecked()){
       intIn = Integer.parseInt(strIn, 16);
      }else if(radio32.isChecked()){
       intIn = Integer.parseInt(strIn, 32);
      }
      
      String result 
       = "default: " + Integer.toString(intIn) + "\n"
       + "Binary: " + Integer.toBinaryString(intIn) + "\n"
       + "Binary: " + Integer.toOctalString(intIn) + "\n"
       + "Radix 10: " + Integer.toString(intIn, 10) + "\n"
       + "Hex: " + Integer.toHexString(intIn) + "\n"
       + "Radix 32: " + Integer.toString(intIn, 32);
      
      textOut.setText(result);
     }catch(NumberFormatException ex){
      textOut.setText(ex.toString());
     }
     
    }
    
   }};

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidstringformat.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
        
 <RadioGroup
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" >
     <RadioButton
         android:id="@+id/optDefault"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="default" 
         android:checked="true" />
     <RadioButton
         android:id="@+id/optBinary"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Binary" />
     <RadioButton
         android:id="@+id/optOctal"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Octal" />
     <RadioButton
         android:id="@+id/opt10"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Radix 10" />
     <RadioButton
         android:id="@+id/optHex"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Hex" />
     <RadioButton
         android:id="@+id/opt32"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="Radix 32" />
 </RadioGroup>

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/buttonprint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" - Print - " />
    <TextView
        android:id="@+id/printout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

</LinearLayout>


No comments:

Post a Comment