Skip to content

Commit ca0ce83

Browse files
committed
Added an object parseHexString to convert hex strings to a byte array and also added tests for it.
1 parent 8fd1aaf commit ca0ce83

File tree

3 files changed

+85
-12
lines changed

3 files changed

+85
-12
lines changed

spra-web/src/main/scala/net/wiringbits/spra/ui/web/utils/Images.scala

+1-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package net.wiringbits.spra.ui.web.utils
22

33
import org.scalajs.dom
44
import org.scalajs.dom.{Blob, File}
5-
import scala.util.{Failure, Success, Try}
65
import scala.scalajs.js.Promise
76
import scala.scalajs.js.typedarray.{ArrayBuffer, Int8Array, Uint8Array}
87
import scala.scalajs.js
@@ -24,17 +23,7 @@ object Images {
2423
}
2524

2625
def convertHexToImage(imageHex: String): String = {
27-
// Check if the argument is a hexadecimal string"
28-
if (!imageHex.startsWith("\\x") || (imageHex.length % 2) == 1) {
29-
throw new IllegalArgumentException(s"Error: Expected a hexadecimal string but found: $imageHex")
30-
}
31-
// Remove the "\x" prefix from the hex string, as it's not part of the actual image data
32-
val hex = imageHex.tail.tail
33-
val imageBinary: Array[Byte] =
34-
Try(hex.grouped(2).map { hex => Integer.parseInt(hex, 16).toByte }.toArray) match {
35-
case Success(value) => value
36-
case Failure(_) => Array.empty
37-
}
26+
val imageBinary: Array[Byte] = ParseHexString.toByteArray(imageHex)
3827
val byteArray = Uint8Array(js.Array(imageBinary.map(_.toShort): _*))
3928
dom.URL.createObjectURL(dom.Blob(js.Array(byteArray.buffer)))
4029
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.wiringbits.spra.ui.web.utils
2+
3+
import scala.util.{Failure, Success, Try}
4+
5+
object ParseHexString {
6+
def toByteArray(hexString: String): Array[Byte] = {
7+
// Check if the argument is a hexadecimal string"
8+
if (!hexString.startsWith("\\x") || (hexString.length % 2) == 1) {
9+
throw new IllegalArgumentException(s"Error: Expected a hexadecimal string but found: $hexString")
10+
}
11+
// Remove the "\x" prefix from the hex string, as it's not part of the actual data
12+
val hex = hexString.tail.tail
13+
Try(hex.grouped(2).map { hex => Integer.parseInt(hex, 16).toByte }.toArray) match {
14+
case Success(value) => value
15+
case Failure(_) =>
16+
throw new IllegalArgumentException(s"Error: Expected a hexadecimal string but found: $hexString")
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package net.wiringbits.spra.admin
2+
3+
import net.wiringbits.spra.ui.web.utils.ParseHexString
4+
import org.scalatest.matchers.must.Matchers.{be, must}
5+
import org.scalatest.wordspec.AnyWordSpec
6+
import scala.util.Try
7+
8+
class ParseHexStringSpec extends AnyWordSpec {
9+
10+
"convert valids hex string to a byte array" should {
11+
val hexData = List(
12+
"\\x5F3A9C1B7D",
13+
"\\x9E2D5B8F2A",
14+
"\\xA3B7D2E6C4",
15+
"\\x4F1E9A6D3B",
16+
"\\x6C2A8F4B7E",
17+
"\\xA3B9F56E8D4C721F9A6D3F2C",
18+
"\\x5F2C8E7B9A1D4E6F3B7A4F2D",
19+
"\\x7E9B6C2A5F8D4B3C6A2E1F9D",
20+
"\\xD4A7C8F25B3E7A9F4C8D6E1B",
21+
"\\xF3A59B4C7D2E8F1A6B9D3C4F2A7E1D5C9A3B6E8D4F2C1A7B3D9E4F1C6A2B"
22+
)
23+
24+
val conversionBytea: List[Array[Byte]] = List(
25+
Array(0x5f, 0x3a, 0x9c, 0x1b, 0x7d),
26+
Array(0x9e, 0x2d, 0x5b, 0x8f, 0x2a),
27+
Array(0xa3, 0xb7, 0xd2, 0xe6, 0xc4),
28+
Array(0x4f, 0x1e, 0x9a, 0x6d, 0x3b),
29+
Array(0x6c, 0x2a, 0x8f, 0x4b, 0x7e),
30+
Array(0xa3, 0xb9, 0xf5, 0x6e, 0x8d, 0x4c, 0x72, 0x1f, 0x9a, 0x6d, 0x3f, 0x2c),
31+
Array(0x5f, 0x2c, 0x8e, 0x7b, 0x9a, 0x1d, 0x4e, 0x6f, 0x3b, 0x7a, 0x4f, 0x2d),
32+
Array(0x7e, 0x9b, 0x6c, 0x2a, 0x5f, 0x8d, 0x4b, 0x3c, 0x6a, 0x2e, 0x1f, 0x9d),
33+
Array(0xd4, 0xa7, 0xc8, 0xf2, 0x5b, 0x3e, 0x7a, 0x9f, 0x4c, 0x8d, 0x6e, 0x1b),
34+
Array(0xf3, 0xa5, 0x9b, 0x4c, 0x7d, 0x2e, 0x8f, 0x1a, 0x6b, 0x9d, 0x3c, 0x4f, 0x2a, 0x7e, 0x1d, 0x5c, 0x9a, 0x3b,
35+
0x6e, 0x8d, 0x4f, 0x2c, 0x1a, 0x7b, 0x3d, 0x9e, 0x4f, 0x1c, 0x6a, 0x2b)
36+
).map(_.map(_.toByte))
37+
38+
hexData.zip(conversionBytea).foreach { case (hex, expectedBytes) =>
39+
s"convert valid data $hex" in {
40+
ParseHexString.toByteArray(hex) must be(expectedBytes)
41+
}
42+
}
43+
}
44+
45+
"throw an exception for a string containing non-hexadecimal characters" should {
46+
val hexData = List(
47+
"\\a5F3A9C1B7D",
48+
"9E2D5B8F2A",
49+
"\\xG3B7D2E6C4",
50+
"\\x4F1E9A76D3B",
51+
"6C42A8F4G7E",
52+
"\\xA3B9F56E8P4C721F9A6D3F2C",
53+
"\\x5F2C8E7B9AA1D4E6F3B7A4F2D",
54+
"\\x7E96C2A5F8D4B3C6A2E1F9D",
55+
"\\xD4A7C8F25B3E7JLAKSNSLKAS",
56+
"\\T1542ABF3A59B4C7D2E8F1A6B9D3C4F2A7E1D5C9A3B6E8D4F2C1A7B3D9E4F1C6A2B"
57+
)
58+
59+
hexData.foreach { value =>
60+
s"throw an exeption for: $value" in {
61+
Try(ParseHexString.toByteArray(value)).isFailure must be(true)
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)