Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 String to binary
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zero-impact




PostPosted: Sat Oct 18, 2008 12:26 pm   Post subject: String to binary

Hi I'm making a program just for experimentation that takes a string and converts it into binary.
This is what I have so far.

Turing:

fcn strBin (input : string) : string
    var output : string := ""

    for i : 1 .. length (input)
        var dividend : int := ord (input (i))
        var quotient : int := dividend div 2
        var remainder : string := ""

        for k : 1 .. 8
            quotient := dividend div 2
            remainder := remainder + intstr (dividend mod 2) %binary digit
            dividend := quotient
        end for

        for decreasing k : 8 .. 1
            output := output + remainder (k)
        end for

    end for
    result output
end strBin

put strBin ("A B C D E F G")


Before I go on to make a function to decode the binary I would like to know if there is any easy way to get around the string length limit in Turing. And if there isn't any ideas on how to do it? It has never really bothered me before but with this function converting each character into eight characters it severely limits the string length.

This question also applies to another problem I'm working on Project Euler problem 25http://projecteuler.net/index.php?section=problems&id=25

The function lrgAdd is a function I made for an assignment in my ICS3M course.

Turing:

fcn lrgAdd (numa : string, numb : string) : string %Function adds two positive integers together no matter how large.
    var num1 := numa                               %Numbers must be passed as string the sum is returned as a string.
    var num2 := numb
    var sum : flexible array 1 .. 0 of char %An array to hold the working sum of the numbers.
    var rtnSum : string := "" %A string that will later hold the sum of the two numbers.
    var carry, smaller, larger, dif : int := 0 %carry is the value of the int carried over to the next column

    if length (num1) > length (num2) then %find the difference in length between the two numbers and pad the smaller number with 0's
        dif := length (num1) - length (num2)
        num2 := (repeat ("0", dif) + num2)
    else
        dif := length (num2) - length (num1)
        num1 := (repeat ("0", dif) + num1)
    end if

    new sum, length (num1) %+ 1 %make an array of chars 1 bigger than the numbers (in case of a carry on the last number)

    for i : 1 .. upper (sum)  %Make the array hold space chars
        sum (i) := ' '
    end for

    for decreasing i : length (num1) .. 1     %go through every number from right to left
        var digitTot : string     %The 2 digit number from the addition of the other 2 digits
        digitTot := intstr (strint (num1 (i)) + strint (num2 (i)) + carry)      %the total of the two digits and the carry

        if length (digitTot) > 1 then     %if the two numbers add up > 9 then make the carry 1
            carry := strint (digitTot (1))
            sum (i) := digitTot (2)     %if it is greater than 9 the sum in that column is the right number in digitTot
        else
            carry := 0      %else reset carry to 0 and sum (i) is the sum of the two digits
            sum (i) := digitTot
        end if
    end for

    if carry > 0 then      %if there is a carry on the last number add a 1 at the beginning of the number
        rtnSum := rtnSum + "1"
    end if

    for i : 1 .. upper (sum)
        rtnSum := rtnSum + sum (i)
    end for
    result rtnSum
end lrgAdd

var prev : string := "0"
var cur : string := "1"
var temp : string
var stream : int
open : stream, "lrgFib.txt", put
setscreen ("text")

loop
    temp := lrgAdd (cur, prev)
    prev := cur
    cur := temp
    put : stream, cur
    exit when length (cur) = 255
end loop

[/url]
Sponsor
Sponsor
Sponsor
sponsor
The_Bean




PostPosted: Sat Oct 18, 2008 2:59 pm   Post subject: Re: String to binary

Pass each letter through the function individually, and output

Turing:

var word : string := ("A B C D E F G")
for i : 1 .. length (word)
    put strBin (word (i))..
end for


or save to an array.

Turing:

var word : string := ("A B C D E F G")
var bin : array 1 .. length (word) of string
for i : 1 .. length (word)
    bin (i) := strBin (word (i))
end for
zero-impact




PostPosted: Sat Oct 18, 2008 3:39 pm   Post subject: Re: String to binary

Thanks!
Wow I can't believe I didn't think of something that simple, DOH!
Unfortunately that doesn't help me with the second problem Sad
The_Bean




PostPosted: Sat Oct 18, 2008 4:04 pm   Post subject: Re: String to binary

There is an easier way to convert to binary and then you just have to reverse it to decode.

I think I've solved Project Euler problem 25, my answer is F at 4782.
I used a complex array
var fib : array 1 .. 3, 1 .. 1000 of int
so fib(3,i)=fib(1,i)+fib(2,i)
the 1..1000 represents each column in the number, so
189283 would be
fib(1,1)=3 fib(1,2)=8 fib(1,3)=2 fib(1,4)=9 fib(1,5)=8 fib(1,6)=1
reading from right to left
if the sum is greater than 10 then add the corresponding amount to the next column over

Although my algorithm takes 22 - 25 seconds to solve the problem
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: