Oracle, Web, Script, SQLserver, Tips & Trick

January 24, 2011

Decimal, hexa, biner converter

Filed under: oracle — sikathabis @ 9:35 am
Tags:

———————————-Package specification

CREATE OR REPLACE PACKAGE conv AS   FUNCTION bin2dec (binval IN CHAR  ) RETURN NUMBER;

FUNCTION dec2bin (N      IN NUMBER) RETURN VARCHAR2;

FUNCTION oct2dec (octval IN CHAR  ) RETURN NUMBER;

FUNCTION dec2oct (N      IN NUMBER) RETURN VARCHAR2;

FUNCTION hex2dec (hexval IN CHAR  ) RETURN VARCHAR2;

FUNCTION dec2hex (N      IN NUMBER) RETURN VARCHAR2;

END conv;/

———————————-Package Body

CREATE OR REPLACE PACKAGE BODY conv AS FUNCTION bin2dec (binval IN CHAR) RETURN NUMBER ISi NUMBER;digits NUMBER;result NUMBER := 0;current_digit CHAR(1);current_digit_dec NUMBER;BEGINdigits := LENGTH(binval);FOR i IN 1..digits LOOPcurrent_digit := SUBSTR(binval, i, 1);current_digit_dec := TO_NUMBER(current_digit);result := (result * 2) + current_digit_dec;END LOOP;RETURN result;END bin2dec;

FUNCTION dec2bin (N IN NUMBER) RETURN VARCHAR2 ISbinval VARCHAR2(64);N2 NUMBER := N;BEGINWHILE ( N2 > 0 ) LOOPbinval := MOD(N2, 2) || binval;N2 := TRUNC( N2 / 2 );END LOOP;RETURN binval;END dec2bin;

FUNCTION oct2dec (octval IN CHAR) RETURN NUMBER ISi NUMBER;digits NUMBER;result NUMBER := 0;current_digit CHAR(1);current_digit_dec NUMBER;BEGINdigits := LENGTH(octval);FOR i IN 1..digits LOOPcurrent_digit := SUBSTR(octval, i, 1);current_digit_dec := TO_NUMBER(current_digit);result := (result * 8) + current_digit_dec;END LOOP;RETURN result;END oct2dec;

FUNCTION dec2oct (N IN NUMBER) RETURN VARCHAR2 ISoctval VARCHAR2(64);N2 NUMBER := N;BEGINWHILE ( N2 > 0 ) LOOPoctval := MOD(N2, 8) || octval;N2 := TRUNC( N2 / 8 );END LOOP;RETURN octval;END dec2oct;

FUNCTION hex2dec (hexval IN CHAR) RETURN varchar2 ISi NUMBER;digits NUMBER;result NUMBER := 0;current_digit CHAR(1);current_digit_dec NUMBER;BEGINdigits := LENGTH(hexval);FOR i IN 1..digits LOOPcurrent_digit := SUBSTR(hexval, i, 1);IF current_digit IN (‘A’,’B’,’C’,’D’,’E’,’F’) THENcurrent_digit_dec := ASCII(current_digit) – ASCII(‘A’) + 10;ELSEcurrent_digit_dec := TO_NUMBER(current_digit);END IF;result := (result * 16) + current_digit_dec;END LOOP;RETURN ltrim(to_char(result));END hex2dec;

FUNCTION dec2hex (N IN NUMBER) RETURN VARCHAR2 IShexval VARCHAR2(64);N2 NUMBER := N;digit NUMBER;hexdigit CHAR;BEGINWHILE ( N2 > 0 ) LOOPdigit := MOD(N2, 16);IF digit > 9 THEN hexdigit := CHR(ASCII(‘A’) + digit – 10);ELSEhexdigit := TO_CHAR(digit);END IF;hexval := hexdigit || hexval;N2 := TRUNC( N2 / 16 );END LOOP;RETURN hexval;END dec2hex;END conv;/

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Teknologi Pendidikan

Just another WordPress.com weblog

KATAKAMI

Jurnalisme Yang Dapat Dipercaya

Tonk Kosonk Baonk Bunyinya

Penampungan Serapah Tanpa Sumpah

Qey050489's Blog

Ayo Bangkit sobat..!!!!!

B. S. Totoraharjo

Orang Bodoh Yang Tak Kunjung Pandai

culturekitchen foodlab

New Food and Art in Yogyakarta

Wahyu Catur Wibowo

Musafir Dunia Maya

SQL.... Still Learning

My Reference on SQL Server

neinframe

Street Photography is like a box of a chocolates, you're never know what you're gonna get - Thomas Leuthard -

Keluarga Andrivito

-- Andrivito's Journal --

DRK™

This site is about Latest NEWS, Articles, Videos, Books, Course, Presentations, Tips & Tricks related to I.T Pro's

Gaurav Saxena

|| तत् त्वम् असि ||

Be a good Moeslim

Menuju Pribadi Muslim Sejati yang Bertaqwa

TuanSUFI

Manajemen Sholat, renungan spiritual, Teknologi Informasi

Pelangi di khatulistiwa....

Mari berbagi kebaikan, pengalaman dan senyum denganku

angkasa13

Ceritanya saya, Pikirannya saya, Hatinya saya

sikathabis

sikathabis semua informasi disini

%d bloggers like this: