[zeromq-dev] Implementing a ZAP handler in C#

Uli Riehm uli.riehm at metadea.de
Sun Aug 2 01:18:39 CEST 2015


Well the thing is to En/Decode this string into a string, or En/Decode 
this string into byte[]...

I'm adding methods

		public static byte[] ToZ85EncodedBytes(this string decoded)
		{
			return EncodeBytes(decoded, ZContext.Encoding);
		}

		public static byte[] ToZ85EncodedBytes(this string decoded, Encoding encoding)
		{
			return EncodeBytes(decoded, encoding);
		}

		public static byte[] ToZ85DecodedBytes(this string encoded)
		{
			return DecodeBytes(encoded, ZContext.Encoding);
		}

		public static byte[] ToZ85DecodedBytes(this string encoded, Encoding encoding)
		{
			return DecodeBytes(encoded, encoding);
		}

and finally also

  		public static byte[] EncodeBytes(string strg, Encoding encoding)
  		{
  			byte[] bytes = encoding.GetBytes(strg);
  			return Encode(bytes);
  		}

		public static byte[] DecodeBytes(string strg, Encoding encoding)
		{
			byte[] bytes = encoding.GetBytes(strg);
			return Decode(bytes);
		}


to have the byte[] and string thing correctly...

Gruß, Uli

Am 28.07.2015 um 16:24 schrieb Robert Pickering:
> Finally got to the bottom of this. It seems when you try and decode a
> Z80 encode key, represented using .NET's native string, class, the
> decode function decodes string to a byte array correctly, it then
> re-encodes these bytes as a string:
>
> 		public static string Decode(string strg)
> 		{
> 			return Decode(strg, ZContext.Encoding);
> 		}
>
> 		public static string Decode(string strg, Encoding
> 		encoding)
> 		{
> 			byte[] bytes = encoding.GetBytes(strg);
> 			byte[] encoded = Decode(bytes);
> 			return encoding.GetString(encoded);
> 		}
>
> This recoding always seems to corrupt the key in some way. The default
> encoding is UTF8, so presumably because the data is random this means
> some invalid byte sequences are generated. (Originally I was trying to
> decode the string as ASCII, which would mean any bytes > 128 would
> become corrupted, but even when I try decoding with UTF8, the key often
> still gets corrupted).
>
> I have changed all return types for decode methods in the clrzmq4
> library to return a byte array. The example now works as expected once
> this change is in place. I'll send a pull request over with the change.
>
> Rob





More information about the zeromq-dev mailing list