-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.

syncgw
  • 21
  • 5
  • 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. – Damien_The_Unbeliever Oct 18 '22 at 06:27
  • Why does it need to be a string? A normal way would be to convert it to a hex string or a base64 string. – ProgrammingLlama Oct 18 '22 at 06:29
  • 2
    Does this answer your question? [Byte to Binary String C# - Display all 8 digits](https://stackoverflow.com/questions/4829366/byte-to-binary-string-c-sharp-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...) –  Oct 18 '22 at 06:29
  • Also note your problem is entirely unrelated to text encodings. You do not intend to encode characters, you intend to represent some numeric (byte) value in some numeral system (binary). Text encodings, however, deal with encoding/decoding characters as/from numeric (byte) values. Looking for some text encoding to do what you want is a fools errand and a misunderstanding of what a text encoding is, because text encodings do not deal with numeral systems. –  Oct 18 '22 at 06:35
  • @Damien_The_Unbelieve The problem is that I've a binary MAPI/HTTP buffer which I need to change on the fly (replace user). – syncgw Oct 18 '22 at 07:07
  • @ProgrammingLlama I tried with base64 string, but there I cannot use .replace in string because I if I encode my string replacement I could not find original string – syncgw Oct 18 '22 at 07:11
  • @MySkullCaveIsADarkPlace No, unfortunately this produces a 010101 string and not the binary string I need. Yes nothing to do with encoding, but I must convert back byte array to string, because Fiddler only accepts string in ```oSession.utilSetRequestBody``` – syncgw Oct 18 '22 at 07:12
  • Can't you upload files with Fiddler (you can with Classic, not sure about the new one)? If so, why not make your change to a binary file and upload that binary file? I can pretty much guarantee Fiddler will also be using something like UTF8 for its `utilSetRequestBody` so even choosing an encoding where this is supported wouldn't get you far. – ProgrammingLlama Oct 18 '22 at 07:15
  • @ProgrammingLlama I do not have the binaries. I try to catch exactly this traffic to find out what is send back and forth during mapi/HTTP Outlook connecting client. During Microsoft RCA testing I need to modify the binary request to replace userid in Fiddler. – syncgw Oct 18 '22 at 07:24
  • 2
    So modify it as binary data. – ProgrammingLlama Oct 18 '22 at 07:26
  • @ProgrammingLlama String ```.replace``` does not work. So I saw the only solution to convert it to something different and convert it back to string. Tried base64, hex and now ```byte[]``` array. I always ended up with troubles converting this back to binary. – syncgw Oct 18 '22 at 07:32
  • 1
    OP, where exactly are you getting this byte array? You say it comes from an HTTP or MAPI buffer of some kind. What API are you using to retrieve it? Are you using some sort of ASP.NET library? Perhaps you could include more code. – John Wu Oct 18 '22 at 07:42
  • 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... ;-) –  Oct 18 '22 at 07:43
  • 1
    Why are you mutating binary data as a string in the first place? The mind boggles... – ProgrammingLlama Oct 18 '22 at 07:45
  • With ```oSession.requestBodyBytes``` in Fiddler I get a ```byte[]``` array. There I need to replace in a mapiHTTP body the userid. For this purpose I use https://stackoverflow.com/questions/5132890/c-sharp-replace-bytes-in-byte/5132938#5132938. At the end I ***must*** user ```oSession.utilSetRequestBody``` to set replace Request body. This function only accepts ```string```. I was able to replace data in ```byte[]```array, but not to convert array back to "string". – syncgw Oct 18 '22 at 07:50
  • I started with the simplest approach: use ```replace```function. – syncgw Oct 18 '22 at 07:51
  • 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. – ProgrammingLlama Oct 18 '22 at 07:55
  • Sorry, I'm not a experienced C# programmer. And I need to convert a ```byte[]``` to something which I can manipulate and then back to a ```string```. And I'm searching for a way to get that problem solved. I'm open for any approach which solves this problem. – syncgw Oct 18 '22 at 08:16
  • a) You can manipulate byte arrays. b) You should not convert that to a string in most cases. – ProgrammingLlama Oct 19 '22 at 03:43

1 Answers1

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.

John Wu
  • 50,556
  • 8
  • 44
  • 80