Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Subdivide string in bytes to a byte array  RSS feed

 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


I know but this values will not pass from 102. Im tryin to make the connection and send this commands over a socket just in for strat, then i'll take the control over the comands. The user will only choose from those available ;)
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.





Well it worked. ;) thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!