3 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
嘗試這個(gè):
String message = "[72,69,76,76,79]";
String[] stringArray = message.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "").split(",");
byte[] byteArray = new byte[stringArray.length];
for(int i = 0; i<stringArray.length; i++)
{
byteArray[i] = (byte) Integer.parseInt(stringArray[i]);
}
首先,轉(zhuǎn)換為字符串?dāng)?shù)組。然后,您在該數(shù)組中進(jìn)行迭代,并將每個(gè)元素轉(zhuǎn)換為 int,然后轉(zhuǎn)換為 byte 并將其分配給字節(jié)數(shù)組。

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊
當(dāng)您獲取請(qǐng)求正文時(shí),Map<String, Object>獲取字節(jié)數(shù)組的唯一方法是解析“stringyfied”字節(jié)數(shù)組并構(gòu)建結(jié)果字節(jié)數(shù)組。
就像是:
String byteArrayAsStr = request.get("message").toString();
// First get only the "string-numeric" values (storing them into a String[]
String[] byteValues = byteArrayAsStr.substring(1, byteArrayAsStr.length() - 1).split(",");
byte[] messageBytes = new byte[byteValues.length];
// then we store each parsed byte value into our result byte[] (messageBytes)
for (int i=0, len=messageBytes.length; i<len; i++) {
messageBytes[i] = Byte.parseByte(byteValues[i].trim()); // trim not necessary in your case, as you send your array without spaces after commas
}
[...]
如果您找不到更好的方法來(lái)直接以所需類(lèi)型獲取請(qǐng)求正文(使用適當(dāng)?shù)?DTO),則可以采用這種方法。

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊
你可以byte[]
這樣制作String
:
byte[] byteArray = bytes.getBytes();
添加回答
舉報(bào)