0

Can one of you fine C# experts decipher this for me?

    int Length = 42;
    return new[] {(byte) (0x80 | Length)};

I found this in some on-line code (slightly modified now). But I'm not quite sure what it is saying. If someone could explain and/or re-write it in VB or Powershell that would be awesome.

I have googled for hours but my google-foo seems to be a little weak on this one.

EDIT: I've been told this question was not well worded because the code didn't work. I have attempted to re-write it to make it more universally appealing.

For reference, the original source is here: https://github.com/Sleepw4lker/TameMyCerts/blob/main/TameMyCerts/X509/X509CertificateExtension.cs

For anyone interested. I believe I was able to re-write this in Powershell.

    [int]$Length = 42
    Return ([byte[]](0x80 -bor $Length))
WayneA
  • 339
  • 1
  • 7
  • 1
    an `or` operation between `0x80` (128 in base10) and `byteArray.Length` then casted into `Byte` to be assigned into first element of the `Byte` array? – Bagus Tesa Jul 14 '23 at 23:22
  • 2
    What you posted won't compile; there has got to be more you haven't shown us. I suggest running the code in a debugger and [explain it to a rubber duck](https://rubberduckdebugging.com/). – Corvus Jul 14 '23 at 23:22
  • Please clarify your question. Provide the context of use if possible. – Nick Louloudakis Jul 14 '23 at 23:26
  • 1
    @WayneA you probably got downvoted because your code gives an arbitrary part of code that does not compile and you are asking what it does. Provide a snippet that is complete (assignment, byteArray variable definition). – Nick Louloudakis Jul 14 '23 at 23:36
  • @NickLouloudakis I think you are all missing the point. I was not asking how or why it worked. I was asking what it meant. And I guarantee it will be useful to have it here, because it is exactly what I just spent hours trying to figure out. – WayneA Jul 14 '23 at 23:40
  • 1
    @WayneA in SO guidelines: https://stackoverflow.com/help/how-to-ask It states "Include just enough code to allow others to reproduce the problem." In order to give you the answer, I had to make assumptions, and I should not have answered normally. Please update the question with a code snippet that would normally work. You do not have to provide your full code, only the 2-3 lines related to the part you are asking. – Nick Louloudakis Jul 14 '23 at 23:43
  • Don't get hung up on semantics The only 'problem' was I did not know what it was doing. And this is the only line related to my question. In fact I specifically removed part of the line to make it clear what my question was. – WayneA Jul 14 '23 at 23:44
  • it is precisely about semantics. You are asking me to show a code snippet that creates an error that others can reproduce. I wasn't asking anyone to fix a problem. The code worked just fine. I just didn't understand what it was doing. ( or to be more precise I did not recognize the syntax being used.) Now I appreciate your answering the question. it was exactly what I needed. But I don't need help 'fixing' it because it isn't broken. Hopefully it will be exactly what someone else needs in the future. – WayneA Jul 14 '23 at 23:55
  • @WayneA I get your point, but still, the way a question phrased should be very specific. There is a huge amount of discussions done in the past about how one asks good questions/provides good answers. It is very easy to slightly deviate and have your question closed. If you had provided the code snippet, specifying this was part of a larger statement, you probably would be OK. And please consider this is not an argumentative debate: I am trying to explain to you how you can make your questions more valuable and effective to get helped on, after some years using SO. Nothing personal. – Nick Louloudakis Jul 15 '23 at 00:39
  • While I strongly disagree, I have tried my best (twice now) to modify the question to bring it more in-line with what you are expecting. – WayneA Jul 17 '23 at 12:40
  • @WayneA thanks for considering my comments and updating the question. FYI, I never downvoted it and you have my upvote now for your effort and clean question. – Nick Louloudakis Jul 18 '23 at 00:45

1 Answers1

1

This is an array initializer syntax in C#. You can check this question for all syntaxes. I infer that this is assigned to a variable of type byte[] - but your code is incomplete, so I can only make assumptions here.

In addition, the initializer explicitly adds the value of expression (byte) (0x80 | byteArray.Length) in the array. This is a bitwise OR operation between 0x80 in hex (128 in Decimal) and the length of the byteArray variable.

I do not know the purpose you are using this, but this it what it does in two sentences.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
  • Awesome, That is what I needed. It's 2 actions in one... – WayneA Jul 14 '23 at 23:27
  • Yes, but the code you provided is not clean - provide the full expression and make sure that your question has a value for the community. – Nick Louloudakis Jul 14 '23 at 23:29
  • P.S. I did check the question you suggested. Unfortunatly I did not put together that there was more than 1 thing going on. – WayneA Jul 15 '23 at 00:02