0

In C you can do something like:

uint16_t datalen = 1024;
uint16_t crc = 0x1021;
uint8_t myHeader = {0x41, 0xBE, 0x21, 0x08, datlen/256, datalen%256, crc/256, crc%256};

Now, how can I accomplish an array initialization like this in Powershell?

I want to send the byte array later to serial port.

stev
  • 83
  • 1
  • 8

1 Answers1

2

Not so different:

[uint16]$datalen = 1024
[uint16]$crc = 0x1021
[byte[]]$myHeader = 0x41, 0xBE, 0x21, 0x08, ($datalen/256), ($datalen%256), ($crc/256), ($crc%256)
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thanks, that works. I missed the variable casting to [uint16] before the byte array. – stev Apr 18 '20 at 10:40
  • Hm, I was to early to take it as good, the problem is with the division, it is performed by rounding up the result, which is not wanted. How to avoid that? – stev Apr 18 '20 at 10:50
  • I think I have to use [Math]::Floor(), rigth? It is a bit of pain because it reduces readability, but at least it works. – stev Apr 18 '20 at 10:56
  • See [round fraction to an integer](https://stackoverflow.com/a/48893799/1701026) – iRon Apr 18 '20 at 15:58