Ignore:
Timestamp:
Aug 11, 2009, 9:37:27 PM (12 years ago)
Author:
Arno Wacker
Message:

This partially addresses #25

  • Added hex-string decoding and encoding to the Stream-to-String/String-to-Stream converter plug-ins.
  • Updated the converter sample to show Hex-string encoding.
Location:
trunk/CrypPlugins/StringToStreamConvertor
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/StringToStreamConvertor/StringToStreamConverter.cs

    r431 r432  
    401401                        outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
    402402                        break;
    403                     case StringToStreamConverterSettings.EncodingTypes.Binary:
     403                    case StringToStreamConverterSettings.EncodingTypes.Base64Binary:
    404404                        try
    405405                        {
     
    415415                        }
    416416                        break;
     417                    case StringToStreamConverterSettings.EncodingTypes.HexStringBinary:
     418                        try
     419                        {
     420                            outputBytes = convertStringToByteArray(value);
     421                            outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
     422                        }
     423                        catch (Exception ex)
     424                        {
     425                            ShowStatusBarMessage("Error converting input! Not a valid hex-string (" + ex.Message + ")", NotificationLevel.Error);
     426                            return;
     427                        }
     428                        break;
    417429                    case StringToStreamConverterSettings.EncodingTypes.Unicode:
    418430                        // outputStream = new CryptoolStream();
     
    468480        }
    469481
     482
     483        private byte[] convertStringToByteArray(String hexString)
     484        {
     485            if (null == hexString)
     486                return new byte[0];
     487           
     488            StringBuilder cleanHexString = new StringBuilder();
     489
     490            //cleanup the input
     491            foreach (char c in hexString)
     492            {
     493                if (Uri.IsHexDigit(c))
     494                    cleanHexString.Append(c);
     495            }
     496
     497            int numberChars = cleanHexString.Length;
     498
     499            if (numberChars < 2) // Need at least 2 chars to make one byte
     500                return new byte[0];
     501
     502            byte[] bytes = new byte[numberChars / 2];
     503
     504            for (int i = 0; i < numberChars; i += 2)
     505            {
     506                bytes[i / 2] = Convert.ToByte(cleanHexString.ToString().Substring(i, 2), 16);
     507            }
     508            return bytes;
     509        }
     510
     511
    470512        private void ShowStatusBarMessage(string message, NotificationLevel logLevel)
    471513        {
  • trunk/CrypPlugins/StringToStreamConvertor/StringToStreamConverterSettings.cs

    r4 r432  
    215215        #region Public StringToStreamConverter specific properties
    216216
    217         public enum EncodingTypes { Default = 0, Binary = 1, Unicode = 2, UTF7 = 3, UTF8 = 4, UTF32 = 5, ASCII = 6, BigEndianUnicode = 7 };
     217        public enum EncodingTypes { Default = 0, Base64Binary = 1, HexStringBinary = 2, Unicode = 3, UTF7 = 4, UTF8 = 5, UTF32 = 6, ASCII = 7, BigEndianUnicode = 8 };
    218218
    219219        /// <summary>
     
    249249        /// Encoding property used in the Settings pane.
    250250        /// </summary>
    251         [ContextMenu("Output stream encoding", "Choose the encoding of the output stream. (Byte encoding)", 1, DisplayLevel.Experienced, ContextMenuControlType.ComboBox, null, new string[] { "Default", "Binary data (convert from base64)", "Unicode", "UTF-7", "UTF-8", "UTF-32", "ASCII", "Big endian unicode" })]
    252         [TaskPane("Output stream encoding", "Choose the encoding of the output stream. (Byte encoding)", "", 1, false, DisplayLevel.Experienced, ControlType.ComboBox, new string[] { "Default", "Binary data (convert from base64)", "Unicode", "UTF-7", "UTF-8", "UTF-32", "ASCII", "Big endian unicode" })]
     251        [ContextMenu("Output stream encoding", "Choose the encoding of the output stream. (Byte encoding)", 1, DisplayLevel.Experienced, ContextMenuControlType.ComboBox, null, new string[] { "Default", "Binary data (convert from base64)", "Binary data (convert from hex-string)", "Unicode", "UTF-7", "UTF-8", "UTF-32", "ASCII", "Big endian unicode" })]
     252        [TaskPane("Output stream encoding", "Choose the encoding of the output stream. (Byte encoding)", "", 1, false, DisplayLevel.Experienced, ControlType.ComboBox, new string[] { "Default", "Binary data (convert from base64)", "Binary data (convert from hex-string)", "Unicode", "UTF-7", "UTF-8", "UTF-32", "ASCII", "Big endian unicode" })]
    253253        public int EncodingSetting
    254254        {
Note: See TracChangeset for help on using the changeset viewer.