Capturing Windows Phone 8 Traffic Using Fiddler

Whenever I need to examine HTTP/HTTPS traffic, I use Fiddler. Fiddler can be a lifesaver when debugging problems, but can also be an extremely useful tool when you want to see what an application sending and receiving.

As I have had some spare time on my hands over the festive break (a rarity these days!!), I wanted to have a look at API calls from some apps on my Windows Phone. Fiddler should fit the bill for this. A few days ago, I upgraded my home machine to run Windows 8.1, so I needed to do a re-install of Fiddler. If you visit http://fiddler2.com/get-fiddler you can download the required build for you operating system. As I have Windows 8.1, I downloaded Fiddler4. Once you have downloaded Fiddler, follow the steps below to set up Fiddler and your phone for traffic capture.

Fiddler Settings

When you have run Fiddler, select the menu option Tools -> Fiddler Options and select the Connections tab. You will see the dialog box that is shown below:

Fiddler Remote Connection
You must now select the checkbox Allow remote computers to connect.

As we want to be able to view HTTPS traffic, we want to be able to decrypt this so we can view it. Click on the HTTPS tab. The tab looks as follows:

HTTPS

Select the checkbox Decrypt HTTPS traffic.

If you haven’t already restart Fiddler. Upon restarting Fiddler, the Windows Firewall warning may pop up.

Windows Phone Settings

We now need to perform some configuration tasks on the phone. Ensure your phone is connected to your WiFi network and perform the following steps.

In order for traffic to be captured by Fiddler, we need to set the WiFi proxy settings for the phones WiFi connection. Long tap on your WiFi connection within the WiFi settings on your phone. An example of this is shown below:

WiFi Settings

Select the edit option. You will then be presented with the following:

Enable Proxy
Swipe On to switch on the proxy functionality.

In the Server/URL setting, type in the IP Address of the computer where you installed Fiddler. You then need to specify the Port number. By default, Fiddler will use 8888. If you have changed this within the Fiddler settings, make sure you use this port number. Click the tick to save your settings.

We now need to download the Fiddler root certificate to the phone. Open Internet Explorer on your phone and key in the URL http://<<server>>:8888/fiddlerroot.cer. <<server>> will be the IP Address of the machine where you installed Fiddler. Using the settings from the example screenshots above, you would use http://192.168.1.78:8888/fiddlerroot.cer. Your phone will then show the following in Internet Explorer:

Fiddler cert
Tap install to install the Fiddler root certificate to your phone.

And that’s it!! As long as Fiddler is running and you have the proxy settings correctly configured on your phone, you should now be able to monitor the HTTP/HTTPS traffic from your Windows Phone.

Remember!

When you have finished monitoring traffic and you have shut down Fiddler, remember to switch of the proxy functionality on your phone. If you don’t do this, you will not be able to use the internet from your WiFi connection, as all traffic will be sent to the proxy, which obviously will not be running.

 

 

 

Serializing and Deserializing JSON in C#

The majority of my systems integration work requires me to work with 3rd party API’s or to create my own APIs. When creating my own, obviously I have full control over what the API call needs to be sent and what it will return and what shape its in. Pretty much all of my APIs work with XML data, its nice and easy to work with and the majority of people understand it at a glance.

More recently I was passed a set of 3rd party APIs that work with JSON (JavaScript Object Notation). For the purposes of this post, I am assuming that you know what JSON is and are comfortable with it. If you want to read more on JSON, the following link will take you over to Wikipedia where you can read more – http://en.wikipedia.org/wiki/JSON.

A very simple JSON string looks as follows:

{"forename" : "Phil", "surname" : "Curnow", "age" : 41}

We have a simple set of Name/Value pairs. In order to work with this data, we certainly don’t want to start writing code to parse the name/value pairs, its just not worth it. Ideally we would like to return this data in an object. To do this we can deserialize the JSON string. The following code snippet will explain how to do this. In order for this code to work, ensure that you have the following using’s at the top of your code:

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

The chances are, you will also need to add a reference to System.Runtime.Serialization in your Visual Studio project. With all of this done, you should now be able to work with the following code.

Creating a Person Class

As already mentioned, we want to deserialize the JSON string into an object. In order to do this, we obviously need to create a class, we will call this class Person. Pay particular attention to the attributes used within the class declaration and the properties.

[DataContract]
class Person
{
    [DataMember]
    public string forename { get; set; }
    [DataMember]
    public string surname { get; set; }
    [DataMember]
    public int age { get; set; }
}

You will see that our properties are named exactly the same as the name in the name/value pairs in the JSON string. We can then write the following code to deserialize the string.

string jsonString = @"{""forename"" : ""Phil"", ""surname"" : ""Curnow"", ""age"" : 41}";

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person)); 
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); 
Person obj = (Person)ser.ReadObject(stream);

Our deserialized string is now in the object obj (which is of the type Person). With this in mind, we can access the properties of the object by simply using obj.forename, obj.surname, etc. To make life a little easier, you could always set the object obj as follows:

var obj = (Person)ser.ReadObject(stream);

Working with an Array of JSON Objects

What do we do if we have more than one person defined in our JSON string, such as:

[{"forename" : "Phil", "surname" : "Curnow", "age" : 41},{"forename" : "Lorna", "surname" : "Curnow", "age" : 44}]

As it stands, our deserialization code will not work with this JSON string, we need a little tweaking. Basically, we need to return a list of People objects, so our code will now look as follows:

string jsonString = @"[{""forename"" : ""Phil"", ""surname"" : ""Curnow"", ""age"" : 41},{""forename"" : ""Lorna"", ""surname"" : ""Curnow"", ""age"" : 44}]";

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Person>)); 
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); 
var obj = (List<Person>)ser.ReadObject(stream);

Notice that we have now specified List<Person> in our code. We can then use the following to display the person data to the console:

foreach (Person p in obj)
    Console.WriteLine("Forename: {0} - Surname: {1}", p.forename, p.surname);

Now this is all very well, but do we really want to write that same block of code each time we want to work with a different data type? Well, probably not!! and this is where a quick conversion of the code to a generic method comes in handy. By simply creating the following generic method, we now have a piece of code that should work with any data type:

public static T DeserializeJSon<T>(string jsonString) 
{
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
    MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
    T obj = (T)ser.ReadObject(stream);
    return obj;
}

Rewriting the above deserialization now becomes as simple as this:

var obj = DeserializeJSon<Person>(jsonString);

or

var obj = DeserializeJSon<List<Person>>(jsonString);

A More Complex JSON String

Whilst I am not going to cover the multitude of different JSON strings you could work with, there is one more I want to show you. Consider the following string (formatted for easier reading):

{
    "forename":"Phil",
    "surname":"Curnow",
    "age":41,
    "address":
    {
        "line1":"21 High Street",
        "line2":"Anyplace, AnyTown, AN1 1AB"
    }
}

We now have an address included, which is made up of name/value pairs in its own right. So how do we deserialize this kind of string? Well, its actually very easy. All we have to do is create a new class for the address data and add a property of the type address to the Person class, as follows:

[DataContract]
class Address
{
    [DataMember]
    public string line1 { get; set; }
    [DataMember]
    public string line2 { get; set; }
}

Our Person class is then modified as follows:

[DataContract]
class Person
{
    [DataMember]
    public string forename { get; set; }
    [DataMember]
    public string surname { get; set; }
    [DataMember]
    public int age { get; set; }
    [DataMember]
    public Address address { get; set; }
}

The address is then handled correctly and the address lines placed in the address property.

Naming Class Properties Differently to JSON Names

You will more than likely want to name your class properties differently to their JSON counterparts. Consider the possibility of having the name/value pair current-account-balance:210.00. Personally I would not want a property in my class named current-account-balance!! To overcome this, we can slightly modify the [DataMember] attribute declaration to the following:

[DataMember (Name="current-account-balance")]
public decimal AccountBalance { get; set; }

This then defines a property in our class called AccountBalance that will contain the value of the current-account-balance.

Serializing Data to JSON

As you can guess, there is an opposite to deserializing, which is serializing. Consider the fact that we have created a Person object, or a list of Person objects and we want to convert this to a JSON string and return this from an API call. As with the deserialization, we need to write a generic method that will convert any type to its JSON equivalent. This method is as follows:

public static string SerializeJSon<T>(T t)
 {     
    MemoryStream stream = new MemoryStream();
    DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
    DataContractJsonSerializerSettings s = new DataContractJsonSerializerSettings();
    ds.WriteObject(stream, t);
    string jsonString = Encoding.UTF8.GetString(stream.ToArray());
    stream.Close();
    return jsonString;
}

If we then create a Person object as follows:

Person p = new Person
{
    forename = "Phil",
    surname = "Curnow",
    age = 41,
    address = new Address { line1 = "21 High Street", line2 = "Anyplace, AnyTown, AN1 1AB" }
};

We can simply convert it to a JSON string using the method call:

string jsonString = SerializeJSon<Person>(p);

Or with a list of Person objects, as follows:

List<Person> people = new List<Person>();
people.Add(new Person ...  );
people.Add(new Person ... );

string jsonString = SerializeJSon<List<Person>>(people);

Conclusion

As you can see, working with JSON data in C# is not at all difficult with the facilities built into the .NET Framework. If you are planning to work with JSON data to a larger extent, it is worth spending time investigating JSON further to become comfortable with looking at the data and being able to model it using classes.

String Search in Scratch

When I originally wrote the custom string blocks for Scratch, I posted a discussion on the ScratchEd site to let people know about them. I received a message a couple of weeks ago about adding a block that allows a string to be searched for a specific string (sub string). This is certainly a useful addition to the library of blocks and one I wanted to add when I had some free time.

Having found myself with the free time this morning, I have implemented a new custom block called FindString. FindString allows you to specify a string to be searched, a string to find and finally the search start character position within the string to be searched. The block is used as follows:

FindStringLooking at the example above, when executed, the block will search the string I like programming in Scratch for the string in starting at the character position 1 within our string to search. The block returns the start position of the string to find in a variable called startsAt. Executing the example will set startsAt to 16, as in starts at character position 16 in our string to search, if the string is not found, startsAt will be set to zero.

This block stops running after it has found the first occurrence of the string to search for. I will explain at the end how to search for more than one occurrence of a string.

How Does It Work?

So, how does the block work? Well, its actually very simple to follow. The image below shows the program code for the block.

Find String BlockWhen I first started writing the block, my first version used two loops (nested loops). The block worked fine, but after looking at the finished block, I realised that it could be re-written to use just one loop, making it a little easier to follow. So, lets break down the block above into logical steps to understand whats going on.

1. As we are doing a charcter by character comparison of the two strings, we need two variables to act as ‘pointers’ into our string to be searched and the string we are searching for. stringPos will hold the current character position in our string to be searched and subPos will hold the character position in the string we are searching for.

2. We initialise the startsAt variable to zero. Remember this variable holds the start position of our string we are searching for when the block has completed.

3. We now loop over the whole of the string to search for character by character, doing the following:

4. We check if the character at position stringPos in our string to be searched is the same as the character at position subPos in our string we are searching for. If they are, we first check if subPos is set to 1, if it is, we want to set startsAt to the value of stringPos, as this could possibly be the start location of our string to search for.

5. We then add 1 to both stringPos and subPos.

6. We next check to see if subPos (remember this is the character position in our string to search for) is equal to the length of our string to search for plus 1 character. If this is the case, we have actually found our string, so we can stop the block executing any further.

7. You may find what has been said in Step 6 confusing, but when we check if the characters from the two strings match in Step 4, if they don’t match, we set subPos back to 1 and add 1 to stringPos. We set subPos back to 1, as we want to continue searching the rest of the string should only a few of the characters of our string to search for has been found.

Its actually quite tricky to clearly explain in words what the block is doing, the best way to understand it is to look at it in Scratch, it really won’t be that hard to follow.

How Can I Search More Than Once Occurrence?

I did say at the beginning of this post that the block will only search for the first occurrence of a string, but what if you want to search for more than one occurrence of the same string? Well its very simple, all we need to do is keep calling the FindString block with a new start position. The easiest way is to do this in a loop and keep calling the block until startsAt equals zero. We will change the start position of each call to the FindString block by the position the last occurrence was found plus 1 character, this ensures we don’t find the same occurrence twice.

I have written a simple example below, that inserts all the positions of the occurrences in a list. The string we are going to search is “My cat likes to eat cat food. He is a happy cat” (I don’t have a cat by the way!). We will search for all the occurrences of the word cat. Shown below is the Scratch code to do this.

Occurrence CodeOnce this code has been run, the Occurances list will look as follows:

Occurrence ListAs you see, searching for multiple occurrences of string is quite a simple process. I hope you find the addition of this block useful and feel free to use it in any way you like.

Catching and Storing Exceptions in SSIS Script Tasks

I use SSIS (SQL Server Integration Services) very often to write data extraction and migration packages. More recently I have been using SSIS to write an interface to update a third party system through calling web services. The SSIS package runs as a scheduled task and queries a database table (call this the interface table) which holds records that have been updated in the source system, that also need updating in the third party system. By using the supplied web services, I am ensuring that all business logic is maintained when updating the third party system, I am also ensuring that no support agreements are invalidated as I am not performing direct updates to the database.

When the package runs, I need to maintain an audit trail of which records were successfully updated and which were not. There are 3 ways in which a record will not be updated, these are:

  1. The record from the source system did not exist in the third party system (This should be a rare occurrance as the source system is updated overnight from the third party system).
  2. Whilst programmatically the call to the web service was successful, the record could not be updated as the web service returned an unsuccessful response.
  3. An exception was thrown in the SSIS package.

Within the SSIS package, I am using a Script Task to perform all the calls to the web services and process the response. The first two items in the list above are handled fairly easily by examining the response coming back from the web service call and updating the status of the record in the interface table.

But what happens if there is an exception thrown in the Script Task in the SSIS package? Well, I would find out eventually that there was an issue, when the customer tells me there is and I have to go and start digging around to find out why the package failed. If I looked in the interface table and there was a status next to the record, I could inform them why the update did not occur. If exceptions are not handled properly, there will be no status information in the interface table saying that an exception has been thrown and I then have to start looking at the job history in SQL Server to find out when and why this happened.

So, how do we handle this in our Script Task? Catching exceptions is easy enough in code and originally I wrote the following code:

try
{
    .. code that could throw an exception ..
    Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception e)
{
    Dts.Events.FireError(-1, "Task Name", e.message, String.Empty, 0);
    Dts.TaskResult = (int)ScriptResults.Failure;  
}

Within the Error Event Handler in the package, I added a simple Execute SQL Task, that used the contents of the System::ErrorDescription variable to update the status message.  Forcing the Script Task to throw an exception (by changing the URL of the web service end-point), the exception information was written back to the interface table, unfortunately all I ended up with as a status was “error in script task” – not exactly useful!! I know there was an error, but I want to know what it was!!

After some further digging and a lot of research, I found a solution to the problem. To overcome the problem, you need to create a package variable that is going to hold the exception message and you need to set this in your catch block. Normally when we work with variables in a Script Task we have to populate the ReadOnlyVariables and/or the ReadWriteVariables lists in the task configuration. For this solution to work, we do not add our package variable to the ReadWriteVariables list.

The code I have ended up with that works is as follows.

try
{
    .. program code that could throw and exception ..
    Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception e)
{
    Variables exceptionVariable = null;
    Dts.VariableDispenser.LockOneForWrite("User::ScriptException", 
                                          ref exceptionVariable);
    exceptionVariable["User::ScriptException"].Value = e.Message;
    exceptionVariable.Unlock();

    Dts.Events.FireError(-1, "Task Name", e.Message, String.Empty, 0);
    Dts.TaskResult = (int)ScriptResults.Failure;
}

Looking at the code above, we have a package variable called ScriptException (remember do not add this to the ReadWriteVariables list!), I then modified my Execute SQL Task for the Error Event Handler to work with the User::ScriptException package variable instead of the System::ErrorDescription variable. When forcing the code to throw an exception, looking at the interface table, the full exception message is now held.

Whilst I don’t want my code to throw exceptions and I always strive for my code to be exception neutral, should the code throw an exception, I can now at least start by looking at the interface table and gain a good understanding of where things went wrong.

 

More Scratch String Custom Blocks

On September 22nd I blogged about a set of custom string blocks I implemented for Scratch. These have been very well received and I am glad people are finding them useful. I did say that I was going to add some more custom blocks to manipulate strings, and I am pleased to say that I have now added four more blocks to the Scratch project. I have now implemented the following blocks:

LTRIM

LTRIM allows you to trim any leading spaces from a string (Left Trim). Using an example, consider the string ”     Scratch” (quotes used to show the spaces). You can pass this string into the LTRIM block and it will remove all the leading spaces, giving you the string “scratch”. You use the block as follows:

LTrim BlockThe trimmed string is stored in the variable result.

RTRIM

RTRIM allows you to trim any trailing spaces from a string (Right Trim). Again using an example where the string is in quotes, if you call the block with the string “Scratch    “ you will receive a string back with the trailing spaces removed. The block is used as follows:

RTrim BlockThe trimmed string is stored in the variable result.

TRIM

TRIM is a combination of LTRIM and RTRIM and will remove any leading and trailing spaces. If you look at the block in Scratch, it shows a good example of being able to reuse exisiting blocks. All TRIM does is call the LTRIM and RTRIM blocks. As an example, if you call the block with the string ”    Scratch    “ all of the leading and trailing spaces are removed. You use the block as follows:

Trim BlockAs with the previous two blocks, the trimmed string is returned in the variable result.

SPLIT

SPLIT is an extremely useful block and in the programming languages I use on a daily basis, it is implemented and I use it often. SPLIT allows you to split a string at a certain character. A simple example of this is splitting a sentence into individual strings. If we passed in the string “I like programming in Scratch” and tell it to split on spaces, the SPLIT block will split this into indiviudal strings as follows:

I
like
programming
in
Scratch

I very often have to write programs that work with comma seperated values (CSV). In my programs, I would read a line of data from a file that could look like “phil curnow,11/04/1972,41” – This just shows a simple line with my name, date of birth and age. Now, using my language of choice, which is C#, if I wanted to display each of these values on screen, I could write some program code that looks like this:

string str = “phil curnow,11/04/1972,41”;

foreach (string s in str.Split(‘,’))
    Console.WriteLine(s);

This would print each of the three items on a seperate line on the screen. All I am saying in the program is that I want to split the string wherever there is a comma.

So how can we implement this in Scratch? Well, this is where lists come in to play. If we pass a string into our custom block and tell it to split on a space, each of the ‘sub-strings’ can be added to a list, and this is exactly how I have implemented the block.

To call our custom block, we need to supply two parameters, the string we want to split and the character we want to split on. So, looking at the example below, we will pass in the string I like programming in Scratch and the second parameter, although looks blank, is a space.

Split BlockAll our sub-strings are added to a list called SplitList and once the block has been executed, SplitList looks as follows:

SplitListUsing my example of the CSV list, we could pass in the string phil curnow,11/04/1972,41 into the block as follows:

Split Block CSVAnd our SplitList list would then contain:

CSV Split ListAs you can see SPLIT is a very powerful and above all, useful block to have.

Do you want to see anymore string blocks?

We now have several very useful custom blocks to manipulate strings in our Scratch projects. Are there any other blocks you would like to see for manipulating strings? If you can think of any other blocks, leave a comment on here or drop me a tweet and time permitting, I will see if I can implement it for you.

 

How the Scratch Custom Upper and Lower case blocks work

Last week I wrote a post about the custom string handling blocks I have written for Scratch. These have been very well received and I have had quite a few positive tweets back about them. I’m glad people are finding them useful!

Two of the blocks I have written allow you to convert a string to either upper or lower case. Now Scratch 2 when dealing with strings or characters is case insensitive, by this I mean if you perform a comparison of A = a Scratch will evaluate these to be the same. To clarify this, lets look at the block of code below.

Character ComparisonIf you run this block, you will see that the variable result is set to 1. This is what I mean when I say case insensitive, we may consider that A is not the same as a. So what does this have to do with converting our string to upper or lower case? Well, I’ll cover this a little later and you will see how this case insensitivity can help us!

ASCII Codes

If I was asked to write a very basic case changing method (same as a block in Scratch) in say C# (which is my preferred language), I may well consider looking at ASCII codes for characters, this is ignoring the fact that there is already two methods available in the .NET Framework to convert between upper and lower case!!. What is ASCII? well, I’m not going into the depths of explaining this here, but if you look at the Wikipedia entry for it at http://en.wikipedia.org/wiki/ASCII this will give you a full overview of it. ASCII codes can be our friend here for converting between case, if you consider the ASCII code for A is 65 (decimal) and the ASCII code for a is 97 (decimal), you can see there is a difference in the code values of 32. This works for B/b and C/c, etc.

So a method to convert upper case to lower case could loop over all the characters in a string and check the ASCII value for it. If the value is between 65 and  90, (A to Z), we can consider it to be an upper case letter, if we then add 32 to the ASCII value, this will give us the lower case version. We want to range check the letter value to ensure we are working with an upper case letter, otherwise the resulting string would look a little strange.

The small C# method below is something that will convert upper to lower case. Now at this point any C# programmer looking at the method would probably pick it to pieces and to be honest I can too. If I was going to write a version for production code it would be vastly different to this. This method has been written to explain my points above and nothing more.

string ToLowerCase(string str)
{
    string result = String.Empty;

    for (int loop = 0; loop < str.Length; loop++)
    {
        if ((int)str[loop] >= 65 && (int)str[loop] <= 90)
            result += Convert.ToChar((int)str[loop] + 32);
        else
            result += str[loop];
    }

    return result;
}

Passing the string abcd&ABCD to this method would return the string abcd&abcd. Great! we have a working conversion routine. So lets go and implement this in Scratch.

Our Scratch Version

Right, so now we know we can work with the ASCII codes for letters, we just convert what we have above to the corresponding Scratch code, right? …. Well, no we don’t sadly. Scratch does not give us the ability to work with the ASCII codes for letters, so this method will not work. Great! so how can we do this then? Well, remember I explained at the beginning of this post that Scratch is case insensitive and it can be our friend, well this is where it becomes our best friend!

When I was implementing my string blocks, I really did think that case conversion had to be included, so with a little thought figured there must be a way of doing this. The trick with solving any problem like this is to have a look at what we have to work with in Scratch. Whilst the C# method is very nice, we have to throw most of it away and think again. My eureka moment came when I thought about lists. My thinking here when converting to lower case was as follows:

1. Have a list that contains all the lower case letters a through to z
2. Loop through every character in our string and see if it exists in our list
3. If it exists, use the character found in the list (which will be the lower case character)
4. If it is not found in the list, use the original character.

So, putting this together, I generated a list called LowerCase which looks like this:

Lowercase ListThis method will work even if we pass over lowercase letters, we will still get our character from the list, which will be lower case. So, my Scratch ToLowerCase block looks like this:

ToLowerCase BlockOur block starts by initialising a couple of variables. result will hold our converted string and charCount is used by the repeat block to loop through every character in our string. I have then implemented another custom block called GetLowerCaseLetter. Splitting out code like this into another block makes the code more readable and also gives us yet another block that allows us to do single character conversion. Our GetLowerCaseLetter block looks in our list and finds the lower case letter we need. After calling this block, our lower case letter is held in the variable letterResult so we simply add this to our result variable. We then add 1 to the character count and go around the loop again. This keeps going until we have worked with every character in our string.

The GetLowerCaseLetter Block

This is where most of the work is done. Lets have a look at this block.

GetLowerCaseLetter BlockAgain at the beginning we initialist a couple of variables. loop is used to loop through our LowerCase list. letterResult is used to return our converted character. We are then going to loop through the list a maximum of 26 times (remember a to z!!). When we are in the loop, we check if the letter we have passed to the block, e.g. A is in our list. So using a simple if block we are essentially saying if A = a then set letterResult to the item found in our list, which will be a. As we don’t want to go around the loop again, we stop this script. At this point letterResult holds a which is the lower case version of what we passed in. If we had passed in a it would have found this and letterResult would hold a.

If our block has got as far as the end of the loop, we have another if block that says if letterResult is empty, we must have passed in a character other than a letter of the alphabet, so just simply return it. This ensures anything like %&*() is still kept in the converted string.

So putting this to the test, if we use the block like this:

Call ToLowerCaseThe string we will get back is abcd&abcd, which is the lower case version of what we passed in!

Converting to Upper Case

Converting to upper case is exactly the same, we just use a list that contains upper case A to Z. If you look at the ToUpperCase block in the Scratch project, you will see it is exactly the same as its lower case version. The GetUpperCaseLetter block again is exactly the same as its lower case version.

So, again as you can see, whilst our Scratch version of converting to upper and lower case is very different to what we may do in other programming languages, if you sit back and think about the problem and consider what you have to work with in Scratch, there is generally always a way of implementing what you want to do.

Scratch Custom String Blocks

Having the ability to manipulate strings is one of the most powerful facilities you can have in a programming language. In my day to day coding I manipulate strings an awful lot. You may want to be able to get specific characters in a string, convert the whole string to uppercase characters or lowercase characters. Many years ago, one of the first programming languages I learnt was BASIC (http://en.wikipedia.org/wiki/BASIC). BASIC has several functions for allowing you to manipulate strings, for example:

LEFT$(string, n)

LEFT$ allows you to return the LEFT most n characters from your string. For example if you use the command PRINT LEFT$(“Scratch”,3) the letters Scr would be printed to the screen. The n parameter tells the function how many characters starting at the beginning of the string you want.

RIGHT$(string, n)

Similar to LEFT$, RIGHT$ allows you to return the RIGHT most n characters from your string. So something like PRINT RIGHT$(“Scratch”, 3) would print tch to the screen. The n parameter tells the function how many characters starting at the end of the string and working backwards you want.

MID$(string, start, n)

MID$ is quite a powerful function. It allows you to get a certain amount of characters, but starting at any point in the string. So using a command like PRINT MID$(“Scratch Programming”, 9, 7) would print Program to the screen. The start parameter tells the function where you want to start in the string and the n parameter tells the function how many characters you want to return from the starting point.

UPPER$ and LOWER$

Some other functions available are UPPER$ and LOWER$. These return either an uppercase or lowercase version of your string. So something like PRINT UPPER$(“scratch”) would print SCRATCH and PRINT LOWER$(“SCRATCH”) would print scratch. Again, very useful!

How can we do this in Scratch?

Out of the box, Scratch does not have these kind of blocks built in. As we know, Scratch 2 allows us to write our own custom blocks, so we could write our own versions of these. What I have done is build versions of the functions explained above as custom blocks that can be used in your Scratch programs.

I have made these available at http://scratch.mit.edu/projects/12402145/. If you look inside the project, all the custom blocks are defined on the stage, with an example of how you would use each block next to the block definition. I will given an explanation below on how you would use each custom block.

Please Note: The resulting string from each of these blocks is stored in a variable called ‘result’.

Scratch LEFT$

This has been implemented as a custom block called Left. You would use the block like this:

Scratch Left BlockThe first parameter is the string or the variable holding the string you want to work with. The second parameter is the number of characters from the left you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold Scr.

Scratch RIGHT$

This has been implemented as a custom block called Right. You would use the block like this:

Scratch Right BlockThe first parameter is the string or variable holding the string you want to work with. The second parameter is the number of characters from the right that you want. The result is stored in a variable called result. Looking at the example above, after calling this block, the result variable would hold tch.

Scratch MID$

This has been implemented as a custom block called Mid. You would use the block like this:

Scratch Mid BlockWe have three parameters that we have to supply to this block. The first parameter is the string or variable holding the string you want to work with. The second parameter is where you want to start in the string. The final parameter is the number of characters you want to get from the string. The result is stored in a variable called result. So, looking at the example call above, we can see that we will be working with the string Scratch Programming and we want to start returning characters from the 9th character in the string and that we want to return 7 characters. Starting at the 9th character (P) we will get 7 characters, which will give us the string Program.

Scratch UPPER$ and LOWER$

I have also implemented blocks that allow strings to be converted to upper of lower case. These blocks are called ToUpperCase and ToLowerCase. As with all the other blocks, the result is stored in a variabled called result. You would use the blocks like this:

Scratch ToUpperCase BlockScratch ToLowerCase BlockCalling the ToUpperCase block on the string shown would convert it to SCRATCH and calling the ToLowerCase block on the string shown would convert it to scratch. If you supplied a string such as ScRaTcH to either of these blocks, it will still work as expected.

I am going to write a further blog post on how the ToUpperCase and ToLowerCase blocks work, because Scratch is a little different to most languages when comparing upper and lower case characters. The code for the other blocks is fairly easy to follow and figure out whats going on.

Anymore Blocks?

I will be implementing more string manipulation blocks over a period of time. The blocks I want to implement next are:

  – SPLIT(string, char)
  – LTRIM(string)
  – RTRIM(string)
  – TRIM(string)

SPLIT enables you to split one string into several strings, splitting the string at a specific character. For example if you supplied a string I Program In Scratch and told the block to split on the space character, you would end up with 4 strings containing:

  I
  Program
  In
  Scratch

Each of these strings would be held in a list.

LTRIM and RTRIM removes all spaces to either the left or the right of a string. So if you had a string ”    Scratch” (quotes used to show the spaces) and passed the string to the LTRIM block, it would remove all the leading spaces. Similarly, if you had the string “Scratch   ” and passed the string to the RTRIM block, it would remove all the trailing spaces. TRIM performs a union of LTRIM and RTRIM and will remove all leading and trailing spaces.

As these blocks are added, I will add a post to this blog and will also add a comment in the Scratch forums and on the ScratchEd web site.

Use and Share!

I do hope that you find these blocks useful and feel free to use and modify them in any way you would like. Share them around with your fellow Scratchers.