本文整理汇总了C#中BitStream.Seek方法的典型用法代码示例。如果您正苦于以下问题:C# BitStream.Seek方法的具体用法?C# BitStream.Seek怎么用?C# BitStream.Seek使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitStream
的用法示例。
在下文中一共展示了BitStream.Seek方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToBase62
/// <summary>
/// Convert a byte array
/// </summary>
/// <param name="original">Byte array</param>
/// <returns>Base62 string</returns>
public static string ToBase62( this byte[] original )
{
StringBuilder sb = new StringBuilder();
BitStream stream = new BitStream( original ); // Set up the BitStream
byte[] read = new byte[ 1 ]; // Only read 6-bit at a time
while( true )
{
read[ 0 ] = 0;
int length = stream.Read( read, 0, 6 ); // Try to read 6 bits
if( length == 6 ) // Not reaching the end
{
if( (int)( read[ 0 ] >> 3 ) == 0x1f ) // First 5-bit is 11111
{
sb.Append( Base62CodingSpace[ 61 ] );
stream.Seek( -1, SeekOrigin.Current ); // Leave the 6th bit to next group
}
else if( (int)( read[ 0 ] >> 3 ) == 0x1e ) // First 5-bit is 11110
{
sb.Append( Base62CodingSpace[ 60 ] );
stream.Seek( -1, SeekOrigin.Current );
}
else // Encode 6-bit
{
sb.Append( Base62CodingSpace[ (int)( read[ 0 ] >> 2 ) ] );
}
}
else if( length == 0 ) // Reached the end completely
{
break;
}
else // Reached the end with some bits left
{
// Padding 0s to make the last bits to 6 bit
sb.Append( Base62CodingSpace[ (int)( read[ 0 ] >> (int)( 8 - length ) ) ] );
break;
}
}
return sb.ToString();
}
示例2: GetString
public static string GetString(byte[] data)
{
int letters = data.Length * 8 / 5;
string base32_string = "";
BitStream b = new BitStream();
b.Write(data, 0, data.Length);
b.Seek(0, System.IO.SeekOrigin.Begin);
if (b.Length == 0) return("");
long output_length = b.Length;
Console.WriteLine("bits in bitstream:" + b.Length);
if (b.Length % 5 != 0)
{
Console.WriteLine("rest bits:" + (b.Length % 5));
output_length += (5 - (b.Length % 5));
}
Console.WriteLine("bits in output buffer(+padding):" + output_length);
byte[] output = new byte[output_length];
output.Initialize();
//output.Length
int ret = b.Read(output, 0, (int)b.Length);//will crash with buffers over 4gb ,or something else
//now we have an array of bits in output that will be converted to base32
//5 bit to 1 byte + zero padding conversion
//pad to correct length with zeros
long pos = 0;
while (pos < output.Length)
{
int value = 0;
/*
value += output[pos + 0] * 1;
value += output[pos + 1] * 2;
value += output[pos + 2] * 4;
value += output[pos + 3] * 8;
value += output[pos + 4] * 16;
*/
value += output[pos + 0] * 16;
value += output[pos + 1] * 8;
value += output[pos + 2] * 4;
value += output[pos + 3] * 2;
value += output[pos + 4] * 1;
base32_string += conversion_array[value];
pos += 5;
}
Console.WriteLine("pos:"+pos);
return (base32_string);
}
示例3: TestBitRead
public void TestBitRead()
{
Console.WriteLine("Test to read bits from a BitStream.");
try
{
BitStream b = new BitStream();
byte[] test = { 0, 255, 0, 0, 255, 0 ,1,32};
b.Write(test, 0, test.Length);
b.Seek(0, System.IO.SeekOrigin.Begin);
Console.WriteLine("Bits in stream :" + b.Length);
Assert.IsTrue(b.Length == test.Length * 8, "length of bitstream not correct.");
byte[] output = new byte[b.Length];
int ret = b.Read(output, 0, output.Length);
Assert.IsTrue(ret == output.Length, "not enought bytes received from bitstream");
for (int i = 0; i < b.Length; i++)
{
Console.Write(output[i].ToString());
}
Console.WriteLine("");
for (int i = 0; i < b.Length; i++)
{
Console.WriteLine(i + ":" + output[i].ToString());
}
Assert.IsTrue(output[48] == 1, "Bits are not correct.");//1 in test bytes
Assert.IsTrue(output[61] == 1, "Bits are not correct.");//32 int test bytes
}
catch (Exception ex)
{
Console.WriteLine("exception occured: "+ex.Message);
Assert.Fail("exception occured.");
}
Console.WriteLine("Read Bits Test successful.");
}
示例4: FromBase62
// ==========================================================================
//
// Base62 Encoding
//
// ==========================================================================
public static byte[] FromBase62(string base62)
{
if (base62 == null)
return null;
if(base62.Length == 0)
return new byte[0];
int len1 = base62.Length - 1;
int count = 0;
var stream = new BitStream(base62.Length * 6 / 8);
foreach (char c in base62)
{
// Look up coding table
if (c < '0' || c > 'z')
return null;
int index = Base62Index.IndexOf(c);
// If end is reached
if (count == len1)
{
// Check if the ending is good
int mod = (int)(stream.Position % 8);
if (mod == 0)
return null;
if ((index >> (8 - mod)) > 0)
return null;
stream.Write(new byte[] { (byte)(index << mod) }, 0, 8 - mod);
}
else
{
// If 60 or 61 then only write 5 bits to the stream, otherwise 6 bits.
if (index == 60)
{
stream.Write(new byte[] { 0xf0 }, 0, 5);
}
else if (index == 61)
{
stream.Write(new byte[] { 0xf8 }, 0, 5);
}
else
{
stream.Write(new byte[] { (byte)index }, 2, 6);
}
}
count++;
}
// Dump out the bytes
byte[] result = new byte[stream.Position / 8];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(result, 0, result.Length * 8);
return result;
}
示例5: ToBase62
public static string ToBase62(this byte[] bytes)
{
if (bytes == null || bytes.Length == 0)
return "";
// https://github.com/renmengye/base62-csharp/
StringBuilder sb = new StringBuilder(bytes.Length * 3 / 2);
var stream = new BitStream(bytes); // Set up the BitStream
var b = new byte[1]; // Only read 6-bit at a time
while (true)
{
b[0] = 0;
int length = stream.Read(b, 0, 6); // Try to read 6 bits
if (length == 6) // Not reaching the end
{
if ((int)(b[0] >> 3) == 0x1f) // First 5-bit is 11111
{
sb.Append(Base62Index[61]);
stream.Seek(-1, SeekOrigin.Current); // Leave the 6th bit to next group
}
else if ((int)(b[0] >> 3) == 0x1e) // First 5-bit is 11110
{
sb.Append(Base62Index[60]);
stream.Seek(-1, SeekOrigin.Current);
}
else // Encode 6-bit
{
sb.Append(Base62Index[(int)(b[0] >> 2)]);
}
}
else if (length == 0) // Reached the end completely
{
break;
}
else // Reached the end with some bits left
{
// Padding 0s to make the last bits to 6 bit
sb.Append(Base62Index[(int)(b[0] >> (int)(8 - length))]);
break;
}
}
return sb.ToString();
}
示例6: FromBase62
/// <summary>
/// Convert a Base62 string to byte array
/// </summary>
/// <param name="base62">Base62 string</param>
/// <returns>Byte array</returns>
public static byte[] FromBase62( this string base62 )
{
// Character count
int count = 0;
// Set up the BitStream
BitStream stream = new BitStream( base62.Length * 6 / 8 );
foreach( char c in base62 )
{
// Look up coding table
int index = Base62CodingSpace.IndexOf( c );
// If end is reached
if( count == base62.Length - 1 )
{
// Check if the ending is good
int mod = (int)( stream.Position % 8 );
if( mod == 0 )
throw new InvalidDataException( "an extra character was found" );
if( ( index >> ( 8 - mod ) ) > 0 )
throw new InvalidDataException( "invalid ending character was found" );
stream.Write( new byte[] { (byte)( index << mod ) }, 0, 8 - mod );
}
else
{
// If 60 or 61 then only write 5 bits to the stream, otherwise 6 bits.
if( index == 60 )
{
stream.Write( new byte[] { 0xf0 }, 0, 5 );
}
else if( index == 61 )
{
stream.Write( new byte[] { 0xf8 }, 0, 5 );
}
else
{
stream.Write( new byte[] { (byte)index }, 2, 6 );
}
}
count++;
}
// Dump out the bytes
byte[] result = new byte[ stream.Position / 8 ];
stream.Seek( 0, SeekOrigin.Begin );
stream.Read( result, 0, result.Length * 8 );
return result;
}