Skip to content

Instantly share code, notes, and snippets.

@lilac
Created November 27, 2024 02:55
Show Gist options
  • Save lilac/480ff3969be46455be41a168bed5f4ac to your computer and use it in GitHub Desktop.
Save lilac/480ff3969be46455be41a168bed5f4ac to your computer and use it in GitHub Desktop.
A Scala script that reads a hex string from the system clipboard and compute the length of it

Intro

This is a Scala script that reads a hex string from the system clipboard and compute the length of it.

Use case

It's mainly a utility for blob. Our data is stored in Cassandra (or ScyllaDB) as blob, but sometimes we need to analyze the data.

Usage

  1. Install scala-cli in your system.
  2. Copy the hex string to your clipboard.
  3. Run the script via scala-cli hex-length.scala.
//> using jvm graalvm:21
//> using dep "commons-codec:commons-codec:1.17.1"
import java.awt._
import java.awt.datatransfer._
import org.apache.commons.codec.binary.Hex
@main def main = {
val c = Toolkit.getDefaultToolkit.getSystemClipboard
// Get data stored in the clipboard that is in the form of a string (text)
val text = c.getData(DataFlavor.stringFlavor).toString
val bytes = Hex.decodeHex(text.substring(2))
println(bytes.length)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment