简体   繁体   中英

C# How can i remove newline characters from binary?

Basically i have binary data, i dont mind if it's unreadable but im writing it to a file which is parsed and so it's importance newline characters are taken out.

I thought i had done the right thing when i converted to string....

byte[] b = (byte[])SubKey.GetValue(v[i]);
s = System.Text.ASCIIEncoding.ASCII.GetString(b);

and then removed the newlines

String t = s.replace("\n","")

but its not working ?

换行符可能是\\ r \\ n,您的二进制数据可能不是ASCII编码的。

Firstly newline ( Environment.Newline ) is usually two characters on Windows, do you mean removing single carriage-return or line-feed characters?

Secondly, applying a text encoding to binary data is likely to lead to unexpected conversions. Eg what will happen to buyes of the binary data that do not map to ASCII characters?

New line character may be \\n or \\r or \\r\\n depends on operating system type , in order this is markers for Linux , Macintosh and Windows .

But if you say you file is binary from what you know they have newlines in ASCII in her content?

If this is binary file this may be a some struct , if this they struct you after remove newline characters shift left all data after the this newline and corrupt data in her .

I would imagine removing the bytes in a binary chunk which correspond the line feeds would actually corrupt the binary data, thereby making it useless.

Perhaps you'd be better off using base64 encoding, which will produce ASCII-safe output.

If this is text data, then load it as text data (using the correct encoding), replace it as as a string, and re-encode it (using the correct encoding). For some encodings you might be able to do a swap at the file level (without decoding/encoding), but I wouldn't bet on it.

If this is any other binary representation, you will have to know the exact details. For example, it is common (but not for certain) for strings embedded in part of a binary file to have a length prefix. If you change the data without changing the length prefix, you've just corrupted the file. And to change the length prefix you need to know the format (it might be big-endian/little-endian, any fixed number of bytes, or the prefix itself could be variable length). Or it might be delimited. Or there might be relative offsets scattered through the file that all need fixing.

Just as likely; you could by chance have the same byte sequence in the binary that doesn't represent a newline; you could be completely trashing the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM