[.net] C# byte string literals

Started by
8 comments, last by DrGUI 19 years, 5 months ago
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!
Advertisement
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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. :)
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.)
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!
Quote:Original post by TheBluMage
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. :)

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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
I like the sound of that. How would I go about doing it? The tutorials were a little too basic I think. [lol]
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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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!
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.

This topic is closed to new replies.

Advertisement