This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
byte[] array pattern search
I am trying to write a simple compression algorthm in C# and .NET 3.5 and I need to be able to search a particular file for occurrences of a certain sequence of bits. what is the fastest way of doing this?
Since the file is 8K I would start by reading all the bytes into an in memory array like this:
byte[] bytes = System.IO.File.ReadAllBytes(fileName);
There is a code sample to search in this stack overflow entry:
byte[] array pattern search
Related
This question already has an answer here:
Read each line in file and put each line into a string
(1 answer)
Closed 5 years ago.
I need to make something that can get a specific line number and get the information in it into a String. And I couldn't figure out how I can do that.
Does anybody have a sample code/example that solves this problem?
Use File.ReadAllText (or Lines it might be) and then split the text by newline into an array of strings.
Then you can index into that array.
This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
How can I convert a hex string to a byte array? [duplicate]
(4 answers)
Closed 9 years ago.
This is a duplicate question, my apologies everyone!
Firstly, I apologize if this is a simple question, I have been searching for a very long time, and either an answer about this does not exist, the answer I am looking for has been buried under answers to questions about how to convert a string to a byte array, or I'm not searching with the right terminology. I have also found a few answers on converting a single hex value into a byte but applying those methods to work with what I want to do doesn't really seem to work very well.
What I am looking for is not how to convert "string" to a byte array, rather, I am trying to convert an already in bytes value from a textbox, into something my application will recognize as a byte array. I'll try to explain better with an example:
textBox.Text = 019F314A
I want byte[] bytes to equal { 0x01, 0x9F, 0x31, 0x4A }
Hopefully that makes sense. Thanks to anyone who can offer any assistance!
I believe you can use Convert.ToByte(), you might have to slice your string in pairs and loop through it.
If you do a quick search there are many topics on this already on stackoverflow
How do you convert Byte Array to Hexadecimal String, and vice versa?
You can also look at this MS example, it is to convert to int, but the idea is the same.
http://msdn.microsoft.com/en-us/library/bb311038.aspx
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to read a text file reversely with iterator in C#
I need to read txt file more than 7 million lines from bottom to the top by just following line of code: I was wondering is the right way or shall i use iterator for this task? Some answers using iterator already in stackoverflow.
foreach (string line in File.ReadAllLines("read.txt").Reverse())
{
Console.WriteLine(line);
}
As files are not line based, there is no convenient way to read lines from the end of the file. What you are doing works fine, as long as the entire file fits in memory.
If you run into memory problems, you can read the file in chunks, e.g. File.ReadLines(path).Skip(6000000), then File.ReadLines(path).Skip(5000000).Take(1000000). This will read the file up to that point every time, but it will use less memory.
Another alternative would be to read chunks of bytes from the file, locate the line breaks in the bytes, and decode the bytes between them into strings.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the maximum possible length of a .NET string?
Is there limit for a C# string to hold data?
Strings cannot have more than 231 characters, since String.Length is a 32-bit integer.
They're also limited by available memory.
string.Length is int, so string can contain Int.MaxInt bytes - 2,147,483,647
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
.NET String to byte Array C#
How do I convert String to byte[] array and vice versa? I need strings to be stored in some binary storage. Please show example in both directions. And one more thing: each string maybe bigger than 90Kb.
If you want to use UTF-8 encoding:
// string to byte[]
byte[] bytes = Encoding.UTF8.GetBytes(someString);
// byte[] to string
string anotherString = Encoding.UTF8.GetString(bytes);
Before you march off and use one of the examples someone's already given you should be aware that there is, in general, no unique mapping between a string and a sequence of bytes. How the string is mapped to binary (and vice versa) is determined by the encoding that you use. Joel Spolsky wrote an awesome article on this subject.
When decoding binary to get a string, you need to use the same encoding as was used to produce the binary in the first place, otherwise you'll run into problems.
Use the Encoding class.
How do I get a consistent byte representation of strings in C# without manually specifying an encoding?