I've encountered an issue with my PowerShell script where a specific string, '2012:08:12 12:12:11', isn't being recognized as a valid DateTime object. The string originates from hex data and is being processed in a loop. However, if I use the exact same string directly within the script, it works perfectly. I'm confused about what's going wrong when the string is processed from the hex data. I've noticed that there's a null terminator ('00') at the end of the input string, potentially causing the issue. How can I properly handle this to ensure the string is recognized correctly?
3 Answers
Make sure that the format of `$text` is exactly what you expect for `ParseExact()`. Check if there are any hidden spaces or unexpected characters. A good troubleshooting step is to try copying `$text` directly to clipboard using `$text | clip`, and then paste it into your `ParseExact` to see if it behaves differently. That way, you can catch any discrepancies easily!
It sounds like your issue is likely due to that '00' null terminator at the end of your string. Try using `$text.Trim([char]0)` right after you convert the hex—it should remove the null character and let `ParseExact` work without a hitch!
Oh, what a brilliant point! I'm pretty sure this is the actual answer. I will check it as soon as I get back to my computer. Thank you so much! Edit: Yes. Confirmed. This is it, as you said: the null terminator, and removing it resolves the issue.
I'm curious about the way you're defining `$hereStrings`. Instead of using a here-string and splitting, have you considered just defining an array directly with multiple byte sequences? It would simplify the code and avoid the need for `.split($n)`, which might also resolve other issues as well.

I like that technique with `$text | clip` and have taken note of it for the future. Thank you very much!