public QueryString(string queryString) { _Values = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(queryString)) { int i = 0; int ampersand; do { ampersand = queryString.IndexOf('&', i); string part = (ampersand == -1) ? queryString.Substring(i) : queryString.Substring(i, ampersand - i); int equals = part.IndexOf('='); if (equals == -1) { Add(part, null); } else { try { var value = BinString.FromUrlString(part.Substring(equals + 1)); Add(part.Substring(0, equals), value.ToString(Encoding.UTF8)); } catch { // ignore any invalid parameters } } i = ampersand + 1; } while (ampersand != -1); } }
public void TestOpenFile() { var contents = BinString.FromTextString("Hello, world!\n", System.Text.Encoding.ASCII); var dd = DataDir.Create(DataDirType.Temporary); string filename = DateTime.Now.Ticks.ToString("x") + ".tmp"; string fullFilename = Path.Combine(dd.Path, filename); Assert.IsFalse(File.Exists(fullFilename)); Assert.ThrowsException <FileNotFoundException>(() => dd.OpenFile(filename, FileMode.Open, FileAccess.Read)); using (var stream = dd.OpenFile(filename, FileMode.Create, FileAccess.ReadWrite)) { stream.Write(contents); stream.Close(); } Assert.IsTrue(File.Exists(fullFilename)); Assert.AreEqual(contents, (BinString)File.ReadAllBytes(fullFilename)); using (var stream = dd.OpenFile(filename, FileMode.Open, FileAccess.Read)) { var read = stream.ReadBinString(contents.Length); Assert.AreEqual(contents, read); } File.Delete(fullFilename); Assert.ThrowsException <ArgumentException>(() => dd.OpenFile(null, FileMode.Create, FileAccess.ReadWrite)); Assert.ThrowsException <ArgumentException>(() => dd.OpenFile(string.Empty, FileMode.Create, FileAccess.ReadWrite)); }
public void WriteAllBytes(string filePath, BinString data) { using (var stream = OpenFile(filePath, FileMode.Create, FileAccess.ReadWrite)) { stream.Write(data); } }
public BinString GetRandomBytes(int length) { if (_Disposed) { throw new ObjectDisposedException(null); } // if there isn't enough data in the buffer, generate some if (_Buffer.Length < length) { int iterations = length / 4 + 1; var builder = new BinStringBuilder(_Buffer.Length + iterations * 4); builder.Append(_Buffer); for (int i = 0; i < iterations; i++) { builder.Append(BitConverter.GetBytes(_Sample())); } _Buffer = builder.ToBinString(); } var r = _Buffer.Substring(0, length); _Buffer = _Buffer.Substring(length); return(r); }
public void Dispose() { if (!_Disposed) { _Buffer = null; _Disposed = true; } }
/// <summary> /// 16進数文字列→その他文字列変換処理 /// </summary> public void ConvertHexStringToOtherString() { uint workdata; // 数値変換 workdata = Convert.ToUInt32(HexString, 16); // 基数変換 DecString = Convert.ToString(workdata, 10); BinString = Convert.ToString(workdata, 2); BinString = BinString.PadLeft(BinString_MaxDigit, '0'); }
public void TestSimpleReadWrite() { string stringData = "Hello, world! This is a string.\n"; var binData = BinString.FromBytes("09F911029D74E35BD84156C5635688C0"); var dd = DataDir.Create(DataDirType.Temporary); dd.WriteAllText("test.txt", stringData, System.Text.Encoding.ASCII); Assert.AreEqual(stringData, dd.ReadAllText("test.txt", Encoding.ASCII)); dd.WriteAllBytes("test.bin", binData); Assert.AreEqual(binData, dd.ReadAllBytes("test.bin")); }
/// <summary> /// 設定データ→設定数値変換処理 /// </summary> public void ConvertSettingsToString() { uint workdata = 0; // 各設定値を設定して、数値変換する if (DACSelectData == DACSelectData_VOUT1) { workdata |= DACSelectData_Bit_VOUT1; } else if (DACSelectData == DACSelectData_VOUT2) { workdata |= DACSelectData_Bit_VOUT2; } else if (DACSelectData == DACSelectData_VOUT3) { workdata |= DACSelectData_Bit_VOUT3; } else if (DACSelectData == DACSelectData_VOUT4) { workdata |= DACSelectData_Bit_VOUT4; } else if (DACSelectData == DACSelectData_VOUT5) { workdata |= DACSelectData_Bit_VOUT5; } else if (DACSelectData == DACSelectData_VOUT6) { workdata |= DACSelectData_Bit_VOUT6; } else if (DACSelectData == DACSelectData_VOUT7) { workdata |= DACSelectData_Bit_VOUT7; } else if (DACSelectData == DACSelectData_VOUT8) { workdata |= DACSelectData_Bit_VOUT8; } else { ; } workdata |= Convert.ToUInt32(DACData, 10); HexString = Convert.ToString(workdata, 16); HexString = HexString.PadLeft(HexString_MaxDigit, '0'); DecString = Convert.ToString(workdata, 10); BinString = Convert.ToString(workdata, 2); BinString = BinString.PadLeft(BinString_MaxDigit, '0'); }
public void SystemRandomSourceTest() { using (var sr = new SystemRandomRandomnessSource()) { for (int i = 0; i < 32; i++) { Assert.AreEqual(i, sr.GetRandomBytes(i).Length); } } using (var seeded = new SystemRandomRandomnessSource(new Random(42))) { Assert.AreEqual(BinString.FromBytes("3e17ba96ae04cd3b99869e56f0adbf3a"), seeded.GetRandomBytes(16)); } }
public void TestGetPath() { var data = BinString.FromBytes("C0FFEEC0FFEEC0FFEEC0FFEE"); var dd = DataDir.Create(DataDirType.Temporary); string filename = DateTime.Now.Ticks.ToString("x") + ".tmp"; string fullPath = dd.GetFullPath(filename); Assert.IsFalse(File.Exists(fullPath)); dd.WriteAllBytes(filename, data); Assert.AreEqual(data, (BinString)File.ReadAllBytes(fullPath)); data = BinString.FromTextString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", Encoding.ASCII); File.WriteAllBytes(fullPath, data); Assert.AreEqual(data, dd.ReadAllBytes(filename)); Assert.IsTrue(File.Exists(fullPath)); dd.DeleteFile(filename); Assert.IsFalse(File.Exists(fullPath)); }
public string GetNewFileName(string hint = null, string extension = null) { var filebase = new StringBuilder(); if (string.IsNullOrEmpty(hint)) { int hash = BinString.FromTextString(Path, Encoding.UTF8).GetHashCode(); filebase.Append("temp"); filebase.Append((hash & 0xffffff).ToString(CultureInfo.InvariantCulture)); filebase.Append((DateTime.Now.Ticks & 0xffffffffffff).ToString(CultureInfo.InvariantCulture)); } else { int hash = BinString.FromTextString(hint, Encoding.UTF8).GetHashCode(); filebase.Append((hash & 0xffffff).ToString(CultureInfo.InvariantCulture)); filebase.Append((DateTime.Now.Ticks & 0xffffff).ToString(CultureInfo.InvariantCulture)); foreach (char c in hint.ToLowerInvariant()) { if (filebase.Length >= 16) { break; } if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) { filebase.Append(c); } } } extension = string.IsNullOrEmpty(extension) ? "" : ("." + extension.TrimStart('.')); string file = filebase.ToString() + extension; int i = 0; while (File.Exists(GetFullPath(file))) { i++; file = $"{filebase}-{i}{extension}"; } return(file); }
public override string ToString() { var sb = new StringBuilder(); foreach (var kvp in _Values) { if (sb.Length > 0) { sb.Append('&'); } sb.Append(kvp.Key); if (!string.IsNullOrEmpty(kvp.Value)) { sb.Append('='); sb.Append(BinString.FromTextString(kvp.Value, Encoding.UTF8).ToUrlString(false, CultureInfo.InvariantCulture)); } } return(sb.ToString()); }
public void WriteAllText(string filePath, string data, Encoding encoding) => WriteAllBytes(filePath, BinString.FromTextString(data, encoding));