在android中转换整数值的整数值

时间:2013-02-16 13:16:05

标签: android string format decimal

将整个字符串转换为整数时遇到问题。 当我在edittext中写“Hello”时,当我按下按钮然后在其他编辑文本中,它应该显示给我 72 101 108 108 111。 请帮帮我......

我的.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>

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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());