Ruby’de girilen yazı veya sayı değerini binary (iki tabanında (01) ) olarak yazan minik programcık parçası.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#encoding=utf-8 =begin ## Integer değerler için itobinary fonksiyonu kullanılabilir def itobinary(sayi) i = 1 num = sayi.to_i stsonuc = "" while i<=num do sonuc = num%2 stsonuc = stsonuc +sonuc.to_s num = num/2 end stsonuc.reverse end =end def stobinary(yaz) genel = "" yaz.split("").each do |i| a=1 num=i.ord stsonuc="" while a<=num do sonuc= num%2 stsonuc=stsonuc+sonuc.to_s num = num/2 end stsonuc=stsonuc.reverse genel=genel+stsonuc end genel end print "Binary'ye çevrilecek değeri giriniz: " gelen=gets.chomp puts stobinary(gelen) |
Bir cevap yazın