Phalox log

Everyday projects

You are not logged in.


Welcome to this project log. This website is still under development, please report any bugs to webmaster_at_phalox*dot*be.

#1 28-11-2010 14:55:56
Poster: phalox

Java & its negativity

Project: Java & its negativity

Yesterday I once more discovered a reason to dislike Java: almost all data types are signed and Sun refuses to make unsigned ones.
Every good language has signed and unsigned data types, so we can conclude that Java isn't one of them :)

For all future employees that might read this (and want me to work with Java), please prove me wrong! I like to be challenged once in a while ;-)

Why would one need an unsigned byte (for example)?
When having fun with microcontrollers and Java (and the RXTX library), one could find the urge to send and receive data that's larger than 127 (decimal).
Since signed bytes go from -128 to 127, you can't use a byte anymore if you want to represent the data in decimal numbers.

Sidenote: binairy operations are still the same. You will only bump into this problem once you start to do arithmetical operations on the data.

So... I want to handle the data as a decimal number (so it can easily be stored in a database), and I really don't need negative numbers.
That's when you start writing your own conversion class.
Next to that, there are also some other handy functions when working with microcontrollers (String representations, String(byte) -> byte, String(hex) -> byte, byte -> int...)

To prevent others from losing the time I spent on it, here is the entire class released under the GPL license.


 * This program is free software; you can redistribute and/or modify it
 * under the terms of the GNU General Public License (see  

 * @author
public class Converter {

        // Singleton:
    private static Converter uniqueInstance ;
        static final String HEXES = "0123456789ABCDEF";

     * Sets up the unique instance of the Singleton
     * @return The unique instance
    public static Converter getInstance() {
        if (uniqueInstance == null) {
            uniqueInstance = new Converter() ;
        return uniqueInstance ;
         * Converts a byte type to a hexadecimal string representation
         * @param tempByte byte
         * @return hexadecimal string
        public String byteToHexString(byte tempByte)
            String result = "0x";
            result = result + (HEXES.charAt((tempByte & 0xF0) >> 4));
            result = result + (HEXES.charAt((tempByte & 0x0F)));
            return result;
         * Converts a byte type to a bit string representation
         * @param tempByte
         * @return bit string
        public String byteToBitString(byte tempByte)
            StringBuffer buf = new StringBuffer();
            for (int i=0; i<8; i++)
                buf.append((int)(tempByte>>(8-(i+1)) & 0x0001));
            return buf.toString();
         * Converts a hexadecimal string to an integer
         * Reads only last two chars
         * @param tempStr hex string
         * @return integer value of hex string
        public int hexStringToInt(String tempStr)
            tempStr = tempStr.substring(tempStr.length()-2, tempStr.length());
            return Integer.parseInt(tempStr, 16);
         * Converts a bit string to an integer
         * @param tempStr bit string
         * @return integer value of bit string
        public int bitStringToInt(String tempStr)
            return Integer.parseInt(tempStr, 2);
         * Converts a byte type to an int that will always be positive
         * @param tempByte An 8 bit byte
         * @return positive (unsigned) value of byte in int
        public int byteToInt(byte tempByte)
            int byteValue = new Integer(tempByte).intValue( );
            if (byteValue < 0 )
                byteValue = tempByte & 0x80;
                byteValue += tempByte & 0x7F;
            return byteValue;
         * Converts an integer to a byte. Int should not be larger than 255
         * @param tempInt int not larger than 255
         * @return byte type (signed)
        public byte intToByte(int tempInt)
            return (byte)tempInt;

#2  27-03-2013 02:12:49


Re: Java & its negativity … -3071.html    sheath column one shoulder knee length chiffon mother of the bride dress wedding 3071    40992

#3  03-07-2018 09:31:39


Re: Java & its negativity

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
java training in chennai

Board footer

Powered by FluxBB