How to BitConverter.GetBytes() return 2 digits only
-
Hello every one simple code as follow
Dim MyInt As Int16 = 12
Dim result As Byte() =BitConverter.GetBytes(MyInt)
Console.WriteLine(BitConverter.ToString(result).Length)
Console.WriteLine(BitConverter.ToString(result))this will return as follow 5 0C-00 Just I want to get 2 0C (length 2 only) and return (0C) only I try to change "MyInt" Declare type from Int16 to all available types but did not work, also i dont need to subtract result as string, i need result as byte() type any advise please ? I am using VS2019 thanks in advance
-
Hello every one simple code as follow
Dim MyInt As Int16 = 12
Dim result As Byte() =BitConverter.GetBytes(MyInt)
Console.WriteLine(BitConverter.ToString(result).Length)
Console.WriteLine(BitConverter.ToString(result))this will return as follow 5 0C-00 Just I want to get 2 0C (length 2 only) and return (0C) only I try to change "MyInt" Declare type from Int16 to all available types but did not work, also i dont need to subtract result as string, i need result as byte() type any advise please ? I am using VS2019 thanks in advance
-
Hello every one simple code as follow
Dim MyInt As Int16 = 12
Dim result As Byte() =BitConverter.GetBytes(MyInt)
Console.WriteLine(BitConverter.ToString(result).Length)
Console.WriteLine(BitConverter.ToString(result))this will return as follow 5 0C-00 Just I want to get 2 0C (length 2 only) and return (0C) only I try to change "MyInt" Declare type from Int16 to all available types but did not work, also i dont need to subtract result as string, i need result as byte() type any advise please ? I am using VS2019 thanks in advance
-
Hello every one simple code as follow
Dim MyInt As Int16 = 12
Dim result As Byte() =BitConverter.GetBytes(MyInt)
Console.WriteLine(BitConverter.ToString(result).Length)
Console.WriteLine(BitConverter.ToString(result))this will return as follow 5 0C-00 Just I want to get 2 0C (length 2 only) and return (0C) only I try to change "MyInt" Declare type from Int16 to all available types but did not work, also i dont need to subtract result as string, i need result as byte() type any advise please ? I am using VS2019 thanks in advance
An int16 (or 'short') is a two-byte value. Intel processors are "little-endian", meaning the last byte (least significant) is stored first and the first byte (most significant) is stored last. In your example, your two byte value, (12) 0x000C, is stored in memory like this: 0x0C, 0x00. So that's why you're getting the bytes in the order you are. BitConverter is returning the bytes in the order they appear in memory. If you were running the exact same code on a processor that is big-endian, the bytes would show up in the array in the order opposite of what you're seeing now. Now, having said all that, if you're looking for just getting a string for the hexadecimal representation of your value, all you have to do is this:
Dim myInt As Int16 = 12
result = String.Format("X", myInt)If you're dealing with values that will never be greater than 255, why are you using an Int16? Why not just use Byte instead?
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak