score:67
Accepted answer
This doesn't handle null
in the same way as your code.
object Hex {
def valueOf(buf: Array[Byte]): String = buf.map("%02X" format _).mkString
def valueOf(o: Byteable): String = valueOf(o.toByteArray)
}
If you want to be able to handle possibly-null
arrays, you might be better doing that from calling code and doing:
val bytes: Array[Byte] = // something, possibly null
val string: Option[String] = Option(bytes).map(Hex.valueOf)
score:5
You should use Scala's Option
type instead of null
. (This is tested with Scala 2.8.0.RC1)
object Hex {
def valueOf (buf: Array[Byte]) = {
if (null == buf) {
None
} else {
val sb = new StringBuilder(buf.length * 2)
for (b <- buf) {
sb.append("%02X".format(b & 0xff))
}
Some(sb.toString())
}
}
/*
def valueOf(o: Byteable) = {
return valueOf(o.toByteArray());
}
*/
}
println(Hex.valueOf(Array(-3, -2, -1, 0, 1, 2, 3)))
println(Hex.valueOf(null))
score:9
Perhaps there are more elegant ways, but something like:
def valueOf(bytes : List[Byte]) = bytes.map{
b => String.format("%02X", new java.lang.Integer(b & 0xff))
}.mkString
should work.
Source: stackoverflow.com
Related Query
- What is/are the Scala way(s) to implement this Java "byte[] to Hex" class
- What are the differences and similarties between Scala traits vs. Java 8 interfaces?
- What are the differences between a Scala Future and a Java Future
- What is the Java equivalent of this Scala code?
- What are the benefits of using the scala akka toolkit vs java akka toolkit version?
- What are the correct apply and unapply methods to avoid this java.lang.ClassCastException error in a Scala Play app?
- Instantiating Scala Class from Java when both are in the same project
- What are All the ways we can run a scala code in Apache-Spark?
- What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala alternatives?
- What are the key differences between Scala and Groovy?
- What are the relationships between Any, AnyVal, AnyRef, Object and how do they map when used in Java code?
- What are the disadvantages to declaring Scala case classes?
- What are the differences between final class and sealed class in Scala?
- What's the Scala way to implement a retry-able call like this one?
- What are the differences and similarities of Scala and Haskell type systems?
- What are the differences between Scala middleware choices?
- What is the Scala equivalent to a Java builder pattern?
- What are the biggest differences between Scala 2.8 and Scala 2.7?
- What is the difference between a class and a type in Scala (and Java)?
- What types are special to the Scala compiler?
- What are the main differences between Scala and Frege (in programming paradigms)?
- Scala class to implement two Java Interfaces - how?
- what are the options for hadoop on scala
- What does => mean at the beginning of a Scala class definition?
- "functions are first class values" what does this exactly mean?
- What are the good Scala IDEs at the start of 2010?
- What are the use cases for Scala 2.9's try...catch generalization?
- What does the tilde (~) mean in this Scala example?
- What should a Scala developer know about Java and/or the JVM?
- What are the benefits of using Scala in .Net?
More Query from same tag
- What does Array((1L, 2L)) do?
- error to import graphx library in scala project
- In Spark Structured streaming with Kafka, how spark manages offset for multiple topics
- How to import an user defined package into scala REPL automatically when scala REPL is started?
- Akka Actor internal state during shard migration in a cluster
- How to send POST data with an AngularJS form to Play Framework Controller
- error: missing argument list for method sql, when trying to access config options
- Kilim with Scala: is it possible and as mature as more "standard" Scala concurrency approaches?
- Effective record linkage
- Anorm compare/search by java.time LocalDateTime
- ZMQ missing events being propagated in jeromq scala
- Setting variable to default value
- 'Deadlock' in scala
- Garbage value in Some/None without return statement
- Intellij IDEA appends and unerscore to the "expecpted" type name, why?
- org.apache.spark.sql.AnalysisException: Reference 'dattim' is ambiguous, could be: dattim#6, event_dattim#55.;
- How to find a highest scoring word inside a string?
- Scala.js Runtime Compilation to Javascript
- shapeless filter a list of options
- Why am i getting Set[Char] instead of Set[String]?
- How to set up intermediary table in MySQL for ManyToMany relations in squeryl for Play 2 framework?
- Why can't I assign a Map variable to an AbstractSeq variable in Scala?
- Scala - template method pattern in a trait
- Play framework Route case insensitive
- Scala regexps: how to return matches as array or list
- java.net.URISyntaxException: Relative path in absolute URI
- unable to run web-app developed using scalatra scala
- Difficult with Scala's implicit parameter resolution
- Why would a Scala Worksheet using Scala-IDE give this as an error?
- scala tools nsc: set compiler flags in compiler settings