reading comma separated numbers.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pesso

    #1

    reading comma separated numbers.

    I have a string that contains the following:
    string s = "130,41,43,178, 41,17,6,78,244, 35,202,144,115" ;

    They are comma separated byte numbers, and I need to
    initialize my byte[] array with them. Looks like I need a loop
    or something. What would be the best way to take a
    comma separated string to initialize a byte[] array using cs?



  • Jon Skeet

    #2
    Re: reading comma separated numbers.

    pesso <no@where.com > wrote:[color=blue]
    > I have a string that contains the following:
    > string s = "130,41,43,178, 41,17,6,78,244, 35,202,144,115" ;
    >
    > They are comma separated byte numbers, and I need to
    > initialize my byte[] array with them. Looks like I need a loop
    > or something. What would be the best way to take a
    > comma separated string to initialize a byte[] array using cs?[/color]

    string[] numbers = s.Split(',');
    byte[] bytes = new byte[numbers];
    for (int i=0; i < numbers.Length; i++)
    {
    bytes[i] = Byte.Parse(numb ers[i]);
    }

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Jeffrey Tan[MSFT]

      #3
      Re: reading comma separated numbers.


      Yes, you are right, but there is a small change in your code.
      I marked it inline.
      Thanks,

      --------------------
      | From: Jon Skeet <skeet@pobox.co m>
      | Subject: Re: reading comma separated numbers.
      | Date: Sat, 20 Sep 2003 10:32:36 +0100
      | Message-ID: <MPG.19d62f0b71 4cea029896fd@ne ws.microsoft.co m>
      | References: <fxTab.146135$0 v4.10796825@bgt nsc04-news.ops.worldn et.att.net>
      | MIME-Version: 1.0
      | Content-Type: text/plain; charset="iso-8859-1"
      | Content-Transfer-Encoding: 7bit
      | X-Newsreader: MicroPlanet Gravity v2.60
      | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
      | NNTP-Posting-Host: pc2-rdng5-6-cust18.winn.cab le.ntl.com 81.103.153.18
      | Lines: 1
      | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
      | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1862 57
      | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
      |
      | pesso <no@where.com > wrote:
      | > I have a string that contains the following:
      | > string s = "130,41,43,178, 41,17,6,78,244, 35,202,144,115" ;
      | >
      | > They are comma separated byte numbers, and I need to
      | > initialize my byte[] array with them. Looks like I need a loop
      | > or something. What would be the best way to take a
      | > comma separated string to initialize a byte[] array using cs?
      |
      | string[] numbers = s.Split(',');
      | byte[] bytes = new byte[numbers];
      ~~~~~~~numbers. Length
      | for (int i=0; i < numbers.Length; i++)
      | {
      | bytes[i] = Byte.Parse(numb ers[i]);
      | }
      |
      | --
      | Jon Skeet - <skeet@pobox.co m>
      | http://www.pobox.com/~skeet
      | If replying to the group, please do not mail me too
      |


      Jeffrey Tan
      Microsoft Online Partner Support
      Get Secure! - www.microsoft.com/security
      This posting is provided "as is" with no warranties and confers no rights.

      Comment

      Working...