Archive

Archive for the ‘Linux’ Category

od command in Linux and a class in Java

February 18, 2011 Leave a comment

Because of my strong desire to earn LPI certification (Linux Professional Institute Certification), in between my studies, I also read the LPIC-1 Linux Guide. Today I was working on the od command in Linux. It is not hard to type od filename and see what it returns, but I was having a hard time to understand what exactly was going on so I started to convert numbers from base eight to ten and then to base two (I know… I could just convert them to base two directly :P) to see what was going on, but I was making calculation mistakes, and since I also like coding, I thought I’d build a Java class that does conversions for me and make my studying od easier. Read on for what od actually does, and for the code in Java.

od stands for octal dump. If one just types in a terminal od filename, the output is a nine column retsult of numbers in base eight. The first column indicates the bytes represented in that row. for example, column one in row 1 will output 0000000 and row two, column one will output 0000020. To understand what this means, lets first convert 0000020 to decimal, getting 16. Now, the 0000000 and 0000020 indicate that columns two to nine in row one contain bytes one through 16. Now one might ask: if there are 16 bytes represented, why only eight columns (excluding the first)? Well, every two bytes are combined to give one octal number. Say for example you have bytes AB, and you pass this content to od command. the output will be :
0000000 041101 000012
0000003
If one converts 041101 into binary, the result is:
0 100 001 001 000 001, which equals the following two bytes:
01000010 01000001, which then equal the ACII, binary, values of characters A and B.
The 000012, will be output even if od is passed no text at all. I looked up the ASCII table, and found out that octal 000012 (decimal ten) is the value of new line character. So, seems like od assumes that every file, even an empty one ends with a new line character, so it outputs either 000012 or what it gets after combining this byte with another (in case the number of bytes in a file is odd, else, 000012 is not combined with any other byte and is output as is).

UPDATE: The 0000012 ASCII values is actually the form feed character. More on that in the following Wikipedia link.

The number 0000003 in column one, row two, in this case, indicates that there are three bytes shown in the previous row. Note that each row has at most nine columns. If there are no bytes to fill all nine columns of a row, the row will have less columns.

Now, the od command may be passed parameters to output decimal ASCII values of each byte separately (od -t dC filename), or hexadecimal values obtained by combining two consecutive bytes (od -x filename). There are other options that may be passed to od command, but the logic behind it is explained here, I believe.

As far as the code in Java goes, I will only copy paste the code. The comments in it are sufficiently explanatory:


import java.util.*;
import javax.swing.*;

public class ConvertNumber{

public static void main(String[] args){
//this line of code prompts a user to specify the method they want to use
String met = JOptionPane.showInputDialog("which method do you need:\ndecToAny(type d), or the method\nto convert a number with a lower base to decimal(type td)").toUpperCase();

//this block of code is executed if the user wants to run the method that generates a decmial numbers representation
//in a base lower than ten
if(met.equals("D")){
String s = JOptionPane.showInputDialog("type in the decimal number and the base you want it converted to with a comma in between (type dec-num, new-base (new base should be lower than ten greater than 1))");
StringTokenizer st = new StringTokenizer(s, ",");
int x = new Integer(st.nextToken().trim()).intValue();
int y = new Integer(st.nextToken().trim()).intValue();

System.out.println(decToAny(x, y));
}

//this block of code is executed if the user wants a number converted to base ten
if(met.equals("TD")){
String s = JOptionPane.showInputDialog("give the number and the base it is in with a comma in between (num,base)");
StringTokenizer str = new StringTokenizer(s, ",");
int num = new Integer(str.nextToken().trim()).intValue();
int b = new Integer(str.nextToken().trim()).intValue();
System.out.println(toDec(num, b));
}
}

public static String decToAny(int x, int baza){
String rez = "";
//this method converts a number that's in base ten to a base lower than ten and greater than two
//the number to convert is x and the base to which to convert is baza
//the result is a string. all one needs to do to convert it to int is the "new Integer(rez).intValue()" line
while(x > 0){
rez = x%baza + rez;
x = x/baza;
}

return rez;
}

//this method converts a number x in base baza into a decimal number
public static int toDec(int x, int baza){
int rez = 0;
int lim = ("" + x).length();
for(int i = 0; i < lim; i++){
//this line converts number x to string. it then iterates through that string from character
//at position 0 to the last and converts the character back to int and multiplies it with
//base raised to the power indicated by lim - i - 1 (lim is the number of digits in the number)
//since character at position zero is raised to a power that is less than the number od digits by one
//you get that from the following lim - i - 1.
rez = new Integer("" + ("" + x).charAt(i)).intValue() * (int)Math.pow(baza, lim - i - 1) + rez;
}
return rez;

}

}

If there is anything you would want me to add or clarify further as far as anything related here goes, please comment with the question you would want me to tackle. I will study it, and will make a post for it.

Advertisements
Categories: Java, Linux

easyLife makes life easier

January 14, 2011 Leave a comment

Hey ther. This time I am going to share with you a nice tool I found that helps transition to Linux’s distro Fedora 14 easier.

Just recently, I somehow damaged my Fedora files on my dual boot system (the other OS is Windows XP). Then, I somehow damaged my internet connection in xp, and so I thought I boot my Fedora 14. I put in the cd, reinstalled Fedora and wanted to eat something. I also wanted to listen to some music, but it would take a little bit of searching around for me, so I gave up. After I ate, I started searching, and what I found is a pretty cool app called easyLife.

easyLife is a simple, and light on memory, app that gives you a list of applications to chose from, and can install them for you on your system. For someone that just wanted to start using Linux and worries whether that is too much of a challenge, then easyLife makes life easier. It is not like life without easyLife is much harder for newbies in Linux world, but easyLife makes things quicker.

Amongst the applications it offers are Adobe Flash and many media players, which are the first things a regular Desktop user wants on their system. In fact, there are other appas like Utils, which include PDF Virtual Printer, 7zip, unrar, isomaster and other tools, that easyLife can install for you. easyLife can also install Java for you 🙂 If you are curious and up for something new go check it. It’s easy to use.

Once you donwload it from this link, you go to Places, then Downloads on your system. After that, you find the file easylife-2.3-3.noarch.rpm. You double click on this file. You are promted to install or cancel, and you of course chose install. After easyLife is installed, you will have an icon on your desktop that corresponds to easyLife. Double click on that, and you will be shown after a few seconds the list of apps you can install through easyLife. Chose anything you’d like. Other than codecs and flash player, I also chose themes 🙂

Enjoy!

Categories: Linux