从 java 方法返回一个字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6137140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return a byte array from a java method
提问by Pranay
I have a byte array which i am passing to a function nibbleSwap
. nibbleSwap
swaps the 1st 4 bits with the 2nd 4 bits for each byte value in the array. After swapping i have to return the swapped byte values. If i just print the byte array i am able to get the correct value but when i return the swapped byte array, it does not print the correct value.
My code is like this:
我有一个字节数组,我将它传递给一个函数nibbleSwap
。nibbleSwap
为数组中的每个字节值交换第一个 4 位和第二个 4 位。交换后我必须返回交换的字节值。如果我只是打印字节数组,我可以获得正确的值,但是当我返回交换的字节数组时,它不会打印正确的值。
我的代码是这样的:
private static byte[] nibbleSwap(byte []inByte){
int []nibble0 = new int[inByte.length];
int []nibble1 = new int[inByte.length];
byte []b = new byte[inByte.length];
for(int i=0;i<inByte.length;i++)
{
nibble0[i] = (inByte[i] << 4) & 0xf0;
nibble1[i] = (inByte[i] >>> 4) & 0x0f;
b[i] =(byte) ((nibble0[i] | nibble1[i]));
/*System.out.printf(" swa%x ",b[i]); --- if i do this by changing the return to void i get the correct output.
*/
}
return b;
}
eg. valuebyte[]
contains: 91,19,38,14,47,21,11
I want the function to return an array containing 19,91,83,41,74,12,11
. Also, can i return this as a String by changing the return type as String because when i did that and printed it, i got the integer values of the swapped byte values?
例如。valuebyte[]
包含:91,19,38,14,47,21,11
我希望函数返回一个包含19,91,83,41,74,12,11
. 另外,我是否可以通过将返回类型更改为 String 来将其作为字符串返回,因为当我这样做并打印它时,我得到了交换字节值的整数值?
Please Help!!
Pranay
请帮忙!!
普拉奈
回答by NPE
The code does exactly what you say you want it to do, on your test data, providedof course that the values (91, 19, 38, 14, 47, 21, 11) are hexadecimal (0x91, 0x19, and so on).
代码完全按照您所说的去做,在您的测试数据上,当然前提是值 (91, 19, 38, 14, 47, 21, 11) 是十六进制的 (0x91, 0x19, 等等) .
I used the following code to call your function:
我使用以下代码来调用您的函数:
public static void main(String[] args) {
byte[] swapped = nibbleSwap(new byte[]{(byte)0x91, 0x19, 0x38, 0x14, 0x47, 0x21, 0x11});
for (byte b : swapped) {
System.out.printf("%x ", b);
}
System.out.println();
}
This prints out:
这打印出来:
19 91 83 41 74 12 11
To return the result as a string, you could use something along the following lines:
要将结果作为字符串返回,您可以使用以下内容:
public class NibbleSwap {
private static String nibbleSwap(byte []inByte){
String ret = "";
for(int i = 0; i < inByte.length; i++)
{
int nibble0 = (inByte[i] << 4) & 0xf0;
int nibble1 = (inByte[i] >>> 4) & 0x0f;
byte b = (byte)((nibble0 | nibble1));
ret += String.format("%x ", b);
}
return ret;
}
public static void main(String[] args) {
System.out.println(nibbleSwap(new byte[]{(byte)0x91,0x19,0x38,0x14,0x47,0x21,0x11}));
}
}
回答by Boro
I do not get your problem clearly but when it goes to its second part as to printing the result array, you do not need to modify the method you just need to do something like in the code below on the result array you get from the method.
我没有清楚地了解您的问题,但是当它进入关于打印结果数组的第二部分时,您不需要修改方法,您只需要在从该方法获得的结果数组上执行以下代码中的操作.
byte[] b = new byte[]{1,2,3,4};
System.out.println(Arrays.toString(b));
Output:
输出:
[1, 2, 3, 4]
I hope this answers (at least part of) your problem.
我希望这能回答(至少部分)您的问题。
回答by Kerem Baydo?an
If i just print the byte array i am able to get the correct value but when i return the swapped byte array, it does not print the correct value.
如果我只是打印字节数组,我可以获得正确的值,但是当我返回交换的字节数组时,它不会打印正确的值。
Your code does exactly what you want.
您的代码完全符合您的要求。
Here is my test code.
这是我的测试代码。
public class StackOverflow {
public static void main(String args[]) throws NullPointerException {
byte[] original = "Kerem".getBytes();
System.out.print("\n--------Print From Original--------------\n");
for (int i = 0; i < original.length; i++) {
System.out.printf("%x ", original[i]);
}
System.out.print("\n--------Print From Method Body-----------\n");
byte[] returnValue = nibbleSwap(original);
System.out.print("\n--------Print From Method Return Value---\n");
for (int i = 0; i < returnValue.length; i++) {
System.out.printf("%x ", returnValue[i]);
}
}
private static byte[] nibbleSwap(byte[] inByte) {
int[] nibble0 = new int[inByte.length];
int[] nibble1 = new int[inByte.length];
byte[] b = new byte[inByte.length];
for (int i = 0; i < inByte.length; i++) {
nibble0[i] = (inByte[i] << 4) & 0xf0;
nibble1[i] = (inByte[i] >>> 4) & 0x0f;
b[i] = (byte) ((nibble0[i] | nibble1[i]));
System.out.printf("%x ", b[i]);
}
return b;
}
}
Here is the output:
这是输出:
run:
--------Print From Original--------------
4b 65 72 65 6d
--------Print From Method Body-----------
b4 56 27 56 d6
--------Print From Method Return Value---
b4 56 27 56 d6
-----------------------------------------
BUILD SUCCESSFUL (total time: 0 seconds)
回答by Peter Lawrey
If all you want is a String with the nibbles swapped, I would just create the String as you go and not create byte[]s.
如果你想要的只是一个交换半字节的字符串,我会随你创建字符串,而不是创建字节 [] s。
public static String nibbleSwapAsString(byte[] inByte) {
StringBuilder sb = new StringBuilder("[");
String sep = "";
for (byte b : inByte) {
sb.append(sep).append(Integer.toHexString(b & 0xF)).append(Integer.toHexString(b >> 4 & 0xF));
sep = ", ";
}
sb.append(']');
return sb.toString();
}
public static void main(String... args) {
String text = nibbleSwapAsString(new byte[]{(byte)0x91, 0x19, 0x38, 0x14, 0x47, 0x21, 0x11});
System.out.println(text);
}
prints
印刷
[19, 91, 83, 41, 74, 12, 11]