Strings.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities
  6. {
  7. /// <summary> General string utilities.</summary>
  8. public static class Strings
  9. {
  10. internal static bool IsOneOf(string s, params string[] candidates)
  11. {
  12. foreach (string candidate in candidates)
  13. {
  14. if (s == candidate)
  15. return true;
  16. }
  17. return false;
  18. }
  19. public static string FromByteArray(byte[] bs)
  20. {
  21. char[] cs = new char[bs.Length];
  22. for (int i = 0; i < cs.Length; ++i)
  23. {
  24. cs[i] = Convert.ToChar(bs[i]);
  25. }
  26. return new string(cs);
  27. }
  28. public static byte[] ToByteArray(char[] cs)
  29. {
  30. byte[] bs = new byte[cs.Length];
  31. for (int i = 0; i < bs.Length; ++i)
  32. {
  33. bs[i] = Convert.ToByte(cs[i]);
  34. }
  35. return bs;
  36. }
  37. public static byte[] ToByteArray(string s)
  38. {
  39. byte[] bs = new byte[s.Length];
  40. for (int i = 0; i < bs.Length; ++i)
  41. {
  42. bs[i] = Convert.ToByte(s[i]);
  43. }
  44. return bs;
  45. }
  46. public static string FromAsciiByteArray(byte[] bytes)
  47. {
  48. return Encoding.ASCII.GetString(bytes);
  49. }
  50. public static byte[] ToAsciiByteArray(char[] cs)
  51. {
  52. return Encoding.ASCII.GetBytes(cs);
  53. }
  54. public static byte[] ToAsciiByteArray(string s)
  55. {
  56. return Encoding.ASCII.GetBytes(s);
  57. }
  58. public static string FromUtf8ByteArray(byte[] bytes)
  59. {
  60. return Encoding.UTF8.GetString(bytes);
  61. }
  62. public static byte[] ToUtf8ByteArray(char[] cs)
  63. {
  64. return Encoding.UTF8.GetBytes(cs);
  65. }
  66. public static byte[] ToUtf8ByteArray(string s)
  67. {
  68. return Encoding.UTF8.GetBytes(s);
  69. }
  70. }
  71. }
  72. #pragma warning restore
  73. #endif