Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,18 @@ object TypeConverter {
}
}

private def byteArrayToString (x: Array[Byte]) = "0x" + x.map("%02x" format _).mkString
private val HEX_CHARS = Array(
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
)
private def byteArrayToString (x: Array[Byte]) : String = {
val result = new StringBuilder(x.length * 2)
x.foreach { b =>
val v = b & 0xFF
result.append(HEX_CHARS(v >>> 4))
result.append(HEX_CHARS(v & 0x0F))
}
"0x" + result.toString()
}
private def stringToByteArray (x: String) = new BigInteger(x.substring(2), 16).toByteArray

private val ByteBufferTypeTag = implicitly[TypeTag[ByteBuffer]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ class TypeConverterTest {
assertEquals("a string", c.convert("a string"))
}

@Test
def testByteArrayToString() {
val c = TypeConverter.forType[String]
val byteArray : Array[Byte] = Array(1,3,-56,45,23,67)
assertEquals("0x0103c82d1743", c.convert(byteArray))
}

@Test
def testDate() {
val c = TypeConverter.forType[Date]
Expand Down