tbf/DataMatrix4Net/Utils.cs

57 lines
1.4 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
namespace DataMatrix4Net
{
public static class Utils
{
const byte EndOfMessage = (byte)129;
public static bool IsDigit(char znak)
{
if (znak >= '0' && znak <= '9') return true;
return false;
}
public static bool IsAscii(char znak)
{
if (znak >= 0 && znak <= '~' + 1) return true;
return false;
}
public static byte EncodeTwoDigits(char znak1, char znak2)
{
return (byte)(130 + 10 * (znak1 - '0') + znak2 - '0');
}
public static byte[] MakeByteArray(string text)
{
IList<byte> bytes = new List<byte>();
int ix = 0;
while (ix < text.Length)
{
if (ix < text.Length - 1 && Utils.IsDigit(text[ix]) && Utils.IsDigit(text[ix + 1]))
{
bytes.Add(Utils.EncodeTwoDigits(text[ix], text[ix + 1]));
ix += 2;
}
else if (Utils.IsAscii(text[ix]))
{
bytes.Add((byte)(text[ix] + 1));
ix++;
}
else
{
/// Skip non ASCII characters
ix++;
}
}
bytes.Add(EndOfMessage);
return bytes.ToArray();
}
}
}