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
so I am using loadString and then joing them to merge multiple lines
That “nijo.txt” file got some very unusual content to parse.
Maybe this approach may handle that:
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?
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
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
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:
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.
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:
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
thank you so much.
solved my problem.