Sabtu, 21 Maret 2020

SMALL BASIC: decimal to binary, decimal to octal, decimal to hexadecimal conversion program

Hello Small Basic Programmer, Welcome.

In this tutorial, we will discuss conversion between Decimal to other system like binary, octal and hexadecimal.

So in this case we essentially discuss about:
1. Decimal to binary conversion program.
2. Decimal to octal conversion program.
3. Decimal to hexadecimal conversion program.
4. The final program that conclude all the numbering conversion system from decimal to 2 numbering system until decimal to 9 numbering system, plus numbering system 16 conversion.

Few facts that you need to know
1. You know that decimal to octal conversion program and decimal to hexadecimal conversion program is a slight modification of decimal to binary conversion program.
2. The array is already first in first in.
3. Only decimal to hexadecimal that need a little bit adding a code; converting from 10 to 15 digits as A to F characters, we called this addtional code.
4.


In this tutorial, we will use division method of conversion. There are more than one method of how to convert decimal to other numbering system.

1. Decimal to binary conversion program
So, here is the code for decimal to binary conversion.

decimal = 100
i = 0
binary[i] = Math.Remainder(decimal, 2)
TextWindow.WriteLine(binary[i])
decimal = Math.Floor(decimal / 2)
While decimal > 1
  i = i + 1
  binary[i] = math.Remainder(decimal, 2)
  decimal = math.Floor(decimal / 2)
  TextWindow.WriteLine(binary[i])
EndWhile
i = i + 1
binary[i] = 1

textwindow.WriteLine("This is item length: " + Array.GetItemCount(binary))
For i = 0 To Array.getitemcount(binary) - 1
  TextWindow.WriteLine("This is binary " + i + ": "+ binary[i])
EndFor

2. Decimal to octal conversion program.
This the decimal to octal conversion program.

decimal_number = 8
i = 0
octal[i] = Math.Remainder(decimal_number, 8)
'TextWindow.WriteLine(binary[i])
decimal = Math.Floor(decimal_number / 8)
TextWindow.WriteLine("This is decimal: " + decimal)
While decimal > 7
  i = i + 1
  octal[i] = math.Remainder(decimal_number, 8)
  decimal = math.Floor(decimal_number / 8)
  'TextWindow.WriteLine(binary[i])
EndWhile
i = i + 1
TextWindow.WriteLine("This is decimal: " + decimal)
octal[i] = decimal

textwindow.WriteLine("This is item length: " + Array.GetItemCount(octal))
For i = 0 To Array.getitemcount(octal) - 1
  TextWindow.WriteLine("This is the octal " + i + ": "+ octal[i])
EndFor


3. Decimal to hexadecimal conversion program.
This is decimal to hexadecimal conversion program.

decimal_to_convert = 13

i = 0

hexa[i] = Math.Remainder(decimal_to_convert, 16)
TextWindow.WriteLine(hexa[i])
decimal_to_convert = Math.Floor(decimal_to_convert / 16)
While decimal_to_convert > 15
  i = i + 1
  hexa[i] = math.Remainder(decimal_to_convert, 16)
  decimal_to_convert = math.Floor(decimal_to_convert / 16)
  TextWindow.WriteLine(hexa[i])
EndWhile
i = i + 1
hexa[i] = decimal_to_convert

For i = 0 To Array.GetItemCount(hexa)
  TextWindow.WriteLine("This is the item of : " + i + " " + hexa[i])
EndFor

'This addtional program that I mention.
For i = 0 To Array.GetItemCount(hexa) - 1
  TextWindow.WriteLine("inside : " + hexa[i])
  If hexa[i] > 9 Then
    If hexa[i] = 10 Then
      hexa[i] = "A"
      ElseIf hexa[i] = 11 Then
        hexa[i] = "B"
        ElseIf hexa[i] = 12 Then
          hexa[i] = "C"
          ElseIf hexa[i] = 13 Then
            TextWindow.WriteLine("Hit here")
            hexa[i] = "D"
            ElseIf hexa[i] = 14 Then
              hexa[i] = "E"
              ElseIf hexa[i] = 15 Then
                hexa[i] = "F"
                EndIf
              EndIf
           
EndFor
             
textwindow.WriteLine("This is item length: " + Array.GetItemCount(hexa))
For i = 0 To Array.getitemcount(hexa) - 1
  TextWindow.WriteLine("This is the hexa " + i + ": "+ hexa[i])
EndFor


4. The final program

TextWindow.WriteLine("1. Enter the decimal number that you need to convert: ")
decimal_to_convert = TextWindow.Read()
TextWindow.WriteLine("2. Enter the base number that you need to convert (1-9),16?")
base = TextWindow.Read()

i = 0

digit[i] = Math.Remainder(decimal_to_convert, base)
decimal_to_convert = Math.Floor(decimal_to_convert / base)
While decimal_to_convert > base - 1
  i = i + 1
  digit[i] = math.Remainder(decimal_to_convert, base)
  decimal_to_convert = math.Floor(decimal_to_convert / base)
EndWhile
i = i + 1
digit[i] = decimal_to_convert

If base = 16 Then
  For i = 0 To Array.GetItemCount(digit) - 1
    TextWindow.WriteLine("inside : " + digit[i])
    If digit[i] > 9 Then
      If digit[i] = 10 Then
        digit[i] = "A"
        ElseIf digit[i] = 11 Then
          digit[i] = "B"
          ElseIf digit[i] = 12 Then
            digit[i] = "C"
            ElseIf digit[i] = 13 Then
              digit[i] = "D"
              ElseIf digit[i] = 14 Then
                digit[i] = "E"
                ElseIf digit[i] = 15 Then
                  digit[i] = "F"
                  EndIf
                EndIf         
              EndFor
            EndIf
         

For i = 0 To Array.getitemcount(digit) - 1
  TextWindow.WriteLine("This is the digit " + i + " of the converted: "+ digit[i])
EndFor










  

Tidak ada komentar:

Posting Komentar