# [.net] C# byte string literals

This topic is 4999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there any way to define byte arrays from a string literal? For example: byte[] MyByteString = "Hey, I'm an array of bytes!"; I know I could use Encoding.ASCII.GetBytes, but I was just wondering if there was anything a little more compile-time-ish out there besides defining the chars as byte literals or the incredibly inconvinient: byte[] MyByteString = {(byte)'H',(byte)'e',(byte)'y' ... }; I've Googled my head off and found nothing, but I thought I'd try here before I gave up. Thanks in advance for whatever answers you can provide!

##### Share on other sites
Nope, literal characters are of a char type, and a literal string is of string type. There is no implicit cast to byte since the char type is a unicode type, and thus may not be representable as a byte.

##### Share on other sites
Thanks, that's what I thought. Too bad there isn't anything kind of like C++ where you can change what kind of literal the string is by putting different letters in front.

Byte String: "Something"
16-bit wide String: L"Something"
.NET String: S"Something"

But alas, not in C#. Oh well, I guess part of the beauty of being a programmer is that you get to design your own tools, so I wrote a little helper tool to do the conversion for me. :)

##### Share on other sites
You can use String.ToCharArray to convert it to a char [], but that may not be what you're after. Or Encoding.UTF8.GetBytes to get the UTF-8 version of the string (Don't use Encoding.ASCII unless you know there's no international characters. Though in the tests I did, the ASCII one is a bit faster.)

##### Share on other sites
Thanks for the suggestion and insight, but I'm trying to stick with something that won't require any runtime conversions. It's not quite so bad when I have a tool to do it for me, so I'll probably just stick with that for now. Thanks!

##### Share on other sites
Quote:
 Original post by TheBluMageThanks, that's what I thought. Too bad there isn't anything kind of like C++ where you can change what kind of literal the string is by putting different letters in front.Byte String: "Something"16-bit wide String: L"Something".NET String: S"Something"But alas, not in C#. Oh well, I guess part of the beauty of being a programmer is that you get to design your own tools, so I wrote a little helper tool to do the conversion for me. :)

You could, if you wanted, design your own attribute and attach metadata that took care of this. Attributes are the "proper" way to attach arbitrary metadata at compile time. However, this is a fairly complex task and might be a little overwhelming if you're just starting C#. You can read more about attributes at this tutorial and the reference document for C# attributes.

##### Share on other sites
I like the sound of that. How would I go about doing it? The tutorials were a little too basic I think. [lol]

##### Share on other sites
I'd really have to think about that before I could give you a good answer. This is really a hackish solution to the problem. To get started, you'll need to declare a new class derived from Attribute, then give it the AttributeUsage(AttributeTargets.Field) property at least.

I think a better (and probably more effective) way would be to define a utility function that took care of this for you. The skeleton would be something like:

namespace MyProject.Framework.Tools{  public class StringUtility  {    // default to ASCII encoding    public static byte[] GetByteEquivalent(string s)    {      return StringUtility.GetByteEquivalent(s, new ASCIIEncoding());    }    // specify an arbitrary Encoding object    public static byte[] GetByteEquivalent(string s, Encoding e)    {      // ...    }  }}

This is probably the best strategy for you if you're going to be using it in many different places.

##### Share on other sites
OK, well thanks for the input. I think I'll probably just keep using the converter for now, at least for anything I'll know at compile-time. Thanks for all your ideas!

##### Share on other sites
Quote:
 Original post by TheBluMage...I guess part of the beauty of being a programmer is that you get to design your own tools... :)

Yeah, that is useful! Last year when we were doing gradient functions (its objective being to 'discover differentiation') in maths, everyone had to work out f(x-0.1), f(x-0.01), f(x), f(x+0.01), and f(x+0.1) using their calculator. It took me ~20 min to write a program to do this for me and it saved loads of time.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631759
• Total Posts
3002138
×