public void OneByteString() { ByteArrayAsString bas_1 = new ByteArrayAsString(1); ByteArrayAsString.StringFieldError err; err = bas_1.SetValue("ab"); Assert.AreEqual(err,ByteArrayAsString.StringFieldError.STRING_TOO_LONG); Assert.AreEqual(bas_1.MaxLength,1); Assert.AreEqual(bas_1.Length,0); Assert.AreEqual(bas_1.GetString,""); err = bas_1.SetValue(""); Assert.AreEqual(err,ByteArrayAsString.StringFieldError.SUCCESS); Assert.AreEqual(bas_1.MaxLength,1); Assert.AreEqual(bas_1.Length,0); Assert.AreEqual(bas_1.GetString,""); err = bas_1.SetValue("a"); Assert.AreEqual(err,ByteArrayAsString.StringFieldError.SUCCESS); Assert.AreEqual(bas_1.MaxLength,1); Assert.AreEqual(bas_1.Length,1); Assert.AreEqual(bas_1.GetString,"a"); err = bas_1.SetValue("ab"); Assert.AreEqual(err,ByteArrayAsString.StringFieldError.STRING_TOO_LONG); Assert.AreEqual(bas_1.MaxLength,1); Assert.AreEqual(bas_1.Length,1); Assert.AreEqual(bas_1.GetString,"a"); }
public void ThirtyByteString() { ByteArrayAsString bas_30 = new ByteArrayAsString(30); ByteArrayAsString.StringFieldError err; err = bas_30.SetValue("1234567890123456789012345678901"); Assert.AreEqual(err,ByteArrayAsString.StringFieldError.STRING_TOO_LONG); Assert.AreEqual(bas_30.MaxLength,30); Assert.AreEqual(bas_30.Length,0); Assert.AreEqual(bas_30.GetString,""); err = bas_30.SetValue(""); Assert.AreEqual(err,ByteArrayAsString.StringFieldError.SUCCESS); Assert.AreEqual(bas_30.MaxLength,30); Assert.AreEqual(bas_30.Length,0); Assert.AreEqual(bas_30.GetString,""); err = bas_30.SetValue("abcde"); Assert.AreEqual(err,ByteArrayAsString.StringFieldError.SUCCESS); Assert.AreEqual(bas_30.MaxLength,30); Assert.AreEqual(bas_30.Length,5); Assert.AreEqual(bas_30.GetString,"abcde"); err = bas_30.SetValue("123456789012345678901234567890"); Assert.AreEqual(err,ByteArrayAsString.StringFieldError.SUCCESS); Assert.AreEqual(bas_30.MaxLength,30); Assert.AreEqual(bas_30.Length,30); Assert.AreEqual(bas_30.GetString,"123456789012345678901234567890"); err = bas_30.SetValue("1234567890123456789012345678901"); Assert.AreEqual(err,ByteArrayAsString.StringFieldError.STRING_TOO_LONG); Assert.AreEqual(bas_30.MaxLength,30); Assert.AreEqual(bas_30.Length,30); Assert.AreEqual(bas_30.GetString,"123456789012345678901234567890"); }
static void Main() { /* byte[] somestring_as_bytes = new byte[10]; for( int offset = 0; offset < somestring_as_bytes.Length; offset++) { somestring_as_bytes[offset] = 0; } somestring_as_bytes[0] = (byte) 'X'; somestring_as_bytes[1] = (byte)'y'; somestring_as_bytes[2] = (byte)'z'; for (int offset = 0; offset < somestring_as_bytes.Length; offset++) { Console.Write("{0:X2} ",somestring_as_bytes[offset]); } Console.WriteLine(); // must scan for null and provide it to ASCIIEncoding.GetString.. since its // STUPID. int somestring_as_bytes_len = 0; for( int offset = 0; ( offset < somestring_as_bytes.Length && somestring_as_bytes[offset] != 0 ); offset++) { somestring_as_bytes_len++; } ASCIIEncoding a_encoding = new ASCIIEncoding(); string mystr = a_encoding.GetString(somestring_as_bytes,0,somestring_as_bytes_len); Console.WriteLine("mystr(" + mystr.Length + ")[" + mystr + "]"); // now.. back to a byte array byte [] re_encoded = a_encoding.GetBytes(mystr.ToCharArray()); Console.WriteLine("re_encoded(" + re_encoded.Length + ")"); for (int offset = 0; offset < re_encoded.Length; offset++) { Console.Write("{0:X2} ",re_encoded[offset]); } Console.WriteLine(); */ // test our string implementation.. ByteArrayAsString bas_1 = new ByteArrayAsString(1); ByteArrayAsString bas_10 = new ByteArrayAsString(10); ByteArrayAsString bas_30 = new ByteArrayAsString(30); Console.WriteLine("setvalue bas_1 to 'ab'"); Console.WriteLine(bas_1.SetValue("ab")); Console.WriteLine("bas_1.MaxLength:" + bas_1.MaxLength); Console.WriteLine("bas_1.Length:" + bas_1.Length); Console.WriteLine("bas_1.GetString(" + bas_1.GetString + ")"); Console.WriteLine("setvalue bas_1 to ''"); Console.WriteLine(bas_1.SetValue("")); Console.WriteLine("bas_1.MaxLength:" + bas_1.MaxLength); Console.WriteLine("bas_1.Length:" + bas_1.Length); Console.WriteLine("bas_1.GetString(" + bas_1.GetString + ")"); Console.WriteLine("setvalue bas_1 to 'a'"); Console.WriteLine(bas_1.SetValue("a")); Console.WriteLine("bas_1.MaxLength:" + bas_1.MaxLength); Console.WriteLine("bas_1.Length:" + bas_1.Length); Console.WriteLine("bas_1.GetString(" + bas_1.GetString + ")"); Console.WriteLine("setvalue to 'ab'"); Console.WriteLine(bas_1.SetValue("ab")); Console.WriteLine("bas_1.MaxLength:" + bas_1.MaxLength); Console.WriteLine("bas_1.Length:" + bas_1.Length); Console.WriteLine("bas_1.GetString(" + bas_1.GetString + ")"); Console.WriteLine("setvalue to '12345678901'"); Console.WriteLine(bas_10.SetValue("12345678901")); Console.WriteLine("bas_10.MaxLength:" + bas_10.MaxLength); Console.WriteLine("bas_10.Length:" + bas_10.Length); Console.WriteLine("bas_10.GetString(" + bas_10.GetString + ")"); Console.WriteLine("setvalue to ''"); Console.WriteLine(bas_10.SetValue("")); Console.WriteLine("bas_10.MaxLength:" + bas_10.MaxLength); Console.WriteLine("bas_10.Length:" + bas_10.Length); Console.WriteLine("bas_10.GetString(" + bas_10.GetString + ")"); Console.WriteLine("setvalue to 'abcde'"); Console.WriteLine(bas_10.SetValue("abcde")); Console.WriteLine("bas_10.MaxLength:" + bas_10.MaxLength); Console.WriteLine("bas_10.Length:" + bas_10.Length); Console.WriteLine("bas_10.GetString(" + bas_10.GetString + ")"); Console.WriteLine("setvalue to 'abcdefghij'"); Console.WriteLine(bas_10.SetValue("abcdefghij")); Console.WriteLine("bas_10.MaxLength:" + bas_10.MaxLength); Console.WriteLine("bas_10.Length:" + bas_10.Length); Console.WriteLine("bas_10.GetString(" + bas_10.GetString + ")"); Console.WriteLine("setvalue to '12345678901'"); Console.WriteLine(bas_10.SetValue("12345678901")); Console.WriteLine("bas_10.MaxLength:" + bas_10.MaxLength); Console.WriteLine("bas_10.Length:" + bas_10.Length); Console.WriteLine("bas_10.GetString(" + bas_10.GetString + ")"); Console.WriteLine("setvalue to '1234567890123456789012345678901'"); Console.WriteLine(bas_30.SetValue("1234567890123456789012345678901")); Console.WriteLine("bas_30.MaxLength:" + bas_30.MaxLength); Console.WriteLine("bas_30.Length:" + bas_30.Length); Console.WriteLine("bas_30.GetString(" + bas_30.GetString + ")"); Console.WriteLine(bas_30.SetValue("")); Console.WriteLine("bas_30.MaxLength:" + bas_30.MaxLength); Console.WriteLine("bas_30.Length:" + bas_30.Length); Console.WriteLine("bas_30.GetString(" + bas_30.GetString + ")"); Console.WriteLine("setvalue to 'abcde'"); Console.WriteLine(bas_30.SetValue("abcde")); Console.WriteLine("bas_30.MaxLength:" + bas_30.MaxLength); Console.WriteLine("bas_30.Length:" + bas_30.Length); Console.WriteLine("bas_30.GetString(" + bas_30.GetString + ")"); Console.WriteLine("setvalue to '12345678901'"); Console.WriteLine(bas_30.SetValue("12345678901")); Console.WriteLine("bas_30.MaxLength:" + bas_30.MaxLength); Console.WriteLine("bas_30.Length:" + bas_30.Length); Console.WriteLine("bas_30.GetString(" + bas_30.GetString + ")"); Console.WriteLine("done"); }