byte

How to convert an array of bytes to a hex string in Scala

If you need to convert an array of bytes to a hex string in Scala, I can confirm that this code works:

def convertBytesToHex(bytes: Seq[Byte]): String = {
    val sb = new StringBuilder
    for (b <- bytes) {
        sb.append(String.format("%02x", Byte.box(b)))
    }
    sb.toString
}

I just used this code as part of a checksum algorithm (SHA-1, SHA-256, etc.), and I tested it against command line checksum commands to verify that it works properly.

Scala: How to parse a number from a String

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 2.1, “Parsing a Number from a String.”

Problem

You want to convert a String to one of Scala’s numeric types (Byte, Double, Int, Float, Long, Short).

Scala data types: bit sizes, ranges, and docs

Scala FAQ: What are the Scala numeric data types? How many bits do they use to store their data, and what is the range of those data types?

Courtesy of the excellent book, Programming in Scala, here is a list and description of the Scala data types, including bit sizes and data ranges:

Data Type  Definition

Boolean    true or false

Byte       8-bit signed two's complement integer (-2^7 to 2^7-1, inclusive)
           -128 to 127

Short      16-bit signed two's complement integer (-2^15 to 2^15-1, inclusive)
           32,768 to 32,767

Int        32-bit two's complement integer (-2^31 to 2^31-1, inclusive)
           2,147,483,648 to 2,147,483,647

Long       64-bit two's complement integer (-2^63 to 2^63-1, inclusive)
           -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

Float      32-bit IEEE 754 single-precision float
           1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative)

Double     64-bit IEEE 754 double-precision float
           4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative)

Char       16-bit unsigned Unicode character (0 to 2^16-1, inclusive)
           0 to 65,535

String     a sequence of Chars

Java “file write” (or “file save”) methods

Java write/save FAQ: Can you share an example of how to write to a file in Java?

Sure. Here are two "Java file save" or "Java file write" examples, taken from my Java file utilities article (Java file utilities to write, save, open, read, and copy files). I'm not going to explain these too much, but if you need a simple method to write to a file in your Java programs, I hope these methods will help you.

A Ruby script to remove binary (garbage) characters from a text file

Problem: You have a file that should be a plain text file, but for some reason it has a bunch of non-printable binary characters (also known as garbage characters) in it, and you'd like a Ruby script that can create a clean version of the file.

Solution: I've demonstrated how to do this in another blog post by using the Unix tr command, but in case you'd like a Ruby script to clean up a file like this, I thought I'd write up a quick program and share it here.

Ruby - How to convert ASCII decimal (byte) values to characters alvin June 9, 2009 - 11:59am

Problem: You have a byte value, or a string of byte values, and you want to use a Ruby script to convert each byte to its equivalent ASCII character.

Solution

I just ran into this problem while working on a script to remove binary/garbage characters from a Unix text file. In short, the file had a bunch of binary "garbage" characters in it, and I wanted a clean version of the file that contained only printable ASCII characters in it.

Ruby - How to convert characters to ASCII decimal (byte) values

Problem: You have a character, or a string of characters, and you want to use a Ruby script to convert each character to its ASCII decimal (byte) value.

Solution

I just ran into this problem while working on a script to remove binary/garbage characters from a Unix text file. In short, all you have to do to convert a character in Ruby to its equivalent decimal ASCII code is use the ? operator in front of it, like this:

How to loop through each character in a Ruby String

Ruby character/string FAQ: How can I loop through each character in a Ruby String, and perform some operation on each character?

I'm currently using Ruby 1.8.6, and you can use the Ruby each_char method if you'll first require the jcode module. To be clear, this code will not work in Ruby 1.8.6:

a = 'hello, world'
a.each_char { |c|
  puts c
}

In fact it results in the following error: