2

This is probably a very ridiculous question but I can't figure out how to import the user's input into a byte array. Since user input is always in a string format a conversation will be needed, however the numbers he entered must be the same in the array. For example:

Console.Write("Enter a number: ");
string text = Console.ReadLine();

/* Lets assume the user entered 22, 101, 1
   How would I get those exact numbers in byte[]
*/

byte[] arr = new Byte[] {text};

UPDATED: what I am looking to get is

byte[] arr = new Byte[] {22, 101, 1};
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
HereToLearn
  • 292
  • 5
  • 16
  • are you looking for the array to contain the integers 22,101,1 or ascii equivalent bytes to the bytes of 22. Which would be 50,50 (for "22") http://www.asciitable.com/ – Matt Johnson Mar 11 '16 at 01:16
  • What I am actually looking to get is byte[] arr = new Byte[] {22, 101, 1}; – HereToLearn Mar 11 '16 at 01:23

4 Answers4

2
Console.Write("Enter a number: ");
string text = Console.ReadLine();
byte[] byteArrayUTF= System.Text.Encoding.UTF8.GetBytes (text);
byte[] byteArrayASCII= System.Text.Encoding.ASCII.GetBytes (text);

You can find a better explanation here for the difference between UTF8 and ASCII

Unicode is a superset of ASCII, and the numbers 0–128 have the same meaning in ASCII as they have in Unicode.

(From the updates)I think You are actually looking for converting the input string (comma separated values ) into an array of bytes. if so you can use thm Like the following:

 string text = Console.ReadLine();
 byte[] byteArr = text.Split(',').Select(x => Convert.ToByte(x)).ToArray();   
Community
  • 1
  • 1
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • Your solution worked great, thank you very much. I was wondering if it is possible to modify byte[] byteArr = text.Split(',').Select(x => Convert.ToByte(x)).ToArray(); So that it will accept input strings with + signs. For example, 1+2, 3+6, 33+2 ? – HereToLearn Mar 11 '16 at 03:53
1

While the other answers explain how to convert a string to a byte[],
I believe what you really want is to get the numbers into a byte[]

var input = "22, 101, 1";
var numbers = input.Split(',')
                   .Select(p => byte.Parse(p));

This will split the input at every , and convert each part to an integer.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
TimothyP
  • 21,178
  • 26
  • 94
  • 142
1

This uses LINQ-to-Objects to "pipeline" from a string to an array of strings to a sequence of bytes to an array of bytes:

var arr = text
    .Split(',')
    .Select(digits => Byte.Parse(digits))
    .ToArray();
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
0

You can use the GetBytes function to convert strings into bytes.

byte[] arr = Encoding.ASCII.GetBytes(text)
byte[] arr = System.Text.UTF8Encoding.UTF8.GetBytes(text)
byte[] arr = Encoding.Unicode.GetBytes(text)

If you are looking for a specific encoding type you can use the following to convert from other standard encoding types into what .NET provides.

byte[] arr = GetStringEncoding(StringEncodingType.CHARSET_UTF_16BE_CODEPAGE).GetBytes(text);

public static Encoding GetStringEncoding(StringEncodingType type)
        {
             switch (type)
            {
                case StringEncodingType.ASCII:
                    return Encoding.ASCII;
                case StringEncodingType.Unicode:
                    return Encoding.Unicode;
                    //return Encoding.UTF7;
                case StringEncodingType.UTF8:
                    return Encoding.UTF8;
                //case StringEncodingType.CHARSET_UTF_16LE_CODEPAGE:// 1200, same as unicode
                case StringEncodingType.CHARSET_UTF_16BE_CODEPAGE:// 1201,
                case StringEncodingType.CHARSET_UTF_32LE_CODEPAGE:// 12000,
                case StringEncodingType.CHARSET_UTF_32BE_CODEPAGE:// 12001,
                case StringEncodingType.CHARSET_WINDOWS_1251_CODEPAGE:// 1251,
                case StringEncodingType.CHARSET_WINDOWS_1252_CODEPAGE:// 1252,
                case StringEncodingType.CHARSET_WINDOWS_1253_CODEPAGE:// 1253,
                case StringEncodingType.CHARSET_WINDOWS_1255_CODEPAGE:// 1255,
                case StringEncodingType.CHARSET_ISO_2022_JP_CODEPAGE:// 50220,
                case StringEncodingType.CHARSET_ISO_2022_CN_CODEPAGE:// 50227,
                case StringEncodingType.CHARSET_ISO_2022_KR_CODEPAGE:// 50225,
                case StringEncodingType.CHARSET_ISO_8859_5_CODEPAGE:// 28595,
                case StringEncodingType.CHARSET_ISO_8859_7_CODEPAGE:// 28597,
                case StringEncodingType.CHARSET_ISO_8859_8_CODEPAGE:// 28598,
                case StringEncodingType.CHARSET_BIG5_CODEPAGE:// 950,
                case StringEncodingType.CHARSET_GB18030_CODEPAGE:// 54936,
                case StringEncodingType.CHARSET_EUC_JP_CODEPAGE:// 20932,
                case StringEncodingType.CHARSET_EUC_KR_CODEPAGE:// 51949,
                case StringEncodingType.CHARSET_SHIFT_JIS_CODEPAGE:// 932,
                case StringEncodingType.CHARSET_IBM855_CODEPAGE:// 855,
                case StringEncodingType.CHARSET_IBM866_CODEPAGE:// 866,
                case StringEncodingType.CHARSET_KOI8_R_CODEPAGE:// 20866,
                case StringEncodingType.CHARSET_MACCYRILLIC_CODEPAGE:// 10007,
                case StringEncodingType.CHARSET_HZ_GB_2312_CODEPAGE:// 52936,
                //case StringEncodingType.CHARSET_X_ISO_10646_UCS_4_3412_CODEPAGE:// 12000, same as CHARSET_UTF_32LE_CODEPAGE
                //case StringEncodingType.CHARSET_X_ISO_10646_UCS_4_2143_CODEPAGE:// 12000, same as CHARSET_UTF_32LE_CODEPAGE
                case StringEncodingType.CHARSET_WINDOWS_874_CODEPAGE:// 874
                    return Encoding.GetEncoding((int)type);
                default:
                    throw new System.NotSupportedException("Error Missing String Encoding Type:" + type.ToString());
            }
        }



public enum StringEncodingType // the numbers are the codepage
    {
        Unknown = -1,
        ASCII = 20127,
        Unicode = 1200,
        UTF8 = 65001,
        //CHARSET_UTF_16LE_CODEPAGE = 1200,  same as unicode
        CHARSET_UTF_16BE_CODEPAGE = 1201,
        CHARSET_UTF_32LE_CODEPAGE = 12000,
        CHARSET_UTF_32BE_CODEPAGE = 12001,
        CHARSET_WINDOWS_1251_CODEPAGE = 1251,
        CHARSET_WINDOWS_1252_CODEPAGE = 1252,
        CHARSET_WINDOWS_1253_CODEPAGE = 1253,
        CHARSET_WINDOWS_1255_CODEPAGE = 1255,
        CHARSET_ISO_2022_JP_CODEPAGE = 50220,
        CHARSET_ISO_2022_CN_CODEPAGE = 50227,
        CHARSET_ISO_2022_KR_CODEPAGE = 50225,
        CHARSET_ISO_8859_5_CODEPAGE = 28595,
        CHARSET_ISO_8859_7_CODEPAGE = 28597,
        CHARSET_ISO_8859_8_CODEPAGE = 28598,
        CHARSET_BIG5_CODEPAGE = 950,
        CHARSET_GB18030_CODEPAGE = 54936,
        CHARSET_EUC_JP_CODEPAGE = 20932,
        CHARSET_EUC_KR_CODEPAGE = 51949,
        CHARSET_SHIFT_JIS_CODEPAGE = 932,
        CHARSET_IBM855_CODEPAGE = 855,
        CHARSET_IBM866_CODEPAGE = 866,
        CHARSET_KOI8_R_CODEPAGE = 20866,
        CHARSET_MACCYRILLIC_CODEPAGE = 10007,
        CHARSET_HZ_GB_2312_CODEPAGE = 52936,
        //CHARSET_X_ISO_10646_UCS_4_3412_CODEPAGE = 12000, same as CHARSET_UTF_32LE_CODEPAGE
        //CHARSET_X_ISO_10646_UCS_4_2143_CODEPAGE = 12000, same as CHARSET_UTF_32LE_CODEPAGE
        CHARSET_WINDOWS_874_CODEPAGE = 874
    }
Matt Johnson
  • 1,913
  • 16
  • 23