我想在android中使用edittext转换像'a-f'
这样的字符串十六进制。我有这样的代码。
if(spinB1.getSelectedItem().equals("Desimal") && spinB2.getSelectedItem().equals("Heksa")){
h = 0;
h2 = "";
try {
//String h4 = etB1.getText().toString();
h2 = Integer.toHexString(u);
//String h3 = Integer.toString(Integer.parseInt(h4, 2),16);
etB2.setText(("" + h2));
}catch(Exception e){
e.printStackTrace();
}
}
当我运行此代码时,我使用'a-f'
之类的字符串填充编辑文本时,此代码无效,但数字为0-9
则无效。
我总是收到这个错误:
04-29 08:54:58.330: E/AndroidRuntime(1650): FATAL EXCEPTION: main
04-29 08:54:58.330: E/AndroidRuntime(1650): Process: com.uzay.convv, PID: 1650
04-29 08:54:58.330: E/AndroidRuntime(1650): java.lang.NumberFormatException: Invalid int: "f"
04-29 08:54:58.330: E/AndroidRuntime(1650): at java.lang.Integer.invalidInt(Integer.java:137)
04-29 08:54:58.330: E/AndroidRuntime(1650): at java.lang.Integer.parse(Integer.java:374)
04-29 08:54:58.330: E/AndroidRuntime(1650): at java.lang.Integer.parseInt(Integer.java:365)
04-29 08:54:58.330: E/AndroidRuntime(1650): at java.lang.Integer.parseInt(Integer.java:331)
04-29 08:54:58.330: E/AndroidRuntime(1650): at com.uzay.convv.Sixth$1.onClick(Sixth.java:60)
04-29 08:54:58.330: E/AndroidRuntime(1650): at android.view.View.performClick(View.java:4424)
04-29 08:54:58.330: E/AndroidRuntime(1650): at android.view.View$PerformClick.run(View.java:18383)
04-29 08:54:58.330: E/AndroidRuntime(1650): at android.os.Handler.handleCallback(Handler.java:733)
04-29 08:54:58.330: E/AndroidRuntime(1650): at android.os.Handler.dispatchMessage(Handler.java:95)
04-29 08:54:58.330: E/AndroidRuntime(1650): at android.os.Looper.loop(Looper.java:137)
04-29 08:54:58.330: E/AndroidRuntime(1650): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-29 08:54:58.330: E/AndroidRuntime(1650): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 08:54:58.330: E/AndroidRuntime(1650): at java.lang.reflect.Method.invoke(Method.java:515)
04-29 08:54:58.330: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-29 08:54:58.330: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-29 08:54:58.330: E/AndroidRuntime(1650): at dalvik.system.NativeStart.main(Native Method)
有没有人为我提供解决方案?
答案 0 :(得分:0)
您应该使用Integer.valueOf(string, 16)
答案 1 :(得分:0)
public static String hexadecimal(String input, String charsetName) throws UnsupportedEncodingException {
if (input == null) throw new NullPointerException();
return asHex(input.getBytes(charsetName));
}
private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
public static String asHex(byte[] buf)
{
char[] chars = new char[2 * buf.length];
for (int i = 0; i < buf.length; ++i)
{
chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
}
return new String(chars);
}
答案 2 :(得分:0)
首先获取字符串的字节数组值:
byte[] byteArrOfString = stringToBeHex.getBytes("UTF-8");
然后循环遍历此字节数组并将其项目转换为十六进制:
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < byteArrOfString.length; i++) {
String h = Integer.toHexString(0xFF & byteArrOfString[i]);
// below is a bug fix
while (h.length() < 2) {
h = "0" + h;
}
hexString.append(h);
}
使用hexString.toString()
获得结果。