Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Huge output of 0's on >256 character to Convert.ToHexString() #101757

Closed
Hashbrown777 opened this issue May 1, 2024 · 8 comments
Closed

Huge output of 0's on >256 character to Convert.ToHexString() #101757

Hashbrown777 opened this issue May 1, 2024 · 8 comments

Comments

@Hashbrown777
Copy link

Description

I'm hoping this works (you guys can repro) and isn't something wrong with PowerShell's calling of .NET.
I'm running Powershell 7.4 (with whatever .NET API that installs with).

Reproduction Steps

Expected behavior

Probably "Error: "Value was either too large or too small for an unsigned byte."" or 200F

Actual behavior

image

Regression?

No response

Known Workarounds

No response

Configuration

x64 Fedora Linux, I cannot ascertain the .NET version, google tells me to look up the non-existent registry value (please let me know if there's a way to ask the library itself at runtime, even if there might be a way to see via the installed files)

Other information

image

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 1, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 1, 2024
@colejohnson66
Copy link

colejohnson66 commented May 1, 2024

Convert.ToHexString is documented as only allowing byte-like types. Specifically, byte[], byte[] with offset+length, and ReadOnlySpan<byte>. Trying to execute Convert.ToHexString('\u0100') or Convert.ToHexString('\u200F') fails to compile:

image

But if ran from PowerShell, it does indeed output a string of n zeros (where n is the character's value times two). I wonder if it's creating a byte array of length c and passing it into the method? An empty array of length n converted to a hexadecimal string is just n*2 zeros.

For example, [Convert]::ToHexString([char]0x200F).Length is 16414, and so is Convert.ToHexString(new byte[(uint)'\u200F']).Length:
image

This sounds like a PowerShell bug, not a .NET one.

@huoyaoyuan
Copy link
Member

You can use [Environment]::Version to display underlying runtime version in PowerShell.

This sounds like a PowerShell bug, not a .NET one.

It may be an un-intuitive behavior of implicit dynamic casting.

@colejohnson66
Copy link

colejohnson66 commented May 1, 2024

I tested on .NET 8.0.4 through PowerShell 7.4.2. If ran on the old non-Core PowerShell 5.1, it uses .NET Framework 4.0, so Convert.ToHexString doesn't exist (it was added in 5).

While it could be argued to be implicit casting behavior, I'd argue that it's a bug to implicitly cast an integer to an array of that length. But that's for PowerShell's repo to decide, not here.

@am11
Copy link
Member

am11 commented May 1, 2024

The issue is with the char[] cast.
[Convert]::ToHexString([char]"`u{200F}") should be:
[Convert]::ToHexString([System.Text.Encoding]::Unicode.GetBytes("`u{200F}"))

@am11 am11 added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 1, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@Hashbrown777
Copy link
Author

Hashbrown777 commented May 1, 2024

Anyway to port issues across or do I close and open a new one over at PowerShell?

Edit: nvm, @colejohnson66 has done it. I guess I'll close this one if Michael considers it a pwsh issue on that ticket?

@Hashbrown777
Copy link
Author

The issue is with the char[] cast.

Yeah I know how to do it properly once I saw the error, it's just that the error I saw wasn't an exception/message, it was some bad behaviour, so I thought I'd report it.

@Hashbrown777
Copy link
Author

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label May 1, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants