❔ Converting from string into hex literal
I am looking for anyone who can help convert a hex string into a int hex literal. I am creating a usb program using usblibdotnet to find and connect to a device on my computer. In order to connect to the device I need the device PID and VID defined as hexedecimal intergers but how I know to retrieve them from device manager is as a string. If I try to use a simple conversion such as "Convert.ToInt32" or even "Int.Parse" it will convert the number into its literal numerical value rather than keeping it as its hexadecimal value. ex. string = "0x05E0" needs to be int = 0x05E0. Most methods I have found for converting end up making it int = 1504 which isn't wrong but it needs to stay as its literal hexadecimal value in order to work with the library and find the device.
17 Replies
needs to be int = 0x05E0
int
is int
There's only one representation of it
0o105
=== 0x45
=== 0b1000101
=== 69
It's all the same in memory
There's no "binary integer"
Or a "hexadecimal integer"
There's an integerWhen using the libusbdotnet library, in order to open the device for usb communication it asks for the vid and pid as a int but if I give it the actual interger value instead of the hex it says it is unable to open the device because it is not found. without changing any other code than just changing the values into their hex definition the method is now able to open the device. Would that mean the method in the library is recognizing them as 2 different values?
It only makes sense if it is taking a string instead of an int.
Once you have an int there is no concept of it being in hex or decimal, that's purely for display purposes. In memory it's 4 bytes, period.
Imagine C# having octal literals
For the meantime while I try to contact the developers of the library would there be any possible way to translate the string = "0x05E0" into an int without turning it into its decimal value of 1504?
int
is int
So they do take a string as input instead of an integer?
Because it is literally impossible for the behavior to change if they take an int as input, no matter what representation your original string used.
Once you convert from string to int it's the exact same four bytes in memory, period.
from what the method shows and asks for is that it needs the PID and VID of a device in a integer format however defining them as decimal values will not let the device appear and only allows it to appear when I define the values in a hexadecimal value. I appreciate the help in understanding this and I will see about waiting and contacting the developers of the library.
There is precedent for some code requiring strings to be in hex format. The .NET runtime itself does this for certain environment variables.
But those are strings.
Angius#1586
REPL Result: Success
Result: bool
Compile: 341.294ms | Execution: 54.258ms | React with ❌ to remove this embed.
They're the same exact value
There's no way a method that works with
0x05E0
won't work with 1504
Unless it's taking a string as input.
Which is what it sounds like to me.
You can specify a value without the 0x prefix for an app.config file setting, but it's not recommended. On .NET Framework 4.8+, due to a bug, a value specified without the 0x prefix is interpreted as hexadecimal, but on previous versions of .NET Framework, it's interpreted as decimal.OOF https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector
Ah, so now we'd need to hear what's the .NET version lol
That's specifically for the runtime reading GC-related configuration.
It shouldn't affect anything else I think.
Same in the IL btw
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.