Strings
General string utilities.
using System;
using System.Text;
namespace Org.BouncyCastle.Utilities
{
public static class Strings
{
internal static void AppendFromByteArray(StringBuilder sb, byte[] buf, int off, int len)
{
sb.EnsureCapacity(sb.Length + len);
for (int i = 0; i < len; i++) {
sb.Append(Convert.ToChar(buf[off + i]));
}
}
internal static bool IsOneOf(string s, params string[] candidates)
{
foreach (string b in candidates) {
if (s == b)
return true;
}
return false;
}
public static string FromByteArray(byte[] bs)
{
if (bs == null)
throw new ArgumentNullException("bs");
int num = bs.Length;
char[] array = new char[num];
for (int i = 0; i < num; i++) {
array[i] = Convert.ToChar(bs[i]);
}
return new string(array);
}
public static string FromByteArray(byte[] buf, int off, int len)
{
Arrays.ValidateSegment(buf, off, len);
char[] array = new char[len];
for (int i = 0; i < len; i++) {
array[i] = Convert.ToChar(buf[off + i]);
}
return new string(array);
}
public static byte[] ToByteArray(char[] cs)
{
byte[] array = new byte[cs.Length];
for (int i = 0; i < array.Length; i++) {
array[i] = Convert.ToByte(cs[i]);
}
return array;
}
public static byte[] ToByteArray(string s)
{
byte[] array = new byte[s.Length];
for (int i = 0; i < array.Length; i++) {
array[i] = Convert.ToByte(s[i]);
}
return array;
}
public static string FromAsciiByteArray(byte[] bytes)
{
return Encoding.ASCII.GetString(bytes);
}
public static string FromAsciiByteArray(byte[] bytes, int index, int count)
{
return Encoding.ASCII.GetString(bytes, index, count);
}
public static byte[] ToAsciiByteArray(char[] cs)
{
return Encoding.ASCII.GetBytes(cs);
}
public static byte[] ToAsciiByteArray(char[] chars, int index, int count)
{
return Encoding.ASCII.GetBytes(chars, index, count);
}
public static byte[] ToAsciiByteArray(string s)
{
return Encoding.ASCII.GetBytes(s);
}
public static string FromUtf8ByteArray(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes);
}
public static string FromUtf8ByteArray(byte[] bytes, int index, int count)
{
return Encoding.UTF8.GetString(bytes, index, count);
}
public static byte[] ToUtf8ByteArray(char[] cs)
{
return Encoding.UTF8.GetBytes(cs);
}
public static byte[] ToUtf8ByteArray(char[] chars, int index, int count)
{
return Encoding.UTF8.GetBytes(chars, index, count);
}
public static byte[] ToUtf8ByteArray(string s)
{
return Encoding.UTF8.GetBytes(s);
}
public static byte[] ToUtf8ByteArray(string s, int preAlloc, int postAlloc)
{
int byteCount = Encoding.UTF8.GetByteCount(s);
byte[] array = new byte[preAlloc + byteCount + postAlloc];
Encoding.UTF8.GetBytes(s, 0, s.Length, array, preAlloc);
return array;
}
}
}