Posts tagged to

Binary, Octal, Decimal and Hexadecimal converter(Java)

0

I was working on a simple project that simulate CPU registers, where users can use an assembly-like commands to issue some process while watching changes on the virtual CPU registers.

you may visit it using this link: http://www.doitmyway.net/2010/02/21/cpu-registers-simulator-educational-project/

In fact CPU data are in binary, although users may want to read those data in different format. So I implemented a simple Java class to convert numbers from/to binary, octal, decimal and hexadecimal.

There are 6 main functions:

  • decToBin
  • binToDec
  • octToBin
  • binToOct
  • hexToBin
  • binToHex

The other functions are supporting functions or functions based on the main functions, e.g, decToHex is a combination of decToBin and binToHex.

baseConvertor.java

//DoItMyWay.net
//a7med.yamani@gmail.com
//licensed under gpl v3 :)
 
package cpuregsim;
 
public class baseConvertor {
 
    //reverse string, give it "abc" and it will returns "cba"
    public String reverseString(String str) {
        str = str.trim();
        int strl = str.length();
        if (strl > 1) {
            StringBuffer sb = new StringBuffer();
            for (int i = strl - 1; i >= 0; i--) {
                sb.append(str.charAt(i) + "");
            }
            return sb.toString();
        }
        return str;
    }
 
    //convert decimal to hex
    public String decToHex(String dec) {
        return binToHex(decToBin(dec));
    }
 
    //convert decimal to oct
    public String decToOct(String dec) {
        return binToOct(decToBin(dec));
    }
 
    //convert decimal to bin
    public String decToBin(String dec) {
        if (!isDecimal(dec)) {
            return "Invalid input!";
        }
        int val = Integer.parseInt(dec);
        if (val == 0) {
            return "0";
        }
        StringBuffer bin = new StringBuffer();
        for (; val != 1; val = val / 2) {
            bin.append((val % 2) + "");
        }
        bin.append("1");
        return reverseString(bin.toString());
    }
 
    //convert oct to decimal
    public String octToDec(String oct) {
        return binToDec(octToBin(oct));
    }
 
    //convert oct to hex
    public String octToHex(String oct) {
        return binToHex(octToBin(oct));
    }
 
    //convert oct to bin
    public String octToBin(String oct) {
        if (!isOctal(oct)) {
            return "Invalid input!";
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < oct.length(); i++) {
            if (oct.charAt(i) == '0') {
                sb.append("000");
            } else if (oct.charAt(i) == '1') {
                sb.append("001");
             } else if (oct.charAt(i) == '2') {
                sb.append("010");
            } else if (oct.charAt(i) == '3') {
                sb.append("011");
            } else if (oct.charAt(i) == '4') {
                sb.append("100");
             } else if (oct.charAt(i) == '5') {
                sb.append("101");
            } else if (oct.charAt(i) == '6') {
                sb.append("110");
            } else if (oct.charAt(i) == '7') {
                sb.append("111");
            }
        }
        return Long.parseLong(sb.toString()) + "";
    }
 
    //convert hex to decimal
    public String hexToDec(String hex) {
        return binToDec(hexToBin(hex));
    }
 
    //convert hex to oct
    public String hexToOct(String hex) {
        return binToOct(hexToBin(hex));
    }
 
    //convert hex to bin
    public String hexToBin(String hex) {
        hex = hex.toUpperCase();
        if (!isHexaDecimal(hex)) {
            return "Invalid input!";
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < hex.length(); i++) {
            if (hex.charAt(i) == '0') {
                sb.append("0000");
            } else if (hex.charAt(i) == '1') {
                sb.append("0001");
            } else if (hex.charAt(i) == '2') {
                sb.append("0010");
            } else if (hex.charAt(i) == '3') {
                sb.append("0011");
            } else if (hex.charAt(i) == '4') {
                sb.append("0100");
            } else if (hex.charAt(i) == '5') {
                sb.append("0101");
            } else if (hex.charAt(i) == '6') {
                sb.append("0110");
            } else if (hex.charAt(i) == '7') {
                sb.append("0111");
            } else if (hex.charAt(i) == '8') {
                sb.append("1000");
            } else if (hex.charAt(i) == '9') {
                sb.append("1001");
            } else if (hex.charAt(i) == 'A') {
                sb.append("1010");
            } else if (hex.charAt(i) == 'B') {
                sb.append("1011");
            } else if (hex.charAt(i) == 'C') {
                sb.append("1100");
            } else if (hex.charAt(i) == 'D') {
                sb.append("1101");
            } else if (hex.charAt(i) == 'E') {
                sb.append("1110");
            } else if (hex.charAt(i) == 'F') {
                sb.append("1111");
            }
        }
        return Long.parseLong(sb.toString()) + "";
    }
 
    //is input represents a Binary number
    public boolean isBinary(String bin) {
        return bin.matches("[0|1]+");
 
    }
 
    //is input represents a Octal number
    public boolean isOctal(String bin) {
        return bin.matches("[0-7]+");
 
    }
 
    //is input represents a Hexadecimal number
    public boolean isHexaDecimal(String bin) {
        return bin.toUpperCase().matches("[0-9|A-F]+");
 
    }
 
    //is input represents a decimal number
    public boolean isDecimal(String bin) {
        return bin.matches("[0-9]+");
 
    }
 
    //convert bin to decimal
    public String binToDec(String bin) {
        if (!isBinary(bin)) {
            return "Invalid input!";
        }
        int nOfDigits = bin.length();
        int val = 0;
        bin = reverseString(bin);
        for (int i = 0; i < nOfDigits; i++) {
            try {
                val = val + Integer.parseInt(bin.charAt(i) + "") * ((int) Math.pow(2, i));
            } catch (NumberFormatException nfe) {
                return "Invalid input!";
            }
        }
        return val + "";
    }
 
    //convert bin to oct
    public String binToOct(String bin) {
        if (!isBinary(bin)) {
            return "Invalid input!";
        }
        String[] tokens = splitByWidth(bin, 3, '0');
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].equals("000")) {
                sb.append("0");
            } else if (tokens[i].equals("001")) {
                sb.append("1");
            } else if (tokens[i].equals("010")) {
                sb.append("2");
            } else if (tokens[i].equals("011")) {
                sb.append("3");
            } else if (tokens[i].equals("100")) {
                sb.append("4");
            } else if (tokens[i].equals("101")) {
                sb.append("5");
            } else if (tokens[i].equals("110")) {
                sb.append("6");
            } else if (tokens[i].equals("111")) {
                sb.append("7");
            }
        }
  return sb.toString();
    }
 
    //convert bin to hex
    public String binToHex(String bin) {
        if (!isBinary(bin)) {
            return "Invalid input!";
        }
        String[] tokens = splitByWidth(bin, 4, '0');
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].equals("0000")) {
                sb.append("0");
            } else if (tokens[i].equals("0001")) {
                sb.append("1");
            } else if (tokens[i].equals("0010")) {
                sb.append("2");
            } else if (tokens[i].equals("0011")) {
                sb.append("3");
            } else if (tokens[i].equals("0100")) {
                sb.append("4");
            } else if (tokens[i].equals("0101")) {
                sb.append("5");
            } else if (tokens[i].equals("0110")) {
                sb.append("6");
            } else if (tokens[i].equals("0111")) {
                sb.append("7");
            } else if (tokens[i].equals("1000")) {
                sb.append("8");
            } else if (tokens[i].equals("1001")) {
                sb.append("9");
            } else if (tokens[i].equals("1010")) {
                sb.append("A");
            } else if (tokens[i].equals("1011")) {
                sb.append("B");
            } else if (tokens[i].equals("1100")) {
                sb.append("C");
            } else if (tokens[i].equals("1101")) {
                sb.append("D");
            } else if (tokens[i].equals("1110")) {
                sb.append("E");
            } else if (tokens[i].equals("1111")) {
                sb.append("F");
            }
        }
        return sb.toString();
    }
 
    //split string by specified width and fill empty spaces
    public String[] splitByWidth(String str, int width, char fillChar) {
        String[] list = null;
        int strLen = str.length();
        StringBuffer sb = new StringBuffer(reverseString(str));
        int mod = strLen % width;
        if (!(mod == 0)) {
            for (int i = 0; i < width - mod; i++) {
                sb.append(fillChar);
            }
        }
        strLen = sb.length();
        list = new String[strLen / width];
        sb = new StringBuffer(reverseString(sb.substring(0)));
        StringBuffer tmp = new StringBuffer();
        int listCounter = 0;
        for (int i = 0; i < strLen; i++) {
            if (i % width == 0 && i != 0) {
                list[listCounter] = tmp.toString();
                listCounter++;
                tmp = new StringBuffer();
            }
            tmp.append(sb.charAt(i));
        }
        list[listCounter] = tmp.toString();
        listCounter++;
        return list;
    }
}


pligg rtl pagination

3

hello,

pligg diflucan dosage is digg clone. it is a very famous one. i am working on converting its template from LTR to RTL. one of the problems is the pagination. it is can not be changed generic levitra using CSS. so i opened html1.php and do the following.

first: open html1.php and find this line

for ($i=$start;$i<=$end &amp;&amp; $i<= $total_pages;$i++) {
if($i==$current) {
$output .= ‘<span>’.$i.’</span>’;
} buy augmentin else {
$output .= ‘<a href=”?page=’.$i.$query.’”>’.$i.’</a>’;
}
}

change moneygram california “>buy generic levitra online cheap Drugstore online Ampicillin buy it to this one

/**pliggArabia.co.cc**/
$temp=”";
for ($i=$start;$i<=$end && $i<= $total_pages;$i++) {
if($i==$current) {
$temp = ‘<span>’.$i.’</span>’.$temp;
} else {
$temp = ‘<a href=”?page=’.$i.$query.’”>’.$i.’</a>’.$temp;
}
}

$output .= $temp;
/**pliggArabia.co.cc**/

this code will reverse the order of the added links from:

previous,1,2,3,4,…,next

to

previous,…,4,3,2,1,next

now you should use the css to swap “next” and “previous”

you may get a complete copy of an arabic pligg from this link

pliggArabia.co.cc

all cheap Amoxil online buy without prescription the best

add jFileChooserFilter to your java code

1

Hi,

Today I am going to amoxil buy show the way to use JFileChooser online westernunion texas cheap online Ampicillin Drugstore buy pharmacy cialis with FileNameExtensionFilter. This is very useful for building user friendly applications. FileNameExtensionFilter is used to tell which files extentions are allowed for the jFileChooser. This will make files with specific extensions appear and all other files disappear from the jfilechooser dialog.

First of all we need to create a list of the accepted extensions. generic levitra This is an example:

String [] acceptedExt={“txt”,”doc”};

Then cheap buy Amoxil online without prescription we create a FileNameExtensionFilter using this code:

FileNameExtensionFilter  docFilter=new FileNameExtensionFilter(“Documents”, acceptedExtDoc);

Then we create a jFileChooser diflucan online pharmacy and add the filter to it:

JFileChooser jf=new JFileChooser();

jf.addChoosableFileFilter(docFilter);

finally, do not forget to show it for your users:

jf.showOpenDialog(null);

download example from here: jFileChooserExample

:) buy levitra medication

SNAG-0025

easy way to pass odesk reading test

88


Hi all,

Today I am going to reveal an extremely easy way to pass oDesk Readiness Test for Independent Contractors and Company Managers.

This method is not about cheating, it is just about utilizing your results in old exams. still, using it i scored 5 out of 5 in 2 minutes, and i am ranked as the 1st :)

So here we go:

As you know, oDesk readiness test consists of 11 questions. They will give you 60 minutes for this exams. The exam is an open book exam, add to that they will provide you a link for a specific page that will help you getting the answer for that question.

Most important part in the exam is the final page. This page will show your results and pointers to questions with wrong answers.

odesk readiness test results

oDesk Readiness Test

 

Those are important facts to mention:

  • Question that not mentioned in the last page are answered correctly by you.
  • Questions of this test are repeated questions. this could be changed after this article :)

Here comes the trick:

  1. Start the test
  2. Answer the question
  3. Press on the “print screen”, save the snap shot in an image, use MSpaint or snagit
  4. Press next
  5. Read the  next question, and repeat steps (2-5) until the end
  6. In the last page, see which questions are answered correctly and which are  not. Do not forget to snap shot this page.
  7. In the next test, utilize the pictures and the results to determine the correct answers
  8. Repeat those steps as needed

by the way i found this fact, if there is an option “all of the above” then it will be the correct answer. else, the first choose is the correct answer.

just to make life easier, those are my screen snapshots:

SNAG-0003SNAG-0004SNAG-0005SNAG-0006SNAG-0007SNAG-0008SNAG-0009SNAG-0010SNAG-0012SNAG-0024

All the best

Go to Top