Click here to Skip to main content
15,884,001 members
Articles / Programming Languages / C#

Neural Network for Recognition of Handwritten Digits in C#

Rate me:
Please Sign up or sign in to vote.
4.93/5 (89 votes)
14 Mar 2012MIT9 min read 308.2K   48K   217  
This article is an example of an artificial neural network designed to recognize handwritten digits.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ArchiveSerialization
{
	static public class MFCStringReader
	{
		static public string ReadCString(BinaryReader reader)
		{
			string str = "";
			int nConvert = 1; // if we get ANSI, convert

			UInt32 nNewLen = ReadStringLength(reader);
			if (nNewLen == unchecked((UInt32)(-1)))
			{
				nConvert = 1 - nConvert;
				nNewLen = ReadStringLength(reader);
				if (nNewLen == unchecked((UInt32)(-1)))
					return str;
			}

			// set length of string to new length
			UInt32 nByteLen = nNewLen;
			nByteLen += (UInt32)(nByteLen * (1 - nConvert)); // bytes to read

			// read in the characters
			if (nNewLen != 0)
			{
				// read new data
				byte[] byteBuf = reader.ReadBytes((int)nByteLen);

				// convert the data if as necessary
				StringBuilder sb = new StringBuilder();
				if (nConvert != 0)
				{
					for (int i = 0; i < nNewLen; i++)
						sb.Append((char)byteBuf[i]);
				}
				else
				{
					for (int i = 0; i < nNewLen; i++)
						sb.Append((char)(byteBuf[i * 2] + byteBuf[i * 2 + 1] * 256));
				}

				str = sb.ToString();
			}

			return str;
		}

		static private UInt32 ReadStringLength(BinaryReader reader)
		{
			UInt32 nNewLen;

			// attempt BYTE length first
			byte bLen = reader.ReadByte();

			if (bLen < 0xff)
				return bLen;

			// attempt WORD length
			UInt16 wLen = reader.ReadUInt16();
			if (wLen == 0xfffe)
			{
				// UNICODE string prefix (length will follow)
				return unchecked((UInt32)(-1));
			}
			else if (wLen == 0xffff)
			{
				// read DWORD of length
				nNewLen = reader.ReadUInt32();
				return nNewLen;
			}
			else
				return wLen;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Vietnam Maritime University
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions