• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1974
  • Last Modified:

Maple - convert string to int.

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.
  • 2
  • 2
1 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.
ubuntuguyAuthor Commented:
Perfect input is assumed. So no error checking is necessary.
ubuntuguyAuthor Commented:
thanks i don't need the answer anymore.
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.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now