col "2進数" for a20
col "16進数" for a10
--■■■bitand関数を使う方法■■■
select val as "10進数",
to_char(sign(bitand(val,power(2, 8-1)))) ||
to_char(sign(bitand(val,power(2, 7-1)))) ||
to_char(sign(bitand(val,power(2, 6-1)))) ||
to_char(sign(bitand(val,power(2, 5-1)))) ||
to_char(sign(bitand(val,power(2, 4-1)))) ||
to_char(sign(bitand(val,power(2, 3-1)))) ||
to_char(sign(bitand(val,power(2, 2-1)))) ||
to_char(sign(bitand(val,power(2, 1-1)))) as "2進数",
to_char(val,'XX') as "16進数"
from (select 255 as val from dual union select 200 from dual union select 129 from dual
union select 128 from dual union select 127 from dual union select 2 from dual
union select 1 from dual union select 0 from dual)
order by val desc;
--■■■Replace関数を使う方法■■■
select val as "10進数",
LPad(LTrim(translate(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(
Replace(Replace(Replace(Replace(Replace(Replace(Replace(to_char(val,'XX')
,'0','----')
,'1','---+')
,'2','--+-')
,'3','--++')
,'4','-+--')
,'5','-+-+')
,'6','-++-')
,'7','-+++')
,'8','+---')
,'9','+--+')
,'A','+-+-')
,'B','+-++')
,'C','++--')
,'D','++-+')
,'E','+++-')
,'F','++++'),'+-','10')),8,'0') as "2進数",
to_char(val,'XX') as "16進数"
from (select 255 as val from dual union select 200 from dual union select 129 from dual
union select 128 from dual union select 127 from dual union select 2 from dual
union select 1 from dual union select 0 from dual)
order by val desc;
--■■■情報処理技術者試験で使われる方法■■■
select val as "10進数",
to_char(mod(trunc(val/power(2,7)),2)) ||
to_char(mod(trunc(val/power(2,6)),2)) ||
to_char(mod(trunc(val/power(2,5)),2)) ||
to_char(mod(trunc(val/power(2,4)),2)) ||
to_char(mod(trunc(val/power(2,3)),2)) ||
to_char(mod(trunc(val/power(2,2)),2)) ||
to_char(mod(trunc(val/power(2,1)),2)) ||
to_char(mod(val,2)) as "2進数",
to_char(val,'XX') as "16進数"
from (select 255 as val from dual union select 200 from dual union select 129 from dual
union select 128 from dual union select 127 from dual union select 2 from dual
union select 1 from dual union select 0 from dual)
order by val desc;
--■■■情報処理技術者試験で使われる方法(小数も対応)■■■
select val as "10進数",
to_char(mod(trunc(整数部/power(2,7)),2)) ||
to_char(mod(trunc(整数部/power(2,6)),2)) ||
to_char(mod(trunc(整数部/power(2,5)),2)) ||
to_char(mod(trunc(整数部/power(2,4)),2)) ||
to_char(mod(trunc(整数部/power(2,3)),2)) ||
to_char(mod(trunc(整数部/power(2,2)),2)) ||
to_char(mod(trunc(整数部/power(2,1)),2)) ||
to_char(mod(整数部,2)) || '.' ||
trunc(小数部*2) ||
trunc(mod(小数部*power(2,1),1)*2) ||
trunc(mod(小数部*power(2,2),1)*2) ||
trunc(mod(小数部*power(2,3),1)*2) ||
trunc(mod(小数部*power(2,4),1)*2) ||
trunc(mod(小数部*power(2,5),1)*2) ||
trunc(mod(小数部*power(2,6),1)*2) ||
trunc(mod(小数部*power(2,7),1)*2) as "2進数"
from (select Val,
trunc(Val) as 整数部,
mod(Val,1) as 小数部
from (select 0.5 as Val from dual
union all select 0 from dual
union all select 0.125 from dual
union all select 0.375 from dual
union all select 0.25 from dual
union all select 8.25 from dual
union all select 16.25 from dual
union all select 32.1 from dual));