-1

I've a byte array

byte[] arrOutput = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE4, 0x04, 0x00, 0x00, 0x09, 0x04, 0x00, 0x00, 0x09, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

Note the 0xff.

With BlockCopy I made a replacement in byte array. Then I'm faced with the problem, to convert array to a binary string.

I tried (with a couple of encodings)

Encoding altEnc = Encoding.GetEncoding("????");
body = altEnc.GetString(data);

This does not work. When I use e.g. ASCII it cannot work because of the limitation to the max. support of 0x7f. Uf I use UTF-8 I get multiple bytes and never a 0xff.

I could not find any solution which convert bytes back to string... Any help appreciated.

19
  • 1
    What problem are you trying to solve? If you have an arbitrary selection of bytes and want to faithfully encode those into a string, you need to use an encoding designed for that problem, such as Base64 encoding. ASCII and UTF-8 are for working with an arbitrary string and turning it into an array of bytes. The opposite problem to the one I suspect you're trying to solve. For any of these, you have an arbitrary input on one side and a constrained output on the other. You can't then feed an arbitrary input to the decoders. Commented Oct 18, 2022 at 6:27
  • 2
    Does this answer your question? Byte to Binary String C# - Display all 8 digits (Note the linked question and answer is about a single byte, but i am confident you know already about loops and string concatenation...) Commented Oct 18, 2022 at 6:29
  • 2
    So modify it as binary data. Commented Oct 18, 2022 at 7:26
  • 2
    "No, unfortunately this produces a 010101 string and not the binary string I need." Then what exactly do you need the string to be? You gotta tell us precisely how the byte values have to be represented in the string, otherwise it's everyones guess and no cake for you... ;-) Commented Oct 18, 2022 at 7:43
  • 2
    What you're dealing with is not a string. The binary data does not represent a string. It is binary. It's as simple as that. Commented Oct 18, 2022 at 7:55

1 Answer 1

0

You need to determine what the encoding, decode the body, THEN do the replacement.

I'm not an expert on the Fiddler APIs but this is the general idea:

var enc = oSession.GetRequestBodyEncoding(); 
var reqBody = enc.GetString(oSession.requestBodyBytes);
var newReqBody = reqBody.Replace( oldString, newString );
var newBytes = enc.GetBytes( newReqBody );
oSession.utilSetRequestBody( newBytes );

Note: There is a chance your body is compressed. If so you will also need to decompress it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.