Sorry for asking a question like that, but I'm really stuck.
I have this method for reading data from file:
public void ReadFromFile()
{
string fileName = #"my .txt file path";
StreamReader sr;
List<char> encoded = new List<char>();
List<byte> converted = new List<byte>();
using (StreamReader sr = new StreamReader(fileName))
{
string line = sr.ReadToEnd();
string[] lines = line.Split('\n');
foreach (var v in lines[2])
{
encoded.Add(v); // just get data I need
}
} }
Now in encoded I have F and # symbols.
I want to get 01000110 (F representation) and 01000000 (# representation)
I tried to convert every item in List<char> encoded into bytes and then use Convert.ToString(value, 2)
But it's not a good idea, because there's a mistake "Value was either too large or too small for an unsigned byte."
in the output file I have something like this:
s,01;w,000;e,1;t,001; // dictionary of character and its code
6 // number of zeros
F# // encoded string
So what I want to do is to DECODE this thing into the input string (that is 'sweet'). For this, I need to decode F# into 0100011001000000
Related
How do I read chars from other countries such as ß ä?
The following code reads all chars, including chars such as 0x0D.
StreamReader srFile = new StreamReader(gstPathFileName);
char[] acBuf = null;
int iReadLength = 100;
while (srFile.Peek() >= 0) {
acBuf = new char[iReadLength];
srFile.Read(acBuf, 0, iReadLength);
string s = new string(acBuf);
}
But it does not interpret correctly chars such as ß ä.
I don't know what coding the file uses. It is exported from code (into a .txt file) that was written 20 plus years ago from a C-Tree database.
The ß ä display fine with Notepad.
By default, the StreamReader constructor assumes the UTF-8 encoding (which is the de facto universal standard today). Since that's not decoding your file correctly, your characters (ß, ä) suggest that it's probably encoded using Windows-1252 (Western European):
var encoding = Encoding.GetEncoding("Windows-1252");
using (StreamReader srFile = new StreamReader(gstPathFileName, encoding))
{
// ...
}
A closely-related encoding is ISO/IEC 8859-1. If the above gives some unexpected results, use Encoding.GetEncoding("ISO-8859-1") instead.
I need to read text file with ASCII encoding char by char. If i have in that file aposthrope(’) character then i get questionmark instead. I checked if the file is in ASCII and it is so there won't be problem.
StreamReader reader = new StreamReader(this.path, Encoding.ASCII);
while (!reader.EndOfStream)
{
char chr = (char) reader.Read();
// if i read character ’ then the content of chr is: 63 '?'
// but i need aposthrope not questionmark
}
Your source must be saved in a different encoding format. If you want to change the question mark(63) to an apostrophe(39) all you have to do is modify your code like so
StreamReader reader = new StreamReader(this.path, Encoding.ASCII);
int c = 0;
while (!reader.EndOfStream)
{
c=reader.Read()
char chr = c==63?(char)39:(char)c;
}
Learn something new: ? : : is the ternary operator and is a very nice shorthand for something like this.
(check condition)?(result if true):(result if false)
I have a text file that I need to read and modify. This file come from another program so I can not modify its format. I need to use it as a template and make a bunch of replacements for specific cases. One of the lines I am reading is delimited with 0xFF characters. But when I call ReadLine the string returns the line delimited with 0x3F characters. I have tried different encodings. ASCII where it comes back as 0x3f and UTF-8 where it comes back as 3bytes 0xEF 0xBF 0xBD. The original text file seems to be ANSI format and the 0xFF character shows up as a "ÿ". How can I get my ReadLine (and the subsequent WriteLine) to keep this character intact?
var replacements = new Dictionary<string, string> { {"to_replace1", "replacement1"}, {"to_replace2", "replacement2"}, {"etc etc", "more replaces"} };
using (var writer = new StreamWriter(projectfileSpecific, false, Encoding.ASCII))
{
foreach (var line in File.ReadLines(projectfileTemplate, Encoding.ASCII))
{
foreach (var replacement in replacements)
{
if (line.Contains(replacement.Key))
{
var replaceLine = line;
writer.WriteLine(replaceLine.Replace(replacement.Key, replacement.Value));
}
else
{
writer.WriteLine(line);
}
}
}
}
I have a text file which is encoded with codepage 850. I am reading this file the following way:
using (var reader = new StreamReader(filePath, Encoding.GetEncoding(850)))
{
string line;
while ((line = reader.ReadLine()) != null)
{
//...
}
//...
}
Now I need for every character in the string line in the loop above the zero-based index of that character which it has in codepage 850, something like:
for (int i = 0; i < line.Length; i++)
{
int indexInCodepage850 = GetIndexInCodepage850(line[i]); // ?
//...
}
Is this possible and how could int GetIndexInCodepage850(char c) look like?
Use Encoding.GetBytes() on the line. CP850 is an 8-bit encoding, so the byte array should have just as many elements as the string had characters, and each element is the value of the character.
Just read the file as bytes, and you have the codepage 850 character codes:
byte[] data = File.ReadAllBytes(filePath);
You don't get it separated into lines, though. The character codes for CR and LF that you need to look for in the data are 13 and 10.
You don't need to.
You are already specifying the encoding in the streamreader constructor.
The string returned from reader.ReadLine() will already have been encoding using CP850
I want to read a file but not from the beginning of the file but at a specific point of a file. For example I want to read a file after 977 characters after the beginning of the file, and then read the next 200 characters at once. Thanks.
If you want to read the file as text, skipping characters (not bytes):
using (var textReader = System.IO.File.OpenText(path))
{
// read and disregard the first 977 chars
var buffer = new char[977];
textReader.Read(buffer, 0, buffer.Length);
// read 200 chars
buffer = new char[200];
textReader.Read(buffer, 0, buffer.Length);
}
If you merely want to skip a certain number of bytes (not characters):
using (var fileStream = System.IO.File.OpenRead(path))
{
// seek to starting point
fileStream.Seek(977, SeekOrigin.Begin);
// read 200 bytes
var buffer = new byte[200];
fileStream.Read(buffer, 0, buffer.Length);
}
you can use Linq and converting array of char to string .
add these namespace :
using System.Linq;
using System.IO;
then you can use this to get an array of characters starting index a as much as b characters from your text file :
char[] c = File.ReadAllText(FilePath).ToCharArray().Skip(a).Take(b).ToArray();
Then you can have a string , includes continuous chars of c :
string r = new string(c);
for example , i have this text in a file :
hello how are you ?
i use this code :
char[] c = File.ReadAllText(FilePath).ToCharArray().Skip(6).Take(3).ToArray();
string r = new string(c);
MessageBox.Show(r);
and it shows : how
Way 2
Very simple :
Using Substring method
string s = File.ReadAllText(FilePath);
string r = s.Substring(6,3);
MessageBox.Show(r);
Good Luck ;
using (var fileStream = System.IO.File.OpenRead(path))
{
// seek to starting point
fileStream.Position = 977;
// read
}
if you want to read specific data types from files System.IO.BinaryReader is the best choice.
if you are not sure about file encoding use
using (var binaryreader = new BinaryReader(File.OpenRead(path)))
{
// seek to starting point
binaryreader.ReadChars(977);
// read
char[] data = binaryreader.ReadChars(200);
//do what you want with data
}
else if you know character size in source file size are 1 or 2 byte use
using (var binaryreader = new BinaryReader(File.OpenRead(path)))
{
// seek to starting point
binaryreader.BaseStream.Position = 977 * X;//x is 1 or 2 base on character size in sourcefile
// read
char[] data = binaryreader.ReadChars(200);
//do what you want with data
}