Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Converting enum numeric value to ASCII in byte?

Converting enum numeric value to ASCII in byte?

Scheduled Pinned Locked Moved C#
tutorialquestion
9 Posts 7 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    So, I have an enum and want to get the ASCII code of the numeric value of a certain enum value. For example if I have the enum value STANDARD (=1), I want to get the ASCII code for '1' (49) in a byte variable. I cant seem to figure out how to do. Anyone?

    enum PackageType : byte { STANDARD=1, REQUEST=2, ANSWER=3 }

    PackageType type = PackageType.STANDARD;
    byte b = (byte)type; // This results in b==1, I want the ASCII (49)

    P Richard Andrew x64R R D B 6 Replies Last reply
    0
    • T TMattC

      So, I have an enum and want to get the ASCII code of the numeric value of a certain enum value. For example if I have the enum value STANDARD (=1), I want to get the ASCII code for '1' (49) in a byte variable. I cant seem to figure out how to do. Anyone?

      enum PackageType : byte { STANDARD=1, REQUEST=2, ANSWER=3 }

      PackageType type = PackageType.STANDARD;
      byte b = (byte)type; // This results in b==1, I want the ASCII (49)

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      How about make STANDARD=49 ? Other options include applying an attribute to the member: [System.ComponentModel.DescriptionAttribute('1')] STANDARD=1 http://msdn.microsoft.com/en-us/library/system.componentmodel.descriptionattribute(v=vs.110).aspx[^]

      T 1 Reply Last reply
      0
      • T TMattC

        So, I have an enum and want to get the ASCII code of the numeric value of a certain enum value. For example if I have the enum value STANDARD (=1), I want to get the ASCII code for '1' (49) in a byte variable. I cant seem to figure out how to do. Anyone?

        enum PackageType : byte { STANDARD=1, REQUEST=2, ANSWER=3 }

        PackageType type = PackageType.STANDARD;
        byte b = (byte)type; // This results in b==1, I want the ASCII (49)

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        ((int)type).ToString();

        Or, if you want just the first character:

        ((int)type).ToString()[0];

        The difficult we do right away... ...the impossible takes slightly longer.

        P 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          ((int)type).ToString();

          Or, if you want just the first character:

          ((int)type).ToString()[0];

          The difficult we do right away... ...the impossible takes slightly longer.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Or type.ToString("D")

          1 Reply Last reply
          0
          • P PIEBALDconsult

            How about make STANDARD=49 ? Other options include applying an attribute to the member: [System.ComponentModel.DescriptionAttribute('1')] STANDARD=1 http://msdn.microsoft.com/en-us/library/system.componentmodel.descriptionattribute(v=vs.110).aspx[^]

            T Offline
            T Offline
            TMattC
            wrote on last edited by
            #5

            "How about make STANDARD=49 ?" Well, I guess it´s time to call it a night. Lol I´ll make STANDARD=49.

            1 Reply Last reply
            0
            • T TMattC

              So, I have an enum and want to get the ASCII code of the numeric value of a certain enum value. For example if I have the enum value STANDARD (=1), I want to get the ASCII code for '1' (49) in a byte variable. I cant seem to figure out how to do. Anyone?

              enum PackageType : byte { STANDARD=1, REQUEST=2, ANSWER=3 }

              PackageType type = PackageType.STANDARD;
              byte b = (byte)type; // This results in b==1, I want the ASCII (49)

              R Offline
              R Offline
              Rob Philpott
              wrote on last edited by
              #6

              Well if you can change you enum you could do this:

              enum PackageType
              {
              Standard = '1',
              Request = '2',
              Answer = '3'
              }

              And that's job done. If not, just add 48 to 'b'.

              Regards, Rob Philpott.

              1 Reply Last reply
              0
              • T TMattC

                So, I have an enum and want to get the ASCII code of the numeric value of a certain enum value. For example if I have the enum value STANDARD (=1), I want to get the ASCII code for '1' (49) in a byte variable. I cant seem to figure out how to do. Anyone?

                enum PackageType : byte { STANDARD=1, REQUEST=2, ANSWER=3 }

                PackageType type = PackageType.STANDARD;
                byte b = (byte)type; // This results in b==1, I want the ASCII (49)

                D Offline
                D Offline
                Daniel Pfeffer
                wrote on last edited by
                #7

                Yet another option would be a conversion function as follows:

                enum PackageType : byte { STANDARD, REQUEST, ANSWER }

                static readonly byte[] PackageTypeValues = { (byte)'1', (byte)'2', (byte)'3' };

                byte PackageTypeToByte( PackageType packageType )
                {
                return PackageTypeValues[(byte)packageType];
                }

                This has the advantage of separating the enumeration values from their display tokens, allowing you to change one without changing the other. The disadvantage is that they can become unsynchronized...

                1 Reply Last reply
                0
                • T TMattC

                  So, I have an enum and want to get the ASCII code of the numeric value of a certain enum value. For example if I have the enum value STANDARD (=1), I want to get the ASCII code for '1' (49) in a byte variable. I cant seem to figure out how to do. Anyone?

                  enum PackageType : byte { STANDARD=1, REQUEST=2, ANSWER=3 }

                  PackageType type = PackageType.STANDARD;
                  byte b = (byte)type; // This results in b==1, I want the ASCII (49)

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #8

                  Yet another possibility:

                  public enum ByteEnum
                  {
                  FortyNine = (byte)'1',
                  Fifty = (byte)'2',
                  FiftyOne = (byte)'3'
                  }

                  // (int) ByteEnum.Fifty => 50

                  I keep thinking you want something other than an Enum here (a Struct ?), but I am not quite sure what that "other" might be.

                  «I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009

                  1 Reply Last reply
                  0
                  • T TMattC

                    So, I have an enum and want to get the ASCII code of the numeric value of a certain enum value. For example if I have the enum value STANDARD (=1), I want to get the ASCII code for '1' (49) in a byte variable. I cant seem to figure out how to do. Anyone?

                    enum PackageType : byte { STANDARD=1, REQUEST=2, ANSWER=3 }

                    PackageType type = PackageType.STANDARD;
                    byte b = (byte)type; // This results in b==1, I want the ASCII (49)

                    N Offline
                    N Offline
                    Nitin Gupta1
                    wrote on last edited by
                    #9

                    byte[] result = Encoding.ASCII.GetBytes(PackageType.STANDARD.ToString());

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups