extends System.Text.Encoding to create Petscii Encoding : Encoding « Internationalization I18N « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Internationalization I18N » Encoding 




extends System.Text.Encoding to create Petscii Encoding
  
//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="Payton Byrd">
//     Copyright (c) 2007 Payton Byrd.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace CBM_Commander.Common.Encoding
{
    public class Petscii : System.Text.Encoding
    {
        public static PetsciiEncoder GetPetsciiEncoder()
        {
            return new PetsciiEncoder();
        }

        public static PetsciiDecoder GetPetsciiDecoder()
        {
            return new PetsciiDecoder();
        }

        public override int GetByteCount(
            char[] chars,
            int index,
            int count)
        {
            return GetPetsciiEncoder().GetByteCount(
                chars, index, count, false);
        }

        public override int GetBytes(
            char[] chars,
            int charIndex,
            int charCount,
            byte[] bytes,
            int byteIndex)
        {
            return GetPetsciiEncoder().GetBytes(
                chars, charIndex, charCount,
                bytes, byteIndex, false);
        }

        public override int GetCharCount(
            byte[] bytes,
            int index,
            int count)
        {
            return GetPetsciiDecoder().GetCharCount(
                bytes, index, count);
        }

        public override int GetChars(
            byte[] bytes,
            int byteIndex,
            int byteCount,
            char[] chars,
            int charIndex)
        {
            return GetPetsciiDecoder().GetChars(
                bytes, byteIndex, byteCount,
                chars, charIndex);
        }

        public override int GetMaxByteCount(int charCount)
        {
            return charCount;
        }

        public override int GetMaxCharCount(int byteCount)
        {
            return byteCount;
        }
    }
}

//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="Payton Byrd">
//     Copyright (c) 2007 Payton Byrd.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace CBM_Commander.Common.Encoding
{
    public sealed class PetsciiDecoder : Decoder
    {
        private byte[] _newLine = null;
        private byte[] _space = null;

        internal PetsciiDecoder()
        {
            _newLine = ASCIIEncoding.ASCII.GetBytes("\n");
            _space = ASCIIEncoding.ASCII.GetBytes(" ");
        }

        public override int GetCharCount(
            byte[] bytes,
            int index,
            int count)
        {
            DecodeBytes(
                bytes, index,
                count, index);

            return count;

        }

        public override int GetChars(
            byte[] bytes,
            int byteIndex,
            int byteCount,
            char[] chars,
            int charIndex)
        {
            char[] decodedChars =
                DecodeBytes(
                    bytes, byteIndex,
                    byteCount, charIndex);

            for (int i = byteIndex;
                i < (byteIndex + byteCount);
                i++)
            {
                chars[charIndex + (i - byteIndex)] =
                    decodedChars[i];
            }

            return byteCount;
        }

        private char[] DecodeBytes(
            byte[] bytes,
            int byteIndex,
            int byteCount,
            int charIndex)
        {
            char[] results = null;
            List<byte> output = new List<byte>();
            byte[] translated = null;

            foreach (byte b in bytes)
            {
                output.AddRange(TranslateByte(b));
            }

            translated = output.ToArray();

            results =
                ASCIIEncoding.ASCII.GetChars(translated);

            return results;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Character"></param>
        /// <returns></returns>
        private byte[] TranslateByte(byte SourceByte)
        {
            switch (SourceByte & 0xff)
            {
                case 0x0a:
                case 0x0d:
                    return _newLine;
                case 0x40:
                case 0x60:
                    return new byte[] { SourceByte };
                case 0xa0:
                case 0xe0:
                    return _space;
                default:
                    switch (SourceByte & 0xe0)
                    {
                        case 0x40:
                        case 0x60:
                            return new byte[] { (byte)(SourceByte ^ (byte)0x20) };

                        case 0xc0:
                            return new byte[] { (byte)(SourceByte ^ (byte)0x80) };

                    }

                    return new byte[] { SourceByte };
            }
        }
    }
}

Petscii Encoder
//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="Payton Byrd">
//     Copyright (c) 2007 Payton Byrd.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace CBM_Commander.Common.Encoding
{
    public sealed class PetsciiEncoder : Encoder
    {
        private byte[] _newLine = null;
        private byte[] _space = null;
        private Dictionary<char, byte>
            _ascii = new Dictionary<char, byte>();

        internal PetsciiEncoder()
        {
            _newLine = ASCIIEncoding.ASCII.GetBytes("\n");
            _space = ASCIIEncoding.ASCII.GetBytes(" ");

            for (byte i = 0x00; i < 0xFF; i++)
            {
                char c = (char)i;
                _ascii.Add(c,
                    ASCIIEncoding.ASCII.GetBytes(
                        new char[] { })[0]);
            }
        }

        public override int GetByteCount(
            char[] chars,
            int index,
            int count,
            bool flush)
        {
            List<byte> targetBytes = new List<byte>();

            byte[] sourceBytes =
                ASCIIEncoding.ASCII.GetBytes(chars);

            for (int i = index; i < (index + count); i++)
            {
                targetBytes.Add(
                    TranslateCharacter(sourceBytes[i]));
            }

            return targetBytes.Count;
        }

        public override int GetBytes(
            char[] chars,
            int charIndex,
            int charCount,
            byte[] bytes,
            int byteIndex,
            bool flush)
        {
            List<byte> targetBytes = new List<byte>();

            byte[] sourceBytes =
                ASCIIEncoding.ASCII.GetBytes(chars);

            foreach (byte b in sourceBytes)
            {
                targetBytes.Add(TranslateCharacter(b));
            }

            for (
                int i = charIndex;
                i < (charIndex + charCount);
                i++)
            {
                bytes[byteIndex + (i - charIndex)] =
                    targetBytes[i];
            }

            return charCount;
        }

        private byte TranslateCharacter(byte Character)
        {
            if (
                Character >= 0x5B &&
                Character <= 0x7E)
            {
                return (byte)(Character ^ 0x20);
            }
            else if (
                Character >= _ascii['A'&&
                Character <= _ascii['Z'])
            {
                return (byte)(Character | 0x80);
            }
            else if (Character == _newLine[0])
            {
                return 0x0D;
            }

            return Character;
        }
    }
}

   
    
  














Related examples in the same category
1.Encoding Class represents a character encoding.
2.Encoding.ASCII encoding for the ASCII (7-bit) character set.
3.Gets an encoding for the UTF-16 format that uses the big endian byte order.
4.Read a text file saved with Big Endian Unicode encoding
5.Get byte count and max byte count
6.Display only the encodings that have one or more different names
7.Returns an array that contains all encodings.
8.Returns a sequence of bytes that specifies the encoding used.
9.Gets an encoding for the UTF-32 format using the little endian byte order.
10.Gets an encoding for the UTF-8 format.
11.Gets the Windows operating system code page that most closely corresponds to the current encoding.
12.Petscii Decoder
13.Petscii Encoder
14.SignedLoWord from IntPtr
15.SignedHiWord from IntPtr
16.Pad data to a WORD.
17.Returns the high WORD from a DWORD value.
18.Signed Lo Word
19.Signed Hi Word
20.Flip Endian
21.A Big-endian binary writer.
22.Big-endian binary reader
23.EBCDIC To ASCII
24.Is Chinese Character
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.