Coding

All things computer programming related...

Get top level subdirectories from a given directory

- Posted in Coding by

Here is a program demonstrating how to get the names of the top-level subdirectories in a given directory:

String manipulation in C#

- Posted in Coding by

Here is a method that, if passed a proper noun, such as a person's name or a street address, will return a properly formatted version of that string:

And here is an example of using method ProperlyCapitalizeName in a program:

Bytes to characters and vice versa

- Posted in Coding by

It just so happens that any character we might use in a string has a numerical encoding. The alphabet ranges from 65-90 for capital letters A through Z, and from 97-122 for lowercase letters a-z. When we want to work with text encoding, we need to add the following namespace to the top of our source code file:

using System.Text;

The numerical representation of our English alphabet takes the form of unique numbers for each upper and lowercase letter, and these are stored in a variable type called byte. A byte is an 8-bit integer that can hold values from 0 to 255.

If we have a string variable named sChar, we can obtain its numerical encoding as follows, if we have referenced the System.Text namespace:

byte[] bt = Encoding.Default.GetBytes(sChar);

The reverse is encoding bytes to characters. If we have an array of bytes, we can convert each element’s value to its corresponding character:

byte[] bytes = {40, 80, 20};
char result;
foreach(byte number in bytes)
{
  result = Convert.ToChar(number);
  Console.WriteLine(“{0} converts to {1}.”, number, result);
}

Full source code listing:

download sourcecode

Recursive file counting

- Posted in Coding by
using System;
using System.Reflection;
using System.IO;

namespace GetFilesRecursively {
class Program {

    static void Main(){
        int cnt = 0;
        long bites = 0;
        string p = string.Empty;
        foreach (string file in System.IO.Directory.GetFiles(
            GetExecutingDirectoryName(), "*",SearchOption.AllDirectories))
        {
            cnt++;
            FileInfo fi = new FileInfo(file);
            bites += fi.Length;
            //do something with file
            p = String.Format("{0:n0}", (int)bites);
        }   
        Console.Write(" Total files: {0}, ", cnt.ToString());
        Console.WriteLine("totaling {0} bytes.", p);
    }

    /* found following method here: 
    https://www.red-gate.com/simple-talk/blogs/c-getting-
    the-directory-of-a-running-executable/ */
    public static string GetExecutingDirectoryName()
    {
        var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);
        return new FileInfo(location.AbsolutePath).Directory.FullName;
    }       
  }
}

A php script to count Flatpress blog entries

- Posted in Coding by

I wrote a custom page in PHP to count the number of blog entries I have made, on a Flatpress self-hosted blog.

I've updated the script to work on my 2023 HTMLy blog, but the original script was for Flatpress.

We went tonight for revival meeting. Richie Coomer, a former middle school teacher of mine, was the preacher. Followed that upon getting home with three episodes of Money Heist.

Zip a passed directory using c-sharp

- Posted in Coding by

If passed a valid subdirectory, this 60-line C# console program will zip it to .zip file that is located in same directory as executing code.

Encrypt to HTML

- Posted in Coding by

I can't remember who put me onto this nifty javascript utility. It was someone on the donationcoder forum, but it's been a long time ago and cursory search didn't turn up the relevant post.

Anyway, encrypt-to-HTML is a real jewel written by Steve Clay, a Floridian web dev. You supply it plain text, either by typing or pasting into a textbox, or else from loading it from a file, and it produces an encrypted HTML file containing your protected content. When you open the produced HTML file in a browser, you're provided with a password textbox. You must enter the correct password and then your plain text will be decrypted and shown to you.

You can download this utility here. It's a 12.6 Kb download.

This small download is downloadable here on my website.

Copy contents from one folder to another

- Posted in Coding by

Introducing the CopyTo method, which faithfully copies one directory's contents to another directory. The method listing is on Github here.

And here is a sample program demonstrating its usage.

Convert images to Base64 strings and vice versa

- Posted in Coding by

I wrote a library, crypt.dll that permits conversion of images to Base64 strings, and vice versa. The source code can be downloaded from github

Numerical encoding of text using c-sharp

- Posted in Coding by

It just so happens that any character we might use in a string has a numerical encoding. The alphabet ranges from 65-90 for capital letters A through Z, and from 97-122 for lowercase letters a-z. When we want to work with text encoding, we need to add the following namespace to the top of our source code file:

using System.Text;

The numerical representation of our English alphabet takes the form of unique numbers for each upper and lowercase letter, and these are stored in a variable type called byte. A byte is an 8-bit integer that can hold values from 0 to 255.

If we have a string variable named sChar, we can obtain its numerical encoding as follows, if we have referenced the System.Text namespace:

byte[] bt = Encoding.Default.GetBytes(sChar);

The reverse is encoding bytes to characters. If we have an array of bytes, we can convert each element’s value to its corresponding character:

byte[] bytes = {40, 80, 20};
char result;
foreach(byte number in bytes)
{
    result = Convert.ToChar(number);
    Console.WriteLine(“{0} converts to {1}.”, number, result);
}

You can download a sample program here.

Page 7 of 10