Looking Forward
Looking Forward

Reputation: 3585

Convert whole String in integer value in android

I have a problem while converting the whole string in integer. When I write in edittext "Hello" and when I press button then in other edittext , it should show me 72 101 108 108 111 . Please help me...

My .xml file

<RelativeLayout 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"
    tools:context=".MainActivity" >



    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"

        android:layout_marginRight="22dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="@string/Message" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"

        android:text="@string/Decimal" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="30dp"

        android:ems="10" 

        android:hint="@string/Decimal"/>

</RelativeLayout>

Upvotes: 0

Views: 259

Answers (2)

Gjordis
Gjordis

Reputation: 2550

Well just cast characters to int and you get the ascii value.

StringBuilder stringToAppendInts = new StringBuilder();
StringBuilder stringToAppendHexs = new StringBuilder();

for(char item : myString.toCharArray()){
   stringToAppendHexs.append(Integer.toHexString((int)item) +" ");
   stringToAppendInts.append( (int)item+" ");
}

edittext.setText(stringToAppendInts.toString());
edittext.setText(stringToAppendHexs.toString());

Upvotes: 1

wns349
wns349

Reputation: 1306

You will have to convert each character to int from entered string. Sample code below might help you:

String str = "Hello";
String strInt = "";
for(int i=0;i<str.length();i++){
  char c = str.charAt(i);
  strInt += (int)c + " ";
}
System.out.println(strInt);

Upvotes: 1

Related Questions