C# sbyte is written to the file as an unsigned byte [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Using the BinaryWriter I write an sbyte variable to a file but it gets written to the file as unsigned and not signed as it should be.
sbyte a = (sbyte)image.pixelData[i + 0];
bw.Write(a);
For example, the above code writes values ranging from 0x00 to 0xFF. (These are the values I see in a hex editor.)

You are demonstrating a fundamental misunderstanding of how data is stored in memory, files, etc.
All computer data is in binary form.
The different data types determine how the data is treated, how calculations are performed, how values are formatted, etc.
If you write a signed value to a file, it is always written using binary form (the only format a computer understands). If you read that data with a hex editor, then the hex editor will translate the data using whatever translation it considers to be appropriate.
If you write data to a file as a signed byte, and then you read that same data back as a signed byte, then the data will be the same as the data written. You should expect no more, and no less.
(Note: If you use the hex editor I wrote (Cygnus Hex Editor), you can inspect the data using any data type. In that case, you can have it appear in the format you expect. Otherwise, the hex editor is converting it to hex, or whatever, which tells you nothing about how the data is stored in the file.)

Related

Optimized parse text file, to then upload to Excel [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My project is to take large amounts of logs, output inside text files and parse some of the data to be made into Excel format.
There is a lot of garbage data in between not needed
This is how one portion of it is like:
2018-05-17 07:16:57.105>>>>>>
{"status":"success", "statusCode":"0", "statusDesc":"Message Processed Sucessfully", "messageNumber":"451", "payload":{"messageCode":"SORTRESPONSE","Id":"5L","Id":"28032","messageTimestamp":"2018-05-16 23:16:55"}}
I will first need to take the time stamp befor the "{}"
as it differs from the messageTimestamp
When generating the excel workbook
This is how it will look like in Excel:
------A-----------------------------------B--------------C
1. Overall time stamp ---------- status------- statusCode
2. 2018-05-17 07:16:57.105 - success --- -0
And so on...
payload has its own section of logs within its "{}"
so its section in excel will look like this:
F
1. payload
2. {"messageCode":"SORTRESPONSE","Id":"5L","Id":"28032","messageTimestamp":"2018-05-16 23:16:55"}
its content can be in one section that's not an issue.
A friend of mine have done something similar but it can take a few minutes to even generate even one relatively small excel document
My Question:
What is the most optimal way can I parse the data needed to then store it in an array or multidimensional array
to then push it into an excel document.
I would try to split the input text on newline characters, then parse the JSON part with Newtonsoft.Json.
I would highly advise to not try to parse the JSON yourself. The bottleneck here will be disk IO not in-memory processing, so make it easy to write correct code and use 3rd party libraries.
Once you have structured data representing the input, you can write each entry to an output file with only the fields you need.
For an Excel file, is CSV okay or do you need XLSX files? For CSV you can just write to a file directly, for XLSX I would recommend the EPPlus library.
https://www.newtonsoft.com/json
https://archive.codeplex.com/?p=epplus

Hashing a byte array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have an input byte array which is huge in size ( > 8000 bytes). I need to store this byte array in DB, and fetch later for further operation.
The problem is, this byte array column is unique. Assume that if I need to retrieve the entire table information on this byte array column. It becomes extremely costly operation when it comes to byte comparison. So thought of storing the Hash Value of this byte array just to make the comparison operation easier.
Just wanted to know if hash value which is generated from the byte array will be unique OR is there any other way to achieve this.
If the size (in bytes) of the hash is smaller than 8000, this is not possible. After all, there are 256 ^ 8000 possible inputs, so there must be at least that many possible outputs if the hash function needs to be unique.
As the default C# hashcode returns an int, which (depending on your architecture) is 32 or 64 bits (so 4 or 8 bytes), this is not even close to be possible with the default hash function. (Of course, you could write your own hash function, but that's quite pointless.)

Splitting a 200 bit hexadecimal bitmask [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a bitmask of 200 bit stored as a hexadecimal value.
In order to apply the & bit operator, I have to first convert the hex to an integer but 200 bit is too big for a uint64 so my question is : how do I split my bitmask in 4 different hexadecimal value without loosing data?
So that I can also split my 200 bit data and then compare every chunk of data with the corresponding chunk of bitmask without altering the result.
You can use the BigInteger from System.Numerics (it's a separate assembly):
BigInteger bi = BigInteger.Parse("01ABC000000000000000000000000000000000", System.Globalization.NumberStyles.HexNumber);
VERY IMPORTANT: prepend a "0" before the hex number! (because BigInteger.Parse("F", NumberStyles.HexNumber) == -1, while BigInteger.Parse("0F", NumberStyles.HexNumber) == 15
BigInteger implement the "classical" logical operators (&, |, ^)
Requires .NET 4.0
The most efficient way of achieving this is writing a class that can store and do binary operations on 200bits of data, have strings as input, etc.

How I can read after EOF? C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I`m using C# and I need to read something after EOF. Is it possible by using C#? How?
Thanks.
You cant. EOF means end of file, there's nothing actually in the file after that.
You may as well ask how you can get ten gallons of oil from a four-gallon drum. Once it's empty, there's no more to be had.
Since you're talking C# hence Windows (and based on your comment and data located behind the end of file pointer), it's possible that they may be referring to "DOS mode" text files, which are (or used to be, I haven't investigated recently) terminated by the CTRL-Z character.
From the earliest days of the PC revolution, where CP/M used integral numbers of disk blocks to store a file and only stored the number of disk blocks rather than the number of bytes, CTRL-Z was used to indicate end of file if the file wasn't an exact multiple of the disk block size.
If that's the case, it's probably best just to open the file as a binary file, then read up to the first CTRL-Z character (code point 26) - everything beyond that could be considered data beyond EOF if it's truly a text file of that format.

Handling special characters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am wondering how to best handle a special character such as ’ using c#?
e.g
public static string DecodeFrom64(string toDecode)
{
byte[] arrayToDecode = System.Convert.FromBase64String(toDecode);
return System.Text.Encoding.Unicode.GetString(arrayToDecode);
}
The problem here is that you've stored a UTF-8 string to a different encoding in your database - probably the Windows-1252 code page (CP2152). As a result the UTF-8 character ’ represented by the byte sequence E2 80 99 is translated into the CP2152 single-byte characters ’. This was all explained to your previously in this answer, which also gives a solution to your current problem.
In order to get back to the original UTF-8 encoding you will need to take the string returned from your database and correct it with the following code:
public static string UTF8From1252(string source)
{
// get original UTF-8 bytes from CP1252-encoded string
byte[] bytes = System.Text.Encoding.GetEncoding("windows-1252").GetBytes(source);
return System.Text.Encoding.UTF8.GetString(bytes);
}
This highlights the fact that it is vital to use the correct encoding at all times when using the GetBytes method.
It is important to note that the reverse of this transformation is not always possible, since there are gaps in the CP2152 code space - values that will be discarded or altered during conversion from byte values.
The hex values for these gaps are: 81 8D 8F 90 9D.
Unfortunately these values are present in various UTF-8 encodings, such as ” (E2 80 9D). If you have one of these values in your database then it will not load correctly. Depending on how you did the first stage conversion the third byte may be lost or corrupted in the database, in which case you cannot retrieve it.

Categories