# Save string into byte array

**URL:** https://discourse.processing.org/t/save-string-into-byte-array/4371
**Category:** Coding Questions
**Created:** [October 11, 2018, 5:46pm UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371 "2018-10-11T17:46:12Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 11, 2018, 5:46pm UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/1 "2018-10-11T17:46:12Z")

</div>

currently I am converting my text file into a string array and then combining them.  
once combined I an converting them into an integer array by using split()  
all the data int the text file is numbers that are a byte long(led color data 0-255)  
but since it is an integer type array it uses 4 times more space than a byte.  
I need a way in which i can store the string which is being split directly into a byte type array

```auto
int[] ledarray;

void setup() 
{
  String[] nijo = loadStrings("nijo.txt");
  String data = join(nijo,"");
  ledarray = (int(splitTokens(data,", ")));

   for (int i = 0; i<ledarray.length; i++) 
     {
       println(ledarray[i]);
    }
}

```

**Thank you**

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 11, 2018, 7:04pm UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/2 "2018-10-11T19:04:47Z")

</div>

```java
final String[] strVals = { "17", "127", "128", "255" };
final byte[] byteVals = byte(int(strVals));
println(byteVals);
exit();

```

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 12, 2018, 4:33am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/3 "2018-10-12T04:33:19Z")

</div>

the below code

```auto
final byte[] byteVals = byte(int(strVals));

```

gives me an error  
“function parseByte() expects parameter like parseByte(boolean)”

the string values int the text file looks like this  
 ![Capture](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2821c2205fca5c13be5e33b3cbe91c79d412af4a.jpeg)

so I am using loadString and then joing them to merge multiple lines

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 12, 2018, 6:46am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/4 "2018-10-12T06:46:32Z")

</div>

That “nijo.txt” file got some very unusual content to parse. 🤔  
Maybe this approach may handle that: 😳

```java
final String[] nijo = {
  "0,1,2,3,4,", 
  " 5,6,7,8"
};

final StringBuilder sb = new StringBuilder();
for (final String line : nijo) sb.append(line.trim());

final String[] strVals = split(sb.toString(), ',');
final byte[] byteVals = byte(int(strVals));

println(byteVals);
exit();

```

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 12, 2018, 5:36pm UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/5 "2018-10-12T17:36:27Z")

</div>

i have not tested the code yet.  
but can you explain what each line of coed does.  
sorry. I am a noob:(

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 12, 2018, 5:40pm UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/6 "2018-10-12T17:40:50Z")

</div>

Tried it now

still gives the same error at

```auto
final byte[] byteVals = byte(int(strVals));

```

“function parseByte() expects parameter like parseByte(boolean)”

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 12, 2018, 8:53pm UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/7 "2018-10-12T20:53:11Z")

</div>

Can you post the content of your "nijo.txt” file here? 🗃

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 13, 2018, 4:32am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/8 "2018-10-13T04:32:59Z")

</div>

Its similar to the picture i posted. But has a lot of data.  
I will upload it to drive and post a link.  
is there a way I can upload it here

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 13, 2018, 4:35am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/9 "2018-10-13T04:35:29Z")

</div>

[nijo.txt](https://drive.google.com/open?id=1VcM9BnIzGLHWze2dGnLWYaTtnfPi6SV_)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 13, 2018, 7:19am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/10 "2018-10-13T07:19:15Z")

</div>

> [@jacobrejin](#):
>
> It’s similar to the picture I posted.

Similar, but definitely not the same. 🤨

Your previous posted sample had a single comma `','` as the delimiter for all the values.  
In the actual “nijo.txt” file, besides the single comma `','`, the comma + space `", "` is the most used delimiter there.

Now, just a vanilla **split()** isn’t enough: [split() / Reference / Processing.org](http://Processing.org/reference/split_.html)  
We’re gonna need **splitTokens()** in order to account for all the complex delimiters in that file:  
[splitTokens() / Reference / Processing.org](http://Processing.org/reference/splitTokens_.html)  
Along w/ `',' + WHITESPACE` as the new delimiter argument: 🤓

```java
final String[] nijo = loadStrings("nijo.txt");

final StringBuilder sb = new StringBuilder();
for (final String line : nijo) sb.append(line);

final String[] strVals = splitTokens(sb.toString(), ',' + WHITESPACE);
final byte[] byteVals = byte(int(strVals));

println(byteVals);
exit();

```

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 14, 2018, 4:44am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/12 "2018-10-14T04:44:47Z")

</div>

the line still has the same error but due to some reason can run and gives me the desired output unlike previous codes u suggested .

The initial coed that i posted had the **splitToken()** applied to it with a comma and a space.😅

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 14, 2018, 4:58am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/13 "2018-10-14T04:58:58Z")

</div>

> [@jacobrejin](#):
>
> … unlike previous codes u suggested.

I can only provide code based on info you had already posted! 🙄  
I had no idea about the content of “nijo.txt” for a long time. ⏲  
And when you’ve finally posted a sample of it, it had a misleading delimiter! 🤪

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 14, 2018, 5:02am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/14 "2018-10-14T05:02:27Z")

</div>

Thank you sooooo much.  
Just one more help.  
is there way to only join only specific no of lines from nijo.txt file by using this

```auto
String data = join(nijo,"");

```

or

```auto
for (final String line : nijo) sb.append(line);

```

I have worked out a more understandable code using your concept

```auto
byte[] leddata;

void setup() 
{
  String[] nijo = loadStrings("nijo.txt");// Load text file as a String
  
  String data = join(nijo,""); //join multiple lines of nijo string array
  String[] strVals = splitTokens(data, ", ");//split using , & space and 
                                             // values as string
  
  leddata = byte(int(strVals));//convert and store string into bye array
  
   for (int i = 0; i<leddata.length; i++) 
  {
   println(leddata[i]);
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 14, 2018, 5:10am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/15 "2018-10-14T05:10:52Z")

</div>

> [@jacobrejin](#):
>
> Is there way to only join only specific no of lines from “nijo.txt” file…

```auto
final String[] nijo = loadStrings("nijo.txt");

final StringBuilder sb = new StringBuilder();
//for (final String line : nijo) sb.append(line);

final int startLine = 100, endLine = 103;
for (int i = startLine; i <= endLine; sb.append(nijo[i++]));

final String[] strVals = splitTokens(sb.toString(), ',' + WHITESPACE);
final byte[] byteVals = byte(int(strVals));

println(byteVals);
exit();

```

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 14, 2018, 5:29am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/16 "2018-10-14T05:29:01Z")

</div>

can i achieve this using join() command

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 14, 2018, 5:36am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/17 "2018-10-14T05:36:11Z")

</div>

Function **join()** acts upon the whole array. We can’t specify the index range for it: 😔  
[Processing.org/reference/join\_.html](http://Processing.org/reference/join_.html)

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 14, 2018, 6:25am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/18 "2018-10-14T06:25:13Z")

</div>

there is another problem which i missed.

the numbers are not getting saved properly.

eg  
255 gets converted to -1  
128 to -128 and so on

if i save it in int type array the problem is not there what can be the reason

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 14, 2018, 1:03pm UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/19 "2018-10-14T13:03:33Z")

</div>

- Java’s `byte` datatype got a range from `-128` to `127`: ☕  
[byte / Reference / Processing.org](http://Processing.org/reference/byte.html)
- It can’t store anything higher than `127`, like `128` or `255`! 😱
- However, don’t need to fret so much; b/c when we save or send those negative values as `byte`, they’re properly interpreted back to the original positive values: 🤭

> [@Udp problem sending byte (-127)](https://discourse.processing.org/t/udp-problem-sending-byte-127/3545):
>
> Hello, I am trying to send visca commands over udp. To send a command to camera 1 you have to send byte (0x81). Unfortunately udp.send sends the value 63! I discovered this by sending from processing 3 on windows and I am receiving with processing on a raspberry pi with processing 3 byte[] mist = {byte(80),byte(0x81), byte(0x82), byte(0x83), byte(0x84), byte(0x85), byte(0xFF)}; String massage=new String(mist); udp.send(massage, ip, port); results on the receiver side to -128, 63, -126,-…

---

<div class="post-metadata">

### Author: ![jacobrejin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jacobrejin](https://discourse.processing.org/u/jacobrejin)
#### Post date: [October 20, 2018, 9:48am UTC](https://discourse.processing.org/t/save-string-into-byte-array/4371/20 "2018-10-20T09:48:11Z")

</div>

thank you so much.  
solved my problem.
