Can you help me optimise this code?

I need help optimising this code:

String ROMNAME = str(char(bytes[308]))+str(char(bytes[309]))+str(char(bytes[310]))+str(char(bytes[311]))+str(char(bytes[312]))+str(char(bytes[313]))+str(char(bytes[314]))+str(char(bytes[315]))+str(char(bytes[316]))+str(char(bytes[317]))+str(char(bytes[318]))+str(char(bytes[319]))+str(char(bytes[320]))+str(char(bytes[321]))+str(char(bytes[322]))+str(char(bytes[323]));

Is bytes[value] an array you created before?
Is it for Processing or Arduino?
To transform a byte array into a String you can use

  String s = new String(bytes);



String ROMNAME = "";


for (int i=308; i<=323; i++) {
  ROMNAME+= str(char(bytes[i]));
  print(i+",");
}
1 Like

Hello,

Is this an academic assignment? Homework?

This is straightforward and worth working through.

Add strings in a for loop()

https://processing.org/reference/addition.html
https://processing.org/reference/for.html

To test:
Fill the array with characters of the alphabet.
The final string should be “abcde…”

Try and do it without looking at other completed code.

Filling array and showing output:

:slight_smile:

I am confused with the outcome of your code.without knowing what the values are in the array. So I make a byte array with random values within the ascii capital letter range, to then transform to a String.
Maybe you can elaborate on the meaning of your code

 void setup() {
  // making random byte array
  byte[] bytes = new byte[500];
  for (int i = 0; i < 500; i++) {
    float v = random(65, 90); //-128, 127); To make it readable 
    bytes[i] = byte(v);
  }
  
  byte[] bytes_cut = new byte[16];
  arrayCopy(bytes, 308, bytes_cut, 0, 16);
  String s = new String(bytes_cut);
  print(s);
}

Edit How stupid I now see that you are only printing the int, sorry.