• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Subdivide string in bytes to a byte array

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic