0

I am not familiar with Java syntax, that question is very stupid.

My goal in C is very simple:

char a[3] = {1, 12, 115};

for{int i = 0; i< 3; i++}
    printf("%1d", (unsigned int)a[i]);

or

char str[256];

memset(&str[0], 0, 256);


for(int i = 0; i<3; i++){
   int ii;
   ii = strlen(&str[0]);
   sprintf(&str[ii], "%1u", (unsigned int)a[i]);       
}/*for*/

printf("%s", &str[0]);

The print result be 112115

if I would like to use hexadecimal

it is just replace

  sprintf(&str[0], "%1u", (unsigned int)a[i]);

as

  sprintf(&str[0], "%#1x", (unsigned int)a[i]);

In java, I met a trouble for the same purpose.

There is a byte[] , I would like to convert it as number array, but I did not get a explicit way to achieve the goal .

I found a way there is :

String str = new String("");
byte[] a = {1, 12, 115};
for(int i = 0; i< 3; i++){
    str  += Integer.toString(a[i].intValue());
}

That looks like clumsy.

Could anyone give me suggestion ? thank you.

Gaiger Chen
  • 301
  • 3
  • 18
  • you possibly like to check this out : http://stackoverflow.com/questions/14671594/java-print-four-byte-hexadecimal-number – nafas Jun 18 '15 at 14:38
  • Excuse me, what is "a[] Array contains char's " means ? I am feeble in JAVA... – Gaiger Chen Jun 18 '15 at 14:42
  • 3
    In that case, use `StringBuffer` for `str` to avoid creating too many strings. `intValue()` is of no use here because bytes are already numbers (they are integers on 8-bits). So `str+=Byte.toString(a[i]);` works well. And all of this is not more weird than using `sprintf` and its strange syntax for formats... – Jean-Baptiste Yunès Jun 18 '15 at 14:44

2 Answers2

2

You can convert your byte[] array to a String like this:

Complete ReEdit:

public static int[] convertToIntArray(byte[] input)
{
    int[] ret = new int[input.length];
    for (int i = 0; i < input.length; i++)
    {
        ret[i] = input[i];
    }
    return ret;
}

The above code is from: how to convert the byte array into an integer array.

You can then convert the integer array into a string array with:

int[] nums = {5,1,2,11,3}; //List or Vector
Arrays.sort(nums); //Collections.sort() for List,Vector
String a=Arrays.toString(nums); //toString the List or Vector
String ar[]=a.substring(1,a.length()-1).split(", ");
System.out.println(Arrays.toString(ar));

The above code is from: Converting an int array to a String array

Community
  • 1
  • 1
1

In java 8 nice too.

String str = IntStream.of(1, 12, 115)
    .map((b) -> String.format("%02x", b)
    .collect(Collectors.joining(", "));

Though: no ByteStream.

Older java:

String convert(byte[] a) {
    StringWriter sw = new StringWriter();
    PrintWriter wr = new PrintWriter(sw, true);
    for (int i = 0; i < a.length; i++) {
        wr.printf("%02x, a[i]);
    }
    return sw.toString();
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • why all the complicated code when you can do `String s = new String(byteArray)`? –  Jun 18 '15 at 16:10
  • @TheWrenchintheSystem his last example concatenated the hexadecimal representations of the bytes as a string, something like ""1c63" – Joop Eggen Jun 18 '15 at 17:35
  • so when you passed the byte array in, you got 1c63? –  Jun 18 '15 at 17:39
  • oh yeah look at that....I read it on a website a while back and never tested it......I'll edit or delete –  Jun 18 '15 at 17:43