2

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.

splattne
  • 102,760
  • 52
  • 202
  • 249
rcode
  • 23
  • 5
  • It looks like you're taking the string "Hello World", and turing it into ASCII bytes. The easy way to do this is `Encoding.ASCII.GetBytes("Hello World")` – canton7 May 21 '21 at 09:26
  • In addition to @canton7 's approach you can then convert that into a hex string with `var hexString = BitConverter.ToString(byteArray);`, or if individual hexes are your thing, consider using `BitConverter.ToString()` on each element of `byteArray` – DekuDesu May 21 '21 at 09:27
  • Yeah, it's not clear what the end result should be -- `byte b = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64};` isn't valid C#, so it's not clear whether you want a byte array, or whether you want a string containing the literal value `"byte b = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64};"`, or something else – canton7 May 21 '21 at 09:28
  • Does this answer your question? [Converting string to byte array in C#](https://stackoverflow.com/questions/16072709/converting-string-to-byte-array-in-c-sharp) – Franz Gleichmann May 21 '21 at 09:30
  • @canton7 byte b is the hexadecimal representation of "Hello World". I need to send this byte array to the server. The server is expecting a hex value representation of whatever my data might be. So if I want to send String s as above I need to convert it to byte[] b like above – rcode May 21 '21 at 09:46
  • 1
    Does this answer your question? [How can I convert a hex string to a byte array?](https://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array) and [Convert string to hex-string in C#](https://stackoverflow.com/questions/16999604/convert-string-to-hex-string-in-c-sharp) –  May 21 '21 at 09:47
  • 2
    @rcode once again: a `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. – Marc Gravell May 21 '21 at 09:50
  • @MarcGravell I am trying to send values to a TCP/IP server. can you clear something up for me? when I convert a string to a byte array look like from the inside? from my understanding if you simply convert string to byte for example string "abc" to byte array then that would look like this: [97,98,99]. which are the decimal representations of a,b,c in ascii table. – rcode May 21 '21 at 10:03
  • @MarcGravell when I capture my packets through Wireshark I want them to look like [61,62,63]. am I making sense? – rcode May 21 '21 at 10:05
  • 3
    @rcode yes, but that's just wireshark representing them as hex; they aren't sent *as* hex - nor are they sent as octal, decimal, etc; they're sent **as bytes**; you can get those bytes via `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. – Marc Gravell May 21 '21 at 10:15
  • 3
    @rcode You're confused. The values sent over the network are just numbers -- hex, decimal, etc, are just ways that humans take numbers and turn them into readable text. The number 12 is the same number, whether it's written as `12` or `0xC` or `014` or 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 – canton7 May 21 '21 at 10:15

2 Answers2

5

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);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

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:

Microsoft .NET documentation: Encoding Class

splattne
  • 102,760
  • 52
  • 202
  • 249
  • I am dealing with ASCII and Non-ASCII characters. ASCII for msg content and Non-ASCII for flags and commands. hope this gives some clarity. – rcode May 21 '21 at 09:49
  • 1
    @rcode not really, no; an example of some "Non-ASCII" characters you are using, and what do you expect the encoded value to be, would be more useful. – Marc Gravell May 21 '21 at 09:51
  • for example i am using 0x06(Decimal = 6, ASCII value = ack) for acknowledgment. correction for my above comment: I meant non-printable ASCII chars not Non-ASCII. – rcode May 21 '21 at 09:56
  • @rcode k; that's fine - ASCII or UTF8 will give the same output for that; where it gets interesting is everything that requires 8 (or more) bits, so: character 128 onwards - in particular (but not limited to) international characters; you *either* need a specific code-page, or something like UTF8, but we can't advise on which *without more information*; if there are no characters >= 128, then ASCII and UTF8 are functionally identical – Marc Gravell May 21 '21 at 10:18
  • @MarcGravell I won't be going on >128 side of ASCII till I stay on plain text stuff. but things will go crazy as soon as I enter encrypted data. – rcode May 21 '21 at 10:28