Save string into byte array

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

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

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

the below code

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

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

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

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();

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

Tried it now

still gives the same error at

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

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

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

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

nijo.txt

Similar, but definitely not the same. :face_with_raised_eyebrow:

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
We’re gonna need splitTokens() in order to account for all the complex delimiters in that file:
splitTokens() / Reference / Processing.org
Along w/ ',' + WHITESPACE as the new delimiter argument: :nerd_face:

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();

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.:sweat_smile:

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

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

String data = join(nijo,"");

or

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

I have worked out a more understandable code using your concept

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]);
  }
}
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();

can i achieve this using join() command

Function join() acts upon the whole array. We can’t specify the index range for it: :pensive:
Processing.org/reference/join_.html

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

  • Java’s byte datatype got a range from -128 to 127: :coffee:
    byte / Reference / Processing.org
  • It can’t store anything higher than 127, like 128 or 255! :scream:
  • 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: :face_with_hand_over_mouth:

thank you so much.
solved my problem.