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.

11
  • 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") Commented May 21, 2021 at 9:26
  • 1
    Does this answer your question? How can I convert a hex string to a byte array? and Convert string to hex-string in C# Commented May 21, 2021 at 9: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. Commented May 21, 2021 at 9:50
  • 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. Commented May 21, 2021 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 Commented May 21, 2021 at 10:15

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

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 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.
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 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
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.