File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string to byte conversion

 
Daniel Ringstrom
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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;
};
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't read your code, but are you looking for String.getBytes()?
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try s.charAt(0).
/Mike
 
Daniel Ringstrom
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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 ...
 
karl koch
Ranch Hand
Posts: 388
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
is this doing the trick for you ?

karl
 
Daniel Ringstrom
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Karl. That was exactly what I was looking for.
/Daniel
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use Integer.parseInt(), risking overflow, when there's Byte.parseByte() ready-made, with error handling built in?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic