如何让 EditText 默认为十六进制 Android

How to make EditText to be hex by default Android

我想通过 COM 端口传输十六进制数据。我有 Android 带有 EditText 的应用程序,我在其中键入格式如下的字符串:

AB 54 09 08 D0 E0 E0 E0 E0

(可以是w/o个空格)

我需要它已经是十六进制格式。到目前为止我必须做的:

byte[] dataToSend = new byte[] {(byte)0xAB, (byte)0x54, (byte)0x09, (byte)0x08, (byte)0xD0, (byte)0xE0, (byte)0xE0, (byte)0xE0, (byte)0xE0};

(我必须手动输入atm)

有什么方法可以默认将字符串表示为十六进制?

您可以将从EditText 接收到的字符串传递给以下函数。它将 return HEX 格式的字符串。

public String toHex(String arg) 
{
    return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}

你可以使用这个:

import org.apache.commons.codec.binary.Hex;
...
...
String hexString = Hex.encodeHexString(myString.getBytes(/* charset */));

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html