Pedro Neves wrote:Hello, i'm tryin to get a string from a user or some kind of input like this: 102 005 000 016 000 and subdive it for a byte Array. Like 102 in first byte, 005 in the second and so on. How can i do this?
Thanks in advance
"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Riaan Nel wrote:
Pedro Neves wrote:Hello, i'm tryin to get a string from a user or some kind of input like this: 102 005 000 016 000 and subdive it for a byte Array. Like 102 in first byte, 005 in the second and so on. How can i do this?
Thanks in advance
Use the split() method on the String class to split your String on spaces. Loop through the resulting array and use Byte.valueOf() to get byte values.
Riaan Nel wrote:
Pedro Neves wrote:Hello, i'm tryin to get a string from a user or some kind of input like this: 102 005 000 016 000 and subdive it for a byte Array. Like 102 in first byte, 005 in the second and so on. How can i do this?
Thanks in advance
Use the split() method on the String class to split your String on spaces. Loop through the resulting array and use Byte.valueOf() to get byte values.
Jeff Verdegan wrote:
Riaan Nel wrote:
Pedro Neves wrote:Hello, i'm tryin to get a string from a user or some kind of input like this: 102 005 000 016 000 and subdive it for a byte Array. Like 102 in first byte, 005 in the second and so on. How can i do this?
Thanks in advance
Use the split() method on the String class to split your String on spaces. Loop through the resulting array and use Byte.valueOf() to get byte values.
Remember, however, that byte's range is -128..127, so either all your input will have to be in that range, or you'll have to use Integer and then cast the result to a byte, losing some precision.
Pedro Neves wrote:
Riaan Nel wrote:
Pedro Neves wrote:Hello, i'm tryin to get a string from a user or some kind of input like this: 102 005 000 016 000 and subdive it for a byte Array. Like 102 in first byte, 005 in the second and so on. How can i do this?
Thanks in advance
Use the split() method on the String class to split your String on spaces. Loop through the resulting array and use Byte.valueOf() to get byte values.
Something like this?
byte [] Array = new byte[4096];
String ss= new String();
String Str= new String();
try{
do
{
sc.next(ss);
ss.split(Str);
for(int i=0 ; i < Str.length() ; i++)
Array[i] = Byte.valueOf(Str, i);
Socket skt = new Socket(s, 12345);
DataOutputStream out = new DataOutputStream(skt.getOutputStream());
out.write(Array);
out.flush();
}while(flag2);
"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Riaan Nel wrote:
Pedro Neves wrote:
Riaan Nel wrote:
Pedro Neves wrote:Hello, i'm tryin to get a string from a user or some kind of input like this: 102 005 000 016 000 and subdive it for a byte Array. Like 102 in first byte, 005 in the second and so on. How can i do this?
Thanks in advance
Use the split() method on the String class to split your String on spaces. Loop through the resulting array and use Byte.valueOf() to get byte values.
Something like this?
byte [] Array = new byte[4096];
String ss= new String();
String Str= new String();
try{
do
{
sc.next(ss);
ss.split(Str);
for(int i=0 ; i < Str.length() ; i++)
Array[i] = Byte.valueOf(Str, i);
Socket skt = new Socket(s, 12345);
DataOutputStream out = new DataOutputStream(skt.getOutputStream());
out.write(Array);
out.flush();
}while(flag2);
Nope, that's not how the String.split() method works. Check out the String API. Also, please UseCodeTags when posting code. It makes it much easier to read.