2

I am working with byte arrays and strings. I have a byte array that I modify and then use to generate a string. I have looked at lots of posts on this website that recommend using BlockCopy or System.Text.Encoding.Default.GetString(); I have tried those but for some reason the string I am getting has all gibberish characters.

Here is the problem and what i expect. Lets say i have hex encoded string of bytes as follows:

string str = "f20bdba6ff29eed7b046d1df9fb70000";

Corresponding array is:

byte[] arrayStr = new byte[] { 0xf2, 0x0b, 0xdb, 0xa6, 0xff, 0x29, 0xee, 0xd7, 0xb0, 0x46, 0xd1, 0xdf, 0x9f, 0xb7, 0x00, 0x00 };

Please note that 2 characters in above string represent byte.

Now, lets say I manipulate arrayStr and change the byte at array index 4 (0xff) to (0xe1). I want that I should be able to get a string such that:

string str = "f20bdba6e129eed7b046d1df9fb70000";

1 Answer 1

1

Look at BitConverter:

string str = BitConverter.ToString(arrayStr).Replace("-", "");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.