For a project I'm working on, I need to be able to convert a UTF8 string into bits or bytes for modification and back. (Bit/Byte type doesn't matter.. '138' or '00000001' or '0A' - as long as I'm able to modify it as a string and convert the Bytes back into a string)
I've tried a few things, but nothing has worked. For the most part, I've been trying to take the result of File.ReadAllBytes(path)
and convert the bytes into a modifiable string and back, but haven't had any success. Also tried doing the same using a test string into a BitArray
with no better luck.
Here's what I'm trying to do in a better example (pseudocode)
string input = "ABC";
Byte[] bytes = StringToBytes(input);
Byte[] bytes2;
string bytestring;
foreach (Byte byte in bytes) {
bytestring = byte.ToString();
/// modify bytestring to the value of a different byte here ///
bytes2.Add(bytestring.ToByte());
}
return BytesToString(bytes2);
Return: 'ACD'
(or whatever)
If you're trying to read a file into a byte[] array, modify those bytes, and convert that back to a string, you could do something like this:
// read the file into a byte array
var bytes = File.ReadAllBytes(inputFileName);
// modify the bytes
// now convert back to a UTF string
var stringFromByteArray = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
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.