My question is how can I convert,
string s = "Hello World";
into
byte b = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64};
If there is a straightforward way in C# .NET Core then it will be very helpful.
The phrase "byte array in hex" makes no sense; bytes are bytes - they don't have any intrinsic format such as decimal, hex, octal: they're just values.
However, what you want is probably:
byte[] bytes = Encoding.UTF8.GetBytes(s);
to get the hex, you can then use tools to get a string again, for example:
string hex = BitConverter.ToString(bytes);
If you say you want an array of bytes for a given string, you have to specify in which way the byte array should represent the string. That's called encoding and the .NET framework has a built-in library (System.Text.Encoding) for handling string encoding operations.
For example, in order to get the string as ASCII representation, use this:
byte[] bytes = Encoding.ASCII.GetBytes(someString);
Of course, ASCII is a very limited set of characters.
Just explore your option here:
Encoding.ASCII.GetBytes("Hello World")byte[]is not hex - nor is it decimal, octal, or anything else; it is just values; can you be really really clear about how you're sending this value to the server? what API are you using here? a raw socket? or...? if you tell us what you're trying to do, we can probably help more.Encoding.GetBytes()as already answered twice. Basically forget about "hex" - that's purely a "how to display this value to meat-bag humans" thing. It exists only in the wireshark UI.12or0xCor014or 10 fingers + 2 toes or anything else. Wireshark is taking the number that it sniffed, and choosing to show its decimal representation to you in its user interface. If you want Wireshark to show you hex representations of numbers that's a Wireshark configuration thing