Convert Byte to Char

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

    Convert Byte to Char

    Hello,

    Suppose I have the following C# code which I want to convert to VB:

    for (int i = 0; i < nFieldLength; i++)
    Console.Write(( char) sValue[i]);

    sValue is a byte [] array.

    The problem is the typecast which I can't find an equivalent for in VB. I
    tried using CChar() and CType() but neither of them seemed to work. Instead
    the compiler said it can't convert Byte to Char.

    Thanks in advance.

    -- John


  • Ken Tucker [MVP]

    #2
    Re: Convert Byte to Char

    Hi,

    Take a look at chr(svalue(i))


    Ken
    ------------------
    "John Smith" <john.smith@x-formation.com> wrote in message
    news:OyDSaWV0EH A.4028@TK2MSFTN GP15.phx.gbl...
    Hello,

    Suppose I have the following C# code which I want to convert to VB:

    for (int i = 0; i < nFieldLength; i++)
    Console.Write(( char) sValue[i]);

    sValue is a byte [] array.

    The problem is the typecast which I can't find an equivalent for in VB. I
    tried using CChar() and CType() but neither of them seemed to work. Instead
    the compiler said it can't convert Byte to Char.

    Thanks in advance.

    -- John



    Comment

    • Cor Ligthert

      #3
      Re: Convert Byte to Char

      John,

      Dim a as char = chrw(65)
      dim b as byte = ascw("A")

      For me are the convert functions one of the things which I like very much
      from VBNet.





      I hope this helps?

      Cor

      "John Smith" <john.smith@x-formation.com>
      ....[color=blue]
      > Hello,
      >
      > Suppose I have the following C# code which I want to convert to VB:
      >
      > for (int i = 0; i < nFieldLength; i++)
      > Console.Write(( char) sValue[i]);
      >
      > sValue is a byte [] array.
      >
      > The problem is the typecast which I can't find an equivalent for in VB. I
      > tried using CChar() and CType() but neither of them seemed to work.
      > Instead
      > the compiler said it can't convert Byte to Char.
      >
      > Thanks in advance.
      >
      > -- John
      >
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Convert Byte to Char

        "John Smith" <john.smith@x-formation.com> schrieb:[color=blue]
        > The problem is the typecast which I can't find an equivalent for in VB. I
        > tried using CChar() and CType() but neither of them seemed to work.
        > Instead
        > the compiler said it can't convert Byte to Char.[/color]

        'CChar', 'Chr', 'ChrW', 'Convert.ToChar ', ...

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Mythran

          #5
          Re: Convert Byte to Char


          "John Smith" <john.smith@x-formation.com> wrote in message
          news:OyDSaWV0EH A.4028@TK2MSFTN GP15.phx.gbl...[color=blue]
          > Hello,
          >
          > Suppose I have the following C# code which I want to convert to VB:
          >
          > for (int i = 0; i < nFieldLength; i++)
          > Console.Write(( char) sValue[i]);
          >
          > sValue is a byte [] array.
          >
          > The problem is the typecast which I can't find an equivalent for in VB. I
          > tried using CChar() and CType() but neither of them seemed to work.
          > Instead
          > the compiler said it can't convert Byte to Char.
          >
          > Thanks in advance.
          >
          > -- John
          >
          >[/color]

          Dim s As String = "Hello World!"
          Dim bytes As Byte() = System.Text.ASC IIEncoding.ASCI I.GetBytes(s)
          Dim output As String = System.Text.ASC IIEncoding.ASCI I.GetString(byt es)
          Console.WriteLi ne(output)

          Mythran


          Comment

          Working...