Converting Java String from/to UTF-8

Java internally encodes String as UTF-16. If you need to send UTF-8 Java String, for example as CORBA string parameter, you must convert it in the following way:

public class StringHelper {

	// convert from UTF-8 -> internal Java String format
	public static String convertFromUTF8(String s) {
		String out = null;
		try {
			out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
		} catch (java.io.UnsupportedEncodingException e) {
			return null;
		}
		return out;
	}

	// convert from internal Java String format -> UTF-8
	public static String convertToUTF8(String s) {
		String out = null;
		try {
			out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
		} catch (java.io.UnsupportedEncodingException e) {
			return null;
		}
		return out;
	}

	public static void main(String[] args) {
		String xmlstring = "Здравей' хора";
		String utf8string = StringHelper.convertToUTF8(xmlstring);
		for (int i = 0; c < utf8string.length(); ++i) {
			System.out.printf("%x ", (int) utf8string.charAt(c));
		}
	}
}

ISO-8859-1 encoding is just used to transfer 8 bit array into a String.

How to debug a SConstruct/SConscript file with Eclipse/PyDev

1) Create a Pydev project
File -> New -> Pydev Project

Project name: scons_pydev
Uncheck “Use default” and Browse for “C:\Python26\Scripts” (on windows)
Uncheck “Create default ‘src’ folder …”

2) Copy “C:\Python26\Scripts\scons” as “C:\Python26\Scripts\scons.py”

3) Create a Debug configuration:
Run -> Debug Configurations…

4) Edit the main tab with “scons_pydev” as project e “scons.py” as Main module

5) Edit the “Arguments” tab, set the working directory to the root of your project (see Working Directory)

6) Set the breakpoint either on SConstruct or SConcript and run in debug mode

Trick: Debugging could be easier executing the following steps:
– modify the Main.py module in “C:\Python26\Lib\site-packages\scons-1.2.0\SCons\Script” as following:
# filelist = [‘SConstruct’, ‘Sconstruct’, ‘sconstruct’]
filelist = [‘SConstruct’, ‘Sconstruct’, ‘sconstruct’, ‘SConstruct.py’, ‘Sconstruct.py’, ‘sconstruct.py’]
– copy SConstruct as SConstruct.py

Python switch statement

Python doesn’t have a switch statement such as other programming languages but you can do the same thing with a dictionary:

import platform

def set_windows() :
   return
def set_linux() :
   return
def set_sunos() :
   return

platform_settings = {
           'Windows' : set_windows, 
           'Linux'   : set_linux, 
           'SunOS'   : set_sunos,
}

platform_settings[platform.system()]()

Making Eclipse look good on Linux

These are the steps I followed to customize Eclipse CDT UI on Linux:

  1. Install Red Hat Liberation fonts which are free/liberated Windows fonts available for any OS and readily available in Fedora’s repositories so they are very easy to install via yum.
  2. Change font properties with the following settings:
    • Application/Document/Desktop font: Liberation Sans, size 10
    • Window title font: Liberation Sans Bold, size 10
    • Fixed with font: Liberation Mono, 10
    • Font rendering: Best contrast
    • Under “Details”:
      • Resolution: 100 dpi
      • Smoothing: Grayscale
      • Hinting: Full
  3. Create an /~/.gtkrc-2.0 file with the following content:
    style "gtkcompact" {
    font_name=" Liberation Sans 10"
    GtkButton::default_border={0,0,0,0}
    GtkButton::default_outside_border={0,0,0,0}
    GtkButtonBox::child_min_width=0
    GtkButtonBox::child_min_heigth=0
    GtkButtonBox::child_internal_pad_x=0
    GtkButtonBox::child_internal_pad_y=0
    GtkMenu::vertical-padding=1
    GtkMenuBar::internal_padding=0
    GtkMenuItem::horizontal_padding=4
    GtkToolbar::internal-padding=0
    GtkToolbar::space-size=0
    GtkOptionMenu::indicator_size=0
    GtkOptionMenu::indicator_spacing=0
    GtkPaned::handle_size=4
    GtkRange::trough_border=0
    GtkRange::stepper_spacing=0
    GtkScale::value_spacing=0
    GtkScrolledWindow::scrollbar_spacing=0
    GtkExpander::expander_size=10
    GtkExpander::expander_spacing=0
    GtkTreeView::vertical-separator=0
    GtkTreeView::horizontal-separator=0
    GtkTreeView::expander-size=8
    GtkTreeView::fixed-height-mode=TRUE
    GtkWidget::focus_padding=0
    }
    class "GtkWidget" style "gtkcompact"
    
    style "gtkcompactextra" {
    xthickness=0
    ythickness=0
    }
    
    class "GtkToolbar" style "gtkcompactextra"
    class "GtkPaned" style "gtkcompactextra"
    
  4. Change every fixed font in Windows->Preferences->General->Appearance->Colors and Font with Liberation Mono 10
  5. Disable bold font in Windows->Preferences->C/C++->Editor->Syntax Coloring

SUN linker takes order parameters into account

While I was compiling a program written by a colleague of mine I encountered this strange error:

/opt/SUNWspro/bin/CC -o program program.o -lcurl -L/opt/libcurl/lib
ld: fatal: library -lcurl: not found
ld: fatal: File processing errors. No output written to program

The library was at the right place and everything seemed right until I checked the documentation and I discovered…

$man ld
...
-L path

Adds path to the library search directories. ld searches
for libraries first in any directories specified by the
-L options and then in the standard directories. This
option is useful only if the option precedes the -l
options to which the -L option applies.
The environment
variable LD_LIBRARY_PATH can be used to supplement the
library search path. See LD_LIBRARY_PATH under ENVIRON-
MENT VARIABLES.
...

So the correct version was the following:

/opt/SUNWspro/bin/CC -o program program.o -L/opt/libcurl/lib -lcurl