Tech Off Thread

18 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

proposal for C# 3.0

Back to Forum: Tech Off
  • User profile image
    ScanIAm

    I'm constantly having to do conversions from character arrays to byte arrays and back again.  I know that there are pitfalls associated with unicode strings, but it would be pretty useful to have a way to:

    1) convert a string to an array of bytes 
    2) convert an array of bytes to a string 


    I needed to create a static byte array for a class that would do some conversion and ended up having to do this:

    public static byte[] FOO = new byte[] { (byte)'F', (byte)'O', (byte)'O');


    Obviously, this isn't very pretty.

  • User profile image
    TommyCarlier

    Why do you need the byte array?

  • User profile image
    TommyCarlier

    If you really need it, you can always do this:

    public static byte[] FOO = System.Text.Encoding.ASCII.GetBytes("FOO");

  • User profile image
    Dr Herbie

    TommyCarlier wrote:
    If you really need it, you can always do this:
    public static byte[] FOO = System.Text.Encoding.ASCII.GetBytes("FOO");


    Which is great if you can remember where the conversion function is in the framework. Always takes me about 5 minutes to find it.
    Would be great is function was moved/copied to something like string.ToASCIIBytes()

    Herbie

  • User profile image
    ScanIAm

    TommyCarlier wrote:
    If you really need it, you can always do this:
    public static byte[] FOO = System.Text.Encoding.ASCII.GetBytes("FOO");


    Damn, I knew that someone had to have done the heavy lifting already.  The encoding/encoder functions are powerful, but not as straightforward as I'd like.  I'll suffer, tho.

    I need the static because we're working on some code that will strip out sections of a received data packet and replace that with a tag.  This is being done because the packets contain text, but can also contain binary data (images).  Once we strip out the binaries, we manipulate the ASCII portion through XML and xml tends to complain about binary data Smiley

    Later, once the data has been manipulated, we turn it back into a byte buffer and replace the tag with the original binary.

  • User profile image
    TommyCarlier

    Dr Herbie wrote:
    Which is great if you can remember where the conversion function is in the framework. Always takes me about 5 minutes to find it.
    Would be great is function was moved/copied to something like string.ToASCIIBytes()
    You're right. These kinds of utility functions are sometimes hard to find. That's why I usually create some static utility classes myself, that simply call existing functions. Like this:
    public static class StringUtils
    {
       public static byte[] ToAsciiBytes(string s)
       { return System.Text.Encoding.ASCII.GetBytes(s); }


       public static string FromAsciiBytes(byte[] bytes)
       { return System.Text.Encoding.ASCII.GetString(bytes); }

       // other functions
    }

    public static byte[] FOO = StringUtils.ToAsciiBytes("FOO");

  • User profile image
    Ion Todirel

    Dr Herbie wrote:
    
    TommyCarlier wrote: If you really need it, you can always do this:
    public static byte[] FOO = System.Text.Encoding.ASCII.GetBytes("FOO");


    Which is great if you can remember where the conversion function is in the framework. Always takes me about 5 minutes to find it.
    Would be great is function was moved/copied to something like string.ToASCIIBytes()

    Herbie
    what stops you from creating a extension method?

  • User profile image
    Andrew Davey

    In C#3.0 you can create an extension method:

    public static byte[] ToASCIIBytes(this string input)
    {
       return System.Text.Encoding.ASCII.GetBytes(input);
    }

    then you can use like this:

    "FOO".ToASCIIBytes()

  • User profile image
    TommyCarlier

    Slightly off-topic: according to the naming guidelines, in acronyms and abbreviations of at least 3 letters, only the first letter can be in uppercase, so your function ToASCIIBytes should be named ToAsciiBytes. Unfortunately, the BCL team has also sinned against this in several places, like System.Text.Encoding.ASCII, which should be System.Text.Encoding.Ascii.

  • User profile image
    BlackTiger

    TommyCarlier wrote:
    Slightly off-topic: according to the naming guidelines, in acronyms and abbreviations of at least 3 letters, only the first letter can be in uppercase, so your function ToASCIIBytes should be named ToAsciiBytes. Unfortunately, the BCL team has also sinned against this in several places, like System.Text.Encoding.ASCII, which should be System.Text.Encoding.Ascii.


    You're not right.

    If you stumbled and fell down, it doesn't mean yet, that you're going in the wrong direction.
    Last modified
  • User profile image
    TommyCarlier

    What's not right about what I wrote?

  • User profile image
    Cannot​Resolve​Symbol

    TommyCarlier wrote:
    Slightly off-topic: according to the naming guidelines, in acronyms and abbreviations of at least 3 letters, only the first letter can be in uppercase, so your function ToASCIIBytes should be named ToAsciiBytes. Unfortunately, the BCL team has also sinned against this in several places, like System.Text.Encoding.ASCII, which should be System.Text.Encoding.Ascii.


    The design guidelines specify that for abbreviations, but not specifically for acronyms.  So, it's really a gray area.  In fact, the same page of the guidelines use a four-letter acronym and has it in all caps.

    (Plus, they're guidelines...)

  • User profile image
    TommyCarlier

    Read the 4th bullet-point on the page you linked to.

  • User profile image
    Cannot​Resolve​Symbol

    TommyCarlier wrote:
    Read the 4th bullet-point on the page you linked to.


    Ah, crap.  I can't read.

    Whatever.  It's still just a guideline.

  • User profile image
    Andrew Davey

    Doesn't FxCop complain about "ASCII"? If it does, have the BCL team specifically ignored the warning?

  • User profile image
    Ion Todirel

    Andrew Davey wrote:
    Doesn't FxCop complain about "ASCII"? If it does, have the BCL team specifically ignored the warning?
    yep:

    Untitledaa

  • User profile image
    Sven Groot

    Andrew Davey wrote:
    Doesn't FxCop complain about "ASCII"? If it does, have the BCL team specifically ignored the warning?

    Much of the BCL doesn't follow FxCop guidelines.

    In this case I agree with them. Although generally I agree with this rule, ASCII is a very common abbreviation and it's always used in all-caps.

  • User profile image
    TommyCarlier

    So are HTML (System.Web.UI.HtmlControls), XML (System.Xml), MIME (System.Net.Mime), SMTP (System.Net.Mail.SmtpClient), and many others. This rule is created so you would always know where an acronym starts and ends, and you could clearly split identifiers into words.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.