예제 #1
0
        /// <summary>
        /// Runtime implementation for VBA.Strings.StrConv VERSION 2
        /// note:
        ///     If Conversion == vbUnicode then the string returned will be encoded using
        ///     System.Runtime.InteropServices.Marshal.StringToHGlobalUni.
        /// </summary>
        /// <param name="str">Byte array representing an string.</param>
        /// <param name="Conversion">The type of the conversion to execute.</param>
        /// <param name="LocaleID">The LocaleID to use in the conversion.</param>
        /// <returns>The converted string.</returns>
        public static string StrConv2(string str, VbStrConvEnum Conversion, int LocaleID)
        {
            string res    = string.Empty;
            IntPtr strPtr = IntPtr.Zero;

            switch (Conversion)
            {
            //Please do not modify the implementations for vbFromUnicode and vbUnicode because they have been
            //already proveed with several systems (C995_045)
            case VbStrConvEnum.vbFromUnicode:
                strPtr = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(str);
                res    = System.Runtime.InteropServices.Marshal.PtrToStringUni(strPtr);
                System.Runtime.InteropServices.Marshal.FreeHGlobal(strPtr);
                break;

            case VbStrConvEnum.vbUnicode:
                strPtr = System.Runtime.InteropServices.Marshal.StringToHGlobalUni(str);
                res    = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(strPtr, str.Length * 2);
                System.Runtime.InteropServices.Marshal.FreeHGlobal(strPtr);
                break;

            default:
                res = Microsoft.VisualBasic.Strings.StrConv(str, (Microsoft.VisualBasic.VbStrConv)((int)Conversion), LocaleID);
                break;
            }

            return(res);
        }
예제 #2
0
        /// <summary>
        /// Runtime implementation for VBA.Strings.StrConv
        /// note:
        ///     If Conversion == vbUnicode then the string returned will be encoded using
        ///     System.Text.Encoding.Default, otherwise the encoding System.Text.Encoding.Unicode
        ///     will be used.
        /// </summary>
        /// <param name="str">Byte array representing an string.</param>
        /// <param name="Conversion">The type of the conversion to execute.</param>
        /// <param name="LocaleID">The LocaleID to use in the conversion.</param>
        /// <returns>The converted string.</returns>
        public static string StrConv(string str, VbStrConvEnum Conversion, int LocaleID)
        {
            string res    = string.Empty;
            IntPtr strPtr = IntPtr.Zero;

            byte[] b;

            switch (Conversion)
            {
            //Please do not modify the implementations for vbFromUnicode and vbUnicode because they have been
            //already proveed with several systems
            case VbStrConvEnum.vbFromUnicode:
                strPtr = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(str);
                res    = System.Runtime.InteropServices.Marshal.PtrToStringUni(strPtr);
                System.Runtime.InteropServices.Marshal.FreeHGlobal(strPtr);
                break;

            case VbStrConvEnum.vbUnicode:
                //It is also possible to use the specific encoding:
                //     - Encoding.GetEncoding("Windows-1252") or
                //     - Encoding.GetEncoding(1252)
                b   = System.Text.Encoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.Unicode, System.Text.Encoding.Unicode.GetBytes(str));
                res = System.Text.Encoding.Unicode.GetString(b);
                break;

            default:
                res = Microsoft.VisualBasic.Strings.StrConv(str, (Microsoft.VisualBasic.VbStrConv)((int)Conversion), LocaleID);
                break;
            }

            return(res);
        }
예제 #3
0
        /// <summary>
        /// Runtime implementation for VBA.Strings.StrConv
        /// note:
        ///     If Conversion == vbUnicode then the string returned will be encoded using
        ///     System.Text.Encoding.Default, otherwise the encoding System.Text.Encoding.Unicode
        ///     will be used.
        /// </summary>
        /// <param name="str">Byte array representing an string.</param>
        /// <param name="conversion">The type of the conversion to execute.</param>
        /// <param name="localeId">The LocaleID to use in the conversion.</param>
        /// <returns>The converted string.</returns>
        public static string StrConv(string str, VbStrConvEnum conversion, int localeId, bool unicode)
        {
            string res;
            IntPtr strPtr;

            switch (conversion)
            {
            case VbStrConvEnum.VbFromUnicode:
                if (unicode)
                {
                    byte[] bytesDefEnc = Encoding.Default.GetBytes(str);

                    if (bytesDefEnc.Length % 2 == 1)
                    {
                        byte[] newByteArray = new byte[bytesDefEnc.Length + 1];
                        bytesDefEnc.CopyTo(newByteArray, 0);
                        newByteArray[newByteArray.Length - 1] = 0;
                        res = Encoding.Unicode.GetString(newByteArray);
                    }
                    else
                    {
                        res = Encoding.Unicode.GetString(bytesDefEnc);
                    }
                    break;
                }
                else
                {
                    strPtr = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(str);
                    res    = System.Runtime.InteropServices.Marshal.PtrToStringUni(strPtr);
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(strPtr);
                    break;
                }

            case VbStrConvEnum.VbUnicode:
                if (unicode)
                {
                    res = Encoding.Default.GetString(Encoding.Unicode.GetBytes(str));
                    break;
                }
                else
                {
                    strPtr = System.Runtime.InteropServices.Marshal.StringToHGlobalUni(str);
                    res    = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(strPtr, str.Length * 2);
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(strPtr);
                    break;
                }

            default:
#if NETSTANDARD2_0
                throw new NotSupportedException("The method Microsoft.VisualBasic.Strings.StrConv is not part of the .Net Standard");
#else
                res = Microsoft.VisualBasic.Strings.StrConv(str, (Microsoft.VisualBasic.VbStrConv)((int)conversion), localeId);
                break;
#endif
            }

            return(res);
        }
예제 #4
0
 /// <summary>
 /// Runtime implementation for VBA.Strings.StrConv
 /// note:
 ///     If Conversion == vbUnicode then the string returned will be encoded using
 ///     System.Text.Encoding.Default, otherwise the encoding System.Text.Encoding.Unicode
 ///     will be used.
 /// </summary>
 /// <param name="str">Byte array representing an string.</param>
 /// <param name="Conversion">The type of the conversion to execute.</param>
 /// <returns>The converted string.</returns>
 public static string StrConv(string str, VbStrConvEnum Conversion)
 {
     //0 is to indicate to use the default ANSI encode of the machine
     return(StrConv(str, Conversion, 0));
 }
예제 #5
0
        /// <summary>
        /// Runtime implementation for VBA.Strings.StrConv VERSION 2
        /// note:
        ///     If Conversion == vbUnicode then the string returned will be encoded using
        ///     System.Runtime.InteropServices.Marshal.StringToHGlobalUni.
        /// </summary>
        /// <param name="str">Byte array representing an string.</param>
        /// <param name="Conversion">The type of the conversion to execute.</param>
        /// <param name="LocaleID">The LocaleID to use in the conversion.</param>
        /// <returns>The converted string.</returns>
        public static string StrConv2(string str, VbStrConvEnum Conversion, int LocaleID)
        {
            string res = string.Empty;
            IntPtr strPtr = IntPtr.Zero;

            switch (Conversion)
            {
                //Please do not modify the implementations for vbFromUnicode and vbUnicode because they have been
                //already proveed with several systems (C995_045)
                case VbStrConvEnum.vbFromUnicode:
                    strPtr = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(str);
                    res = System.Runtime.InteropServices.Marshal.PtrToStringUni(strPtr);
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(strPtr);
                    break;
                case VbStrConvEnum.vbUnicode:
                    strPtr = System.Runtime.InteropServices.Marshal.StringToHGlobalUni(str);
                    res = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(strPtr, str.Length * 2);
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(strPtr);
                    break;
                default:
                    res = Microsoft.VisualBasic.Strings.StrConv(str, (Microsoft.VisualBasic.VbStrConv)((int)Conversion), LocaleID);
                    break;
            }

            return res;
        }
예제 #6
0
 /// <summary>
 /// Runtime implementation for VBA.Strings.StrConv
 /// note:
 ///     If Conversion == vbUnicode then the string returned will be encoded using
 ///     System.Runtime.InteropServices.Marshal.StringToHGlobalUni.
 /// </summary>
 /// <param name="str">Byte array representing an string.</param>
 /// <param name="Conversion">The type of the conversion to execute.</param>
 /// <returns>The converted string.</returns>
 public static string StrConv2(string str, VbStrConvEnum Conversion)
 {
     //0 is to indicate to use the default ANSI encode of the machine
     return StrConv2(str, Conversion, 0);
 }
예제 #7
0
        /// <summary>
        /// Runtime implementation for VBA.Strings.StrConv
        /// note:
        ///     If Conversion == vbUnicode then the string returned will be encoded using
        ///     System.Text.Encoding.Default, otherwise the encoding System.Text.Encoding.Unicode
        ///     will be used.
        /// </summary>
        /// <param name="str">Byte array representing an string.</param>
        /// <param name="Conversion">The type of the conversion to execute.</param>
        /// <param name="LocaleID">The LocaleID to use in the conversion.</param>
        /// <returns>The converted string.</returns>
        public static string StrConv(string str, VbStrConvEnum Conversion, int LocaleID)
        {
            string res = string.Empty;
            IntPtr strPtr = IntPtr.Zero;
            byte[] b;

            switch (Conversion)
            {
                //Please do not modify the implementations for vbFromUnicode and vbUnicode because they have been
                //already proveed with several systems
                case VbStrConvEnum.vbFromUnicode:
                    strPtr = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(str);
                    res = System.Runtime.InteropServices.Marshal.PtrToStringUni(strPtr);
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(strPtr);
                    break;
                case VbStrConvEnum.vbUnicode:
                    //It is also possible to use the specific encoding:
                    //     - Encoding.GetEncoding("Windows-1252") or
                    //     - Encoding.GetEncoding(1252)
                    b = System.Text.Encoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.Unicode, System.Text.Encoding.Unicode.GetBytes(str));
                    res = System.Text.Encoding.Unicode.GetString(b);
                    break;
                default:
                    res = Microsoft.VisualBasic.Strings.StrConv(str, (Microsoft.VisualBasic.VbStrConv)((int)Conversion), LocaleID);
                    break;
            }

            return res;
        }
예제 #8
0
 public static string StrConv(string str, VbStrConvEnum conversion, bool unicode)
 {
     //0 is to indicate to use the default ANSI encode of the machine
     return(StrConv(str, conversion, 0, unicode));
 }
예제 #9
0
 public static string StrConv(string str, VbStrConvEnum conversion, int localeId)
 {
     //0 is to indicate to use the default ANSI encode of the machine
     return(StrConv(str, conversion, localeId, true));
 }