Sign in to follow this  
TheBluMage

[.net] C# byte string literals

Recommended Posts

TheBluMage    372
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 this post


Link to post
Share on other sites
Washu    7829
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 this post


Link to post
Share on other sites
TheBluMage    372
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 this post


Link to post
Share on other sites
Dean Harding    546
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 this post


Link to post
Share on other sites
TheBluMage    372
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 this post


Link to post
Share on other sites
kSquared    1356
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.

Share this post


Link to post
Share on other sites
kSquared    1356
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 this post


Link to post
Share on other sites
TheBluMage    372
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 this post


Link to post
Share on other sites
DrGUI    402
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this