首页 > 编程语言 >c# 避免代码中直接暴露字符串

c# 避免代码中直接暴露字符串

时间:2023-02-28 11:13:35浏览次数:38  
标签:string c# res 暴露 byteDatas var Length 字符串 new

        /// <summary>
        /// 避免代码中直接暴露字符串
        /// 将字符串转字节 
        /// </summary>
        /// <param name="byteDatas"></param>
        /// <returns></returns>
        public static string stringMakeBytesCode(this string txt)
        {
            var byteDatas = System.Text.Encoding.Unicode.GetBytes(txt);

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < byteDatas.Length; i++)
            {
                builder.Append(string.Format("0x{0:X2} ", byteDatas[i]));
            }

            var res = builder.ToString();
            //res = $"byte[] bts =new byte[{byteDatas.Length}] " + "{" + res.Replace(" ", ",") + "}";
            res = $"var s = System.Text.Encoding.Unicode.GetString( new byte[{byteDatas.Length}] " + "{" + res.Replace(" ", ",") + "});";
            return res;
        }

 

标签:string,c#,res,暴露,byteDatas,var,Length,字符串,new
From: https://www.cnblogs.com/wujiangling/p/17163278.html

相关文章