冰楓論壇

標題: [C#] byte[]陣列與string字串轉換方式 [打印本頁]

作者: whitefox    時間: 2023-6-3 00:24
標題: [C#] byte[]陣列與string字串轉換方式
本帖最後由 whitefox 於 2023-6-3 00:29 編輯

這裡分享四種轉換方式
方法一
  1. string str = System.Text.Encoding.UTF8.GetString(decBytes);
  2. byte[] decBytes = System.Text.Encoding.UTF8.GetBytes(str);
複製代碼
以上UTF8可以換成ANSI或是直接用Default取代

方法二
  1. string str = BitConverter.ToString(decBytes);
  2. string[] tempArr = str.Split('-');
  3. string[] decBytes = new byte[tempArr.Length];
  4. for (int i = 0; i< tempArr.Length; i++)
  5. {
  6.     decByte[i] = Convert.ToByte(tempArr[i], 16);
  7. }
複製代碼
方法三
  1. string str = Convert.ToBase64String(decBytes);
  2. byte[] decBytes = Convert.FromBase64String(str);
複製代碼
方法四
  1. string str = HttpServerUtility.UrlTokenEncode(decBytes);
  2. byte[] decBytes = HttpServerUtility.UrlTokenDecode(str);
複製代碼





歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓