A geek way to greet the New Year (or, how to convert a String to hexadecimal characters)

Here’s a way to impress your geek friends, or look really silly …

Below is Java code to convert a predefined New Year’s greeting into hexadecimal format.

[code]
/**
Copyright 2012 James G. Willmore

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import java.math.BigInteger;

/**
* A geek way to usher in the New Year.
*
* @author James G. Willmore
*
*/
public class GeekNewYear
{
// —————————————————————————

public static void main(String[] args)
{
//we need a single argument containing only numbers
if(args.length != 1 || !args[0].matches("[0-9]+"))
{
System.out.println("This app works better if you provide a single year.");
System.exit(1);
}

Integer year = Integer.parseInt(args[0]);
String greeting = "Happy New Year, " + year + "!";
String output = String.format("%022x", new BigInteger(greeting.getBytes()));

System.out.println(greeting + " converted to hexadecimal is " + output);
}

// —————————————————————————
}
[/code]

You can compile the code in your favorite command line / terminal window.

And run the application as follows and the following output is produced.

Enjoy!