binaryreader

Converting from byte[] to string

别等时光非礼了梦想. 提交于 2019-12-02 00:29:27
问题 I have the following code: using (BinaryReader br = new BinaryReader( File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite))) { int pos = 0; int length = (int) br.BaseStream.Length; while (pos < length) { b[pos] = br.ReadByte(); pos++; } pos = 0; while (pos < length) { Console.WriteLine(Convert.ToString(b[pos])); pos++; } } The FILE_PATH is a const string that contains the path to the binary file being read. The binary file is a mixture of integers and characters. The integers are 1 bytes

Converting from byte[] to string

不羁岁月 提交于 2019-12-01 21:16:08
I have the following code: using (BinaryReader br = new BinaryReader( File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite))) { int pos = 0; int length = (int) br.BaseStream.Length; while (pos < length) { b[pos] = br.ReadByte(); pos++; } pos = 0; while (pos < length) { Console.WriteLine(Convert.ToString(b[pos])); pos++; } } The FILE_PATH is a const string that contains the path to the binary file being read. The binary file is a mixture of integers and characters. The integers are 1 bytes each and each character is written to the file as 2 bytes. For example, the file has the following

Downloading pdf file using WebRequests

我的梦境 提交于 2019-11-30 09:04:32
I'm trying to download a number of pdf files automagically given a list of urls. Here's the code I have: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; var encoding = new UTF8Encoding(); request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-gb,en;q=0.5"); request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate"); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"; HttpWebResponse resp = (HttpWebResponse

Efficient way to read big endian data in C#

纵然是瞬间 提交于 2019-11-30 07:05:11
I use the following code to read BigEndian information using BinaryReader but I'm not sure if it is the efficient way of doing it. Is there any better solution? Here is my code: // some code to initialize the stream value // set the length value to the Int32 size BinaryReader reader =new BinaryReader(stream); byte[] bytes = reader.ReadBytes(length); Array.Reverse(bytes); int result = System.BitConverter.ToInt32(temp, 0); CodesInChaos BitConverter.ToInt32 isn't very fast in the first place. I'd simply use public static int ToInt32BigEndian(byte[] buf, int i) { return (buf[i]<<24) | (buf[i+1]<

Why does Code Analysis tell me, “Do not dispose objects multiple times” here:

允我心安 提交于 2019-11-29 16:34:47
On this code: public static string Base64FromFileName(string fileName) { try { FileInfo fInfo = new FileInfo(fileName); long numBytes = fInfo.Length; FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fStream); byte[] bdata = br.ReadBytes((int)numBytes); br.Close(); fStream.Close(); return Convert.ToBase64String(bdata); } catch(Exception e) { throw e; } } ...I get, courtesy of Visual Studio's Code Analysis tool, the warning, " Do not dispose objects multiple times...To avoid generating a System.ObjectDisposedException you should

Downloading pdf file using WebRequests

筅森魡賤 提交于 2019-11-29 14:10:31
问题 I'm trying to download a number of pdf files automagically given a list of urls. Here's the code I have: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; var encoding = new UTF8Encoding(); request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-gb,en;q=0.5"); request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate"); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.UserAgent = "Mozilla/5.0

Send a value by socket and read the value by reader.ReadInt32() changes value

一笑奈何 提交于 2019-11-29 12:56:24
I am trying to send a value by socket .So i have two parts in my project Client and server . The client sends a value to server using this code : System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(networkStream); binaryWriter.Write(1); binaryWriter.Write(2); binaryWriter.Flush(); So in other part i need to read the two values i mean 1 and 2 ; So in server part i have this code : static void Listeners() { Socket socketForClient = tcpListener.AcceptSocket(); if (socketForClient.Connected) { NetworkStream networkStream = new NetworkStream(socketForClient); while (true) { List<int>

C# checking for binary reader end of file

不羁岁月 提交于 2019-11-29 00:54:51
I was searching for a way to check whether I've reached the end of a file for my binary reader and one suggestion was to use PeekChar as such while (inFile.PeekChar() > 0) { ... } However, it looks like I've run into an issue Unhandled Exception: System.ArgumentException: The output char buffer is too sma ll to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'Syste m.Text.DecoderReplacementFallback'. Parameter name: chars at System.Text.Encoding.ThrowCharsOverflow() at System.Text.Encoding.ThrowCharsOverflow(DecoderNLS decoder, Boolean nothin gDecoded) at System.Text

Send a value by socket and read the value by reader.ReadInt32() changes value

荒凉一梦 提交于 2019-11-28 07:11:19
问题 I am trying to send a value by socket .So i have two parts in my project Client and server . The client sends a value to server using this code : System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(networkStream); binaryWriter.Write(1); binaryWriter.Write(2); binaryWriter.Flush(); So in other part i need to read the two values i mean 1 and 2 ; So in server part i have this code : static void Listeners() { Socket socketForClient = tcpListener.AcceptSocket(); if (socketForClient

C# checking for binary reader end of file

。_饼干妹妹 提交于 2019-11-27 15:28:45
问题 I was searching for a way to check whether I've reached the end of a file for my binary reader and one suggestion was to use PeekChar as such while (inFile.PeekChar() > 0) { ... } However, it looks like I've run into an issue Unhandled Exception: System.ArgumentException: The output char buffer is too sma ll to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'Syste m.Text.DecoderReplacementFallback'. Parameter name: chars at System.Text.Encoding.ThrowCharsOverflow() at