Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

string to byte conversion

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following piece of code I wrote converts a string to a byte. It works but it looks awful. Can this be done in an easier way?

byte stringToByte(String s){
// Converts a string containing a byte on format
["0","255"]
// to Java byte format [-128,127].
byte b = 0;
if(Integer.parseInt(s) > 255 | | Integer.parseInt(s) < 0){<br /> System.out.println("Byte format expected. " +<br /> Integer.parseInt(s) + " is out of range.");<br /> }<br /> else<br /> {<br /> int n = Integer.parseInt(s);<br /> if( n > Byte.MAX_VALUE) n = n - 256;
Integer N = new Integer(0);
s = String.valueOf(n);
N = Integer.valueOf(s);
b = N.byteValue();
}
return b;
};
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't read your code, but are you looking for String.getBytes()?
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try s.charAt(0).
/Mike
 
Daniel Ringstrom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No that doesn't work. For example for the string "67" the method String.getBytes() returns 54 55 (ascii). What I want to do is to return 67 as a byte. The string "67" -> 67, "255" -> -1,
"128" -> -128, "127" -> 127 ...
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
is this doing the trick for you ?

karl
 
Daniel Ringstrom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Karl. That was exactly what I was looking for.
/Daniel
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why use Integer.parseInt(), risking overflow, when there's Byte.parseByte() ready-made, with error handling built in?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Byte.parseByte() will throw a NumberFormatException for Daniel's example of "128". But this opens a larger can of worms - what is the desired input range of the function? If 128 should be converted to -128, then a simple solution is
<pre>
byte stringToByte(String s){
return (byte) Integer.parseInt(s);
}
</pre>
The question is, if a number is outside the range of a byte, do you want to throw an error, or quietly convert if anyway? Karl's solution assumes 0 < x < 255 is the intended input range, which is a reasonable possibility. (What about the endpoints though?) Lots of possible ways to do this - it all depends on what you really need this function to do.
[This message has been edited by Jim Yingst (edited December 03, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic