# Can you help me optimise this code?

**URL:** <https://discourse.processing.org/t/can-you-help-me-optimise-this-code/16251>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [December 8, 2019, 3:13pm UTC](https://discourse.processing.org/t/can-you-help-me-optimise-this-code/16251 "2019-12-08T15:13:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yes](https://avatars.discourse-cdn.com/v4/letter/y/ea5d25/32.png) [@yes](https://discourse.processing.org/u/yes)\
**Post date:** [December 8, 2019, 3:13pm UTC](https://discourse.processing.org/t/can-you-help-me-optimise-this-code/16251/1 "2019-12-08T15:13:53Z")

</div>

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]));`

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 8, 2019, 3:40pm UTC](https://discourse.processing.org/t/can-you-help-me-optimise-this-code/16251/2 "2019-12-08T15:40:21Z")

</div>

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

```auto
  String s = new String(bytes);

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 8, 2019, 4:05pm UTC](https://discourse.processing.org/t/can-you-help-me-optimise-this-code/16251/3 "2019-12-08T16:05:45Z")

</div>

```auto

String ROMNAME = "";

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

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 8, 2019, 4:24pm UTC](https://discourse.processing.org/t/can-you-help-me-optimise-this-code/16251/4 "2019-12-08T16:24:18Z")

</div>

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/addition.html)  
[https://processing.org/reference/for.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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/dd0ac05e5552e005fd9954148d758fdfd747fbc8.png)

🙂

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 8, 2019, 7:01pm UTC](https://discourse.processing.org/t/can-you-help-me-optimise-this-code/16251/5 "2019-12-08T19:01:49Z")

</div>

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

```auto
 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.
