Want to advertise here? Contact us!
Reputation Top 1%
Cristiano Ghersi
Great passion for Software Engineering, mostly coding in Java and C#, I enjoy creating modular and reusable code for everybody!
168 Snippets  (1st place)
13 Channels
51 Channels
3 hours ago
Last Visit
May 5, 2012
5163 points  (1st place)
Top 5% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Master Code Generator
Guru Code Generator
Junior Publisher
Serious Publisher
Senior Publisher
Master Publisher
Guru Publisher
Master Topic Creator
Guru Topic Creator
Junior Topic Hub
Serious Topic Hub
Senior Topic Hub
Master Topic Hub
Junior Trend Maker
Serious Trend Maker
Senior Trend Maker
Senior Team Manager
Junior Influencer
Serious Influencer
Senior Influencer
Junior Commenter
Serious Commenter
Senior Commenter
Master Commenter
Junior Judge
Serious Judge
Senior Judge
Master Judge
Junior Popular Coder
Serious Popular Coder
Senior Popular Coder
Master Popular Coder
Guru Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Master Autobiographer
Junior Snip2Coder
Serious Snip2Coder
Senior Snip2Coder
Junior Geek Aggregator
Serious Geek Aggregator
Senior Geek Aggregator
Junior Famous Coder

Recent Snippets See all snippets by cghersi

public by cghersi modified Friday at 3:57:33 PM  2  3  2

XOR in C#

Given that technically there isn't a XOR conditional operator (like &&, ||, etc.), the best way to perform a XOR in a conditional statement is to use != operator, because it evaluates to a boolean
bool a = true;
bool b = false;
if (a != b)
  return true;
  return false;

public by cghersi @ Snip2CodeDev modified Sep 19, 2014  5  3  4

Snip2Code Value Proposition

Here there's a depict of how Snip2Code could help companies in improving their performance in the software development activities
public static void checkEfficiency() {		
	//let the company adopt Snip2Code:
	Company acme = new Company(Goal.FASTER_TIME_TO_MARKET);
	//check how a coder become more efficient with the use of Snip2Code tools:
	Coder alice = acme.getCoder();
	Results[] aliceResults = alice.getOptimizations();
	//check results:
	if ((aliceResults[0] == Results.EFFICIENT_CODE_FIND) &&
		(aliceResults[1] == Results.REUSE_CODE) &&
		(aliceResults[2] == Results.PRESERVE_KNOWLEDGE) &&
		(aliceResults[3] == Results.SHARE_KNOWLEDGE)) {
		print("Yes, this is what you can get from Snip2Code!");			
	} else {			
		print("This is not an option!");			

public by cghersi modified Aug 12, 2014  1  3  2

Embed a browser into SWT Dialog and manage the firing of popups from links in the displayed page

This is how to manage an embedded browser. Note: unfortunately this seems not working on some versions of Mac OS X.
try {
	Browser browser = new Browser(this, SWT.NONE);
	browser.addOpenWindowListener(new OpenWindowListener() {
		public void open(WindowEvent event) {
			log.debug("Opening browser:" + event);
			new Thread( new Runnable() {
	            public void run() {
	            	log.debug("some stuff t do here...");
			final Shell shell = new Shell(event.display);
		    shell.setText("My Browser page");
		    shell.setLayout(new FillLayout());
		    event.browser = new Browser(shell, SWT.NONE);
		    event.browser.addCloseWindowListener(new CloseWindowListener() {
				public void close(WindowEvent event) {
} catch (Throwable t) {
	//on some architecture seems that SWT Browser is not working...
	log.warn("Cannot display SWT Browser");

public by cghersi modified Aug 5, 2014  1  3  0

How to simply compute the MD5 hash of a given string, being sure to have the same results retrieved by HASH method of MS SQL Server

Returns the MD5 hash of the given string
public static String computeMD5Hash(String input) {
	if (isNullOrEmpty(input))
		return "";
	byte[] bytesOfMessage = null;
	try {
		bytesOfMessage = input.getBytes("UTF-16LE");
	} catch (UnsupportedEncodingException e) {
		log.error("Cannot support UTF-8 encoding due to " + e, e);
		return "";
	MessageDigest md = null;
	try {
		md = MessageDigest.getInstance("MD5");
	} catch (NoSuchAlgorithmException e) {
		log.error("Cannot support MD5 algorithm due to " + e, e);
		return "";
	byte[] thedigest = md.digest(bytesOfMessage);
	return printHexByteArray(thedigest);

public by cghersi modified Aug 5, 2014  1  3  0

How to print a byte array in hexadecimal form in Java

Prints the given byte array as hexadecimal string
public static String printHexByteArray(byte[] input) {
    if (input == null)
        return "";
    StringBuilder sbs = new StringBuilder();
    for (int i = 0; i < input.length; i++) {
    	sbs.append(String.format("%02x", input[i] & 0xff));
    return sbs.toString();