Petscii Decoder
//-----------------------------------------------------------------------
// <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 };
}
}
}
}
Related examples in the same category