0

文字列全体を整数に変換するときに問題が発生します。エディットテキスト「こんにちは」と入力し、ボタンを押してから他のエディットテキストに書き込むと、72 101 108108111と表示されます。私を助けてください...

私の.xmlファイル

<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>
4

2 に答える 2

1

入力した文字列から各文字をintに変換する必要があります。以下のサンプルコードが役立つ場合があります。

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);
于 2013-02-16T13:21:04.000 に答える
1

文字をintにキャストするだけで、ASCII値が得られます。

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());
于 2013-02-16T13:21:18.957 に答える