Maple - convert string to int.

Posted on 2009-04-20
Last Modified: 2012-05-06

How can I convert a list of strings into a list of integers in maple?

I have an alphabet:


and a message:

msg := "I DO NOT LIKE MAPLE ";

I break the message into a list of substrings of size 2:

str := StringTools[LengthSplit](msg,2);

 str := "I ", "DO", " N", "OT", " L", "IK", "E ", "MA", "PL", "E "

How can I convert each substring into an integer where the following assignments would would hold:

_space = 00 A = 01 B = 02 C = 03 :: :: Z = 26

so I should end up with something like this:

[0800], [0415], ......... [0500]

that's a two digit number for each letter combined for two letter, a total of four digits.
Question by:ubuntuguy
    LVL 1

    Accepted Solution

    I need one clarification. What you expect if the input message contains a character that is outside of list of alphabet list.
    For example: If for above alphabet list, for input message "I LIKE MANGO, BANANA and APPLE."
    like comma(,) and full stop(.) are not there in the list of alphabets.
    LVL 1

    Author Comment

    Perfect input is assumed. So no error checking is necessary.
    LVL 1

    Author Closing Comment

    thanks i don't need the answer anymore.
    LVL 1

    Expert Comment

    Following steps can be followed to implement the code for this problem:-

    Step 1:
    First you need to create an array to store the corresponding integer value for a character in your alphabet. There are various ways. I think that on the basis of ascii value of that character you can create array. For example, for 'J' whose ascii value is 74, we can have CharCodeArray[74] = 10,  for 'A' we can have CharCodeArray[65] = 01 as ascii value of 'A' is 65 so on... Initially we have CharCodeArray containing zeros like CharCodeArray[i] = -1 for i = 0 to 99. We create array based on the list of the alphabets.

    Step 2:
    Then we read the next substring generated from input msg's characters and get the ascii value of each one. In our case its 2 character substring. say first character's ascii value is 73 (for character 'I') and second character's ascii value is 32 (for space).

    Step 3:
    Get the coded value for both the characters from CharCodeArray.

    Step 4:
    Multiply first character with 100 and add second to the result. And the print the output in [----] format specifying the '[' , ']' and 4 digits for the resultant value.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    This article will show, step by step, how to integrate R code into a R Sweave document
    Article by: Nicole
    This is a research brief on the potential colonization of humans on Mars.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now