i'm generating a pdf in my client using jsPDF, and want to encrypt it in my server app with SharpPDF, but when im trying to pass the jsPDF to SharpPDF i get that it can't recognize some characters:
here is the pdf value using jsPDF output and then encoding it into base 64
var out = doc.output();
var url = 'data:application/pdf;base64,' + out.toString(CryptoJS.enc.Base64);
%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Length 9717>>
stream
0.57 w
0 G
BT
/F1 14 Tf
16.099999999999998 TL
0 g
227.46 510.24 Td
(SOME TEXT) Tj
ET
BT
/F3 8 Tf
9.2 TL
0 g
42.52 48.19 Td
then at my server when im trying to encrypt it using this example http://www.pdfsharp.net/wiki/ProtectDocument-sample.ashx
i get an error in this line:
System.IO.File.WriteAllText("D://" + data.Texto, data.Valor);
HERE--> PdfDocument document = PdfReader.Open("D://" + data.Texto, "some text");
PdfSecuritySettings securitySettings = document.SecuritySettings;
securitySettings.UserPassword = "user";
securitySettings.OwnerPassword = "owner";
The error is Token 'obj' was not expected.
Related
I suspect that my decoding is not working properly. That is why I am testing it by encoding, decoding and the re-encoding to see if I am getting the same result. That is however not the case.
I encoded a byte[] named model.PDF to a base64 string.
Now, for decoding, I converted model.PDF to a decoded base64 string. However the output looks faulty or corrupted upon debugging and I suspect this is where something is going wrong.
To encode again, the decoded data is turned into byte[] again and then into an encoded base64 string. However base64EncodedData does not match plainTextEncodedData. Please help me create a flawless encode to decode to re-encode flow.
// ENCODING - Byte array -> base64 encoded string
string base64EncodedData = Convert.ToBase64String(model.PDF);
// DECODING - Byte array -> base64 decoded string
var base64DecodedData = Encoding.UTF8.GetString(model.PDF);
// ENCODING AGAIN
byte[] plainTextBytes = Encoding.UTF8.GetBytes(base64DecodedData);
var plainTextEncodedData = Convert.ToBase64String(plainTextBytes);
To elaborate, the re-encoding matches the initial encoding perfectly if executed like this.
var PDF = System.Text.Encoding.UTF8.GetBytes("redgreenblue");
string base64EncodedData = Convert.ToBase64String(PDF);
// DECODING - Byte array -> base64 decoded string
var base64DecodedData = Encoding.UTF8.GetString(PDF);
// ...
But, my model.PDF is fetched from the database as shown below, in which case the re-encoding does not match.
while (reader.Read()) {
model.PDF = reader["PDF"] == DBNull.Value ? null : (byte[])reader["PDF"];
}
On an online base64 decoder (https://www.base64decode.org/), decoding an example value of base64EncodedData shows the ideal and correct value.
%PDF-1.5
%
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(en-IN) /StructTreeRoot 8 0 R/MarkInfo<</Marked true>>>>
endobj
2 0 obj
<</Type/Pages/Count 1/Kids[ 4 0 R] >>
endobj
3 0 obj
<</Author(admin) /CreationDate(D:20190724114817+05'30')
/ModDate(D:20190724114817+05'30') /Producer(Microsoft Excel 2013) /Creator(Microsoft Excel 2013) >>
endobj
4 0 obj
<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 6 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 612 792] /Contents 5 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>
endobj
5 0 obj
<</Filter/FlateDecode/Length 171>>
stream
...
However, in my program, the value of base64DecodedData shows up in its entirety as:
%PDF-1.5
%����
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(en-IN) /StructTreeRoot 8 0 R/MarkInfo<</Marked true>>>>
endobj
2 0 obj
<</Type/Pages/Count 1/Kids[ 4 0 R] >>
endobj
3 0 obj
<</Author(admin) /CreationDate(D:20190724114817+05'30')
/ModDate(D:20190724114817+05'30') /Producer(��
The 2 look similar in ways but my program seems to be producing a corrupt version of what the actual base64 decoded string should be.
A PDF is an ASCII file that can contain binary data (including strings in other encodings).
So you cannot read it as plain text.
If a PDF file contains binary data, as most do [...] the header line
shall be immediately followed by a comment line containing at least
four binary characters—that is, characters whose codes are 128 or
greater.
Taken from this answer, which has some more infos
You see exactly these four characters in your own output.
I am using Adobe's EchoSign API to retrieve a string representation of a PDF file. The problem I am running into is that writing the file to disk is working properly. The file length is a much different length than the string and won't open as a PDF.
As a test, I used an existing PDF file - one that I know is a true PDF, and tried to pull the contents of the file as a string like their API provides and then write it back to another file. The result is the same. I can open the "real" PDF using Adobe, but the new file will not open. This should be simple, but I am obviously missing something.
Here is what I have done to test this out:
Scenario 1: Using string received from the API
File.WriteAllText(fileName, PDFstring, new UTF8Encoding(false));
Scenario 2: Using string received from the API. Yeah, it seemed dumb, but nothing has been working.
using (var sw = File.CreateText(fileName))
{
for (int p = 0; p < PDFstring.Length; p++)
{
var c = PDFstring.Substring(p, 1);
sw.Write(c);
}
}
Scenario 3: Use a known good PDF file and try to copy it by creating a string and writing it to a new file.
var filename = #"C:\Adobe\GoodDocument.pdf";
var newFile = #"C:\Adobe\Rewrite.pdf";
var fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
var file = new StreamReader(fs);
var allAdobe = file.ReadToEnd();
fs.Close();
File.WriteAllText(newFile, allAdobe, new UTF8Encoding(false));
All three scenarios gave the same results. I cannot use the new file. The file lengths are all longer than they should be. Attempting to open the new file asks for a password where the original does not.
Obeservation: I just ran scenario 3 again. Accept this time using the copied (incorrect) file as the original. The result was an exact duplicate! What gives? Is Adobe playing tricks with me?
The #hans-kilian answer is enought if you won't edit something before rewrite the document, but i think you can read it a string changing Reading format and Writing format to ASCII:
var filename = #"C:\Adobe\GoodDocument.pdf";
var newFile = #"C:\Adobe\Rewrite.pdf";
var fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
var file = new StreamReader(fs, System.Text.Encoding.Default);
var allAdobe = file.ReadToEnd();
fs.Close();
File.WriteAllText(newFile, allAdobe, System.Text.Encoding.Default);
EDIT: I realize only now that your string come from an API, so that's the only viable solution :)
EDIT2: Ok, i read your link and i understand that you need to decode in base 64 some chunks of your PDF strings, and i think is what i was saying you in my yesterday comment:
I open a "test.pdf" with notepad++ and i've that piece of code:
%PDF-1.7
4 0 obj
(Identity)
endobj
5 0 obj
(Adobe)
endobj
8 0 obj
<<
/Filter /FlateDecode
/Length 146861
/Type /Stream
>>
stream
[.......] LOTS OF ANSI CHARACTERS [.......]
endstream
endobj
13 0 obj
<<
/Font <<
/F1 11 0 R
>>
>>
endobj
3 0 obj
<<
/Contents [ 12 0 R ]
/CropBox [ 0.0 0.0 595.32001 841.92004 ]
/MediaBox [ 0.0 0.0 595.32001 841.92004 ]
/Parent 2 0 R
/Resources 13 0 R
/Rotate 0
/Type /Page
>>
endobj
10 0 obj
<<
/Length 535
>>
stream
/CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def /CMapName /Adobe-Identity-UCS def /CMapType 2 def 1 begincodespacerange <0000> <FFFF> endcodespacerange 15 beginbfchar <0003> <0020> <0018> <0044> <0026> <0046> <002C> <0048> <0057> <0050> <0102> <0061> <011E> <0065> <015D> <0069> <0175> <006D> <0190> <0073> <019A> <0074> <01C7> <0079> <0355> <002C> <0357> <003A> <035B> <2019> endbfchar endcmap CMapName currentdict /CMap defineresource pop end end
endstream
endobj
9 0 obj
[ 3 3 226 24 24 615 38 38 459 44 44 623 87 87 516 258 258 479 286 286 497 349 349 229 373 373 798 400 400 391 410 410 334 455 455 452 853 853 249 855 855 267 859 859 249 ]
endobj
6 0 obj
[ -798 -268 798 952 ]
endobj
7 0 obj
798
endobj
2 0 obj
<<
/Count 1
/Kids [ 3 0 R ]
/Type /Pages
>>
endobj
1 0 obj
<<
/Pages 2 0 R
/Type /Catalog
>>
endobj
14 0 obj
<<
/Author (user)
/CreationDate (D:20180713094854+02'00')
/ModDate (D:20180713094854+02'00')
/Producer (Microsoft: Print To PDF)
/Title (Microsoft Word - Documento1)
>>
endobj
xref
0 15
0000000000 65535 f
0000148893 00000 n
0000148834 00000 n
0000147825 00000 n
0000000009 00000 n
0000000035 00000 n
0000148778 00000 n
0000148815 00000 n
0000000058 00000 n
0000148591 00000 n
0000148004 00000 n
0000147008 00000 n
0000147480 00000 n
0000147780 00000 n
0000148942 00000 n
trailer
<<
/Info 14 0 R
/Root 1 0 R
/Size 15
>>
startxref
149133
%%EOF
(i use code snippet just to have the code correctly formatted ;) )
What i've inside [.......] LOTS OF ANSI CHARACTERS [.......] is ANSI but in your situation is a base64string that need to be "replaced" with his base64 decoded to ANSI string, if i'm right you can do that like below:
byte[] data = Convert.FromBase64String(your_base_64_string);
string decodedString = Encoding.Default.GetString(data);
Let me know if you can hit the goal :)
PDF is a binary format. So you need to read and write them as bytes like this:
var document = File.ReadAllBytes("document.pdf");
File.WriteAllBytes("new document.pdf", document);
While Legion technically answered the posed question, I feel it's necessary for anyone following in my footsteps to get the full answer.
What lead to this question was me trying to write the content of a response to an Adobe Sign API call to a file.
I am using C# and the RestSharp library. This is important. The RestSharp IRestResponse object that provides the content apparently creates this property from the data received from the call. Because the content is so complex, creating the string representation immediately made writing it to a PDF file impossible. Digging deeper into the response object, I noticed a property call RawBytes. This is a byte array of the response. If I write the byte array directly to disk, everything.just.works.
Sorry to bother everyone with this. I was one layer above the actual problem
I am trying to use an API to download documents which will be in either PDF or Word document formats. The service gives directions only up to how to construct the request URL, which I have done.
The API offers an xml or json method in the url, I am using xml.
I have tried to get the string and parsing it to an XDocument:
XDocument response;
using (var webClient = new System.Net.WebClient())
{
response = XDocument.Parse(webClient.DownloadString(url));
}
Console.WriteLine(response);
I also tried downloading as a file:
using (var webClient = new System.Net.WebClient())
{
webClient.DownloadFile(url, filepath));
}
Both produce the same result:
%PDF-1.5
%µµµµ
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(en-US) >>
endobj
2 0 obj
<</Type/Pages/Count 2/Kids[ 3 0 R 27 0 R] >>
endobj
3 0 obj
<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 5 0 R/F2 7 0 R/F3 9 0 R/F4 11 0 R/F5 13 0 R/F6 21 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/Annots[ 18 0 R 19 0 R 20 0 R 26 0 R] /MediaBox[ 0 0 612 792] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S>>
endobj
4 0 obj
<</Filter/FlateDecode/Length 6592>>
stream
xœÝ=k“Û6’ß]åÿÀOYÍžÍo2ÙrÝ8‰sÙ‡Çn²Æ2=¦¡|ó°ãÔþøën€/‘ÐH r]¥"%ˆÝht7ú…KÎïÞ:Q™H•0#R©®xš©ä²¼{çå_ïÞùßíC~ùkRÓ“åi.›B¥Fï÷Œ‡ÏîÞùôK¸I<{y÷K2ø%ŒóTfR™'Ï.îÞÉRø,ÉRžkx•Ery>ñá_ß½óÛ"9ùwòìïwï|ÏÿÑ…J‹¼A„ñ4×r®<a,Í䮺ÈS×\¤ïáZÌ"ë6Ts•*UCd
NUÃS„ªÊ¤<.UU‘š T•9)&U¥NMª
–ʸTðû Te9rRLªò•L
and a lot more like this goes on. It's very odd characters, I am not sure how to parse an XML or PDF document from this.
You could use HttpClient for Async requests Httpclient is a better option instead of webclient
public static async Task get()
{
HttpClient _httpClient = new HttpClient())
string url = "https://someurl.com";
var result = await _httpClient.GetAsync($"{url}");
// contents to a log file
string resultContent = await result.Content.ReadAsStringAsync();
File.WriteAllText("D://dT.pdf", resultContent);
// ... write to log
}
I have a txt file and readline is not working right for my file.
My lines in my code.
And this is my text in my file.Lines are like this, but my code doesnt understand lines like this.
X02233 52330 DISCHY 8 BLUZ
std STD 0 0 0 0 0 8698230653909 0.00
X02237 52337 VALONIA BLUZ STD STD 0 0 0 0 0 8698230653916 0.00
X02245 72458 HARMONY 9 BLUZ STD STD 0 0 0 0 0 8698230653923 0.00
UPDATE :
var text = File.ReadAllText(lblPath.Text);
var lines = text.Split('\n'); //Unix-based newline
var longestLine = lines.OrderByDescending(a => a.Length).First();
var shortestLine = lines.OrderBy(a => a.Length).First();
var orderByShort = lines.OrderBy(a => a.Length);
I get out of memory exception in this code.Above example is only a part of my file.My notepad file is 105 MB.
You can use File.ReadAllText to read the whole file to a string and then use the Split method to split based on the end of line character your file is using:
var text = File.ReadAllText(myFilePath);
var lines = text.Split("\n"); //Unix-based newline
File.ReadAllLines by default uses \r\n sequence for new lines - see documentation:
A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed.
i want to read a .dat file that have following conditions:-
First name offset 21
First name format ASCIIz 15 chars + \0
Middle initials offset 37
ID offset -8
ID format/length Unsigned int (4 bytes)
so help me for sorting this issue in c#.
Thanks in advance.
Gurpreet
.dat file
( ÿ / rE ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ XÙþÞ¦d e e Mr. Sam Ascott Sam 9209 Sandpiper Lane 21204 410 5558987 410 5556700 275 MM229399098 (¬ Þ e ܤ•Þ„ œÔ£ÝáØáØ ’Þ[Þ €–˜ ä–˜ [Þ ¶ Norman Eaton Friend of Dr. Shultz Removal of #1,16,17 & 32 öÜÝ)Ý Ä d 01 21 21 21 e 101 22099 XÙþÞ¦d e . Mrs. Patty Baxter Patty 3838 Tommytrue Court 21234 410 2929290 410 3929209 FM218798127 HAY FEVER Þ . „¤¢Þè _ÐÍÝBÒBÒ ’ÞÝ €–˜ ä–˜ ÍÝ f Joanne Abbey
Here is a tutorial how to use BinaryReader for this purpose:
http://dotnetperls.com/binaryreader
You can use Jet OleDB to query .dat files:
var query = "select * from file.dat";
var connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\file.dat;Extended Properties=\"text;HDR=NO;FMT=FixedLength\"");
See this link:
Code Project. Read Text File (txt, csv, log, tab, fixed length)
And check these:
Reading a sequential-access file
DAT files in C#
Read a file in C#
BinaryReader Class
Jon Skeet. Reading binary data in C#
As said, have a look at the BinaryReader
//Example...
BinaryReader reader = new BinaryReader(stream);
string name = Encoding.ASCII.GetString(reader.ReadBytes(8));
int number = reader.ReadInt32();
my .dat file content - " €U§µPÕ „ÕG¬u "
click here to see the content of my .dat file in Notepad and hexa editor
string fileName = #"W:\yourfilename.dat";
//Read the binary file as byte array
byte[] bHex = File.ReadAllBytes(fileName);
//Create string builder for extracting the HEX values
StringBuilder st = new StringBuilder();
//initialize the int for 0
int i = 0;
// check it worked
//Reverse the HEX array for readability
foreach (char c in bHex.Reverse())
{
i++;
// 12 to 21 byte in the reverse order for interseted value in ticks"
if (i > 12 && i < 21)
st.Append(Convert.ToInt32(c).ToString("X2"));
}
// Convert HEX to Deciamal
long Output = Convert.ToInt64(st.ToString(), 16);
//Convert ticks to date time
DateTime dt = new DateTime(Output);
//Write the output date to console
Console.Write(dt);
Final de-crypt binary content to data-time.
final output of the program