以下内容将字符串转换为字节数组:
byte[] sent_byte = new byte[cmd_str.length];
for (int i = 0; i < sent_byte.length; i++) {
sent_byte[i] = (byte) Integer.parseInt(cmd_str[i], 16);
}
如何将其转换回原始字符串?
答案 0 :(得分:2)
您必须创建一个新的String对象并将字节数组分配给它:
String stringBytes = new String(sent_byte);
答案 1 :(得分:1)
你也可以使用:
int isCentered5(int a[ ], int len) {
int sum=0;
for(int i=0;i<len;i++){
sum+=a[i];
}
//check if sum less than 15
if(sum<15){
printf("Sum of all elements are less than 15");
}
//check if even terms- we need odd terms to find middle sum
elseif(len%2==0){
printf("There should be odd lenght of array to find middle sum");
}
else{
//check if middle sum of array is 15?
//can you help me here,how can i check is middle term sum is 15 or not
for(int i=0;i<len;i++){
}
}
}
int isCentered5(int a[ ], int len);//function declaration
int main() {
}
答案 2 :(得分:0)
**Osdata**
C:\Program Files (x86)
Z:\Program Files\Google\Update <br>
W:\Program Files (x86)\Internet Explorer\es-ES <br>
C:\Program Files (x86) <br>
q:\Program Files\Google\Update <br>
W:\Program Files (x86)\Internet Explorer\es-ES <br>
C:\Program Files (x86) <br>
Z:\Program Files\Google\Update <br>
P:\Program Files (x86)\Internet Explorer\es-ES <br>
C:\Program Files (x86) <br>
Z:\Program Files\Google\Update <br>
X:\Program Files (x86)\Internet Explorer\es-ES <br>
C:\Program Files (x86) <br>
F:\Program Files\Google\Update <br>
W:\Program Files (x86)\Internet Explorer\es-ES <br>
**Files** <br>
D:\MyFolder\Work <br>
D:\PDF\Year\2016 <br>
E:\Games\Report\Tri <br>
E:\all\Reports\Like\12\P <br>
D:\MyFolder\Work\32\WERT <br>
D:\PDF\Year\2016\RET\12\ER <br>
E:\Games\Report\Tri <br>
E:\all\Reports\Like <br>
D:\MyFolder\Work <br>
D:\PDF\Year\2016 <br>
E:\Games\Report\Tri <br>
E:\all\Reports\Like <br>
D:\MyFolder\Work <br>
D:\PDF\Year\2016 <br>
E:\Games\Report\Tri <br>
E:\all\Reports\Like <br>
**OthersData** <br>
\\Games\AllGames\Data <br>
\\Report\New\AllNew\Report <br>
\\Re12\FERT\Like <br>
答案 3 :(得分:0)
您的问题的答案:标准的java工具无法实现。
说明:来自此代码
Integer.parseInt(cmd_str[i], 16)
我们知道cmd_str []是 String 的数组,因为
static int parseInt(String s)
static int parseInt(String s, int radix)
是一种将String(非字节或字节数组)解析为 signed 整数的方法。
以及此代码中发生的事情
(byte) Integer.parseInt...
这个32位有符号整数是否被转换为8位有符号整数。这意味着如果你试图
String bits8string = new String(sent_byte, "AnyStandardCharset");
你会得到垃圾(或没有),因为字符不是有符号整数。因此,从int到char的转换不是一些charset(如ascii)表,因为将字符映射到负值是没有意义的。(我从未见过任何这样的表。
此处代码汇集了您的代码作为证据:
import java.io.UnsupportedEncodingException;
public class stringbyte16{
public static void main(String cmd_str[]) throws UnsupportedEncodingException{
byte[] sent_byte = new byte[cmd_str.length];
String bits8string="";
for (int i = 0; i < sent_byte.length; i++) {
sent_byte[i] = (byte) Integer.parseInt(cmd_str[i], 16);
System.out.println(sent_byte[i]);
}
bits8string+=new String(sent_byte, "UTF-16");//or "US-ASCII", nothing, etc
System.out.println(bits8string);
}
}
只需使用以空格分隔的十六进制数字(按照预期的方式)运行
解决这个问题的唯一方法就是这样:
import java.io.UnsupportedEncodingException;
public class stringbyte16{
public static void main(String cmd_str[]) throws UnsupportedEncodingException{
Character HexChar [] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
byte[] sent_byte = new byte[cmd_str.length];
String bits8string="";
for (int i = 0; i < sent_byte.length; i++) {
sent_byte[i] = (byte) Integer.parseInt(cmd_str[i], 16);
System.out.println(sent_byte[i]);
bits8string+=HexChar[(int)sent_byte[i]];
}
System.out.println(bits8string);
}
}
答案 4 :(得分:-1)
试试这个:
public static void main(String args[]) throws UnsupportedEncodingException {
String str = "0123456abcdef中文123abc";
byte[] data = str.getBytes("UTF-8");
String hex = encode(data, 0, data.length);
System.out.println(hex);
data = decode(hex);
str = new String(data, "UTF-8");
System.out.println(str);
hex += "a";
data = decode(hex);
str = new String(data, "UTF-8");
System.out.println(str);
}