Results 1 to 6 of 6

Thread: [RESOLVED] Why won't Convert.ToByte convert 128?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2015
    Location
    Connecticut, USA
    Posts
    147

    Resolved [RESOLVED] Why won't Convert.ToByte convert 128?

    This is odd...

    I have a function that breaks a string into individual characters then converts the chars to a byte array. It's used for sending serial data to an external device.


    Code:
    Dim msg As String = Chr(241) + Chr(128) + Chr(0) + Chr(129) ' character string
    SndMsg32(msg)
    
     Function SndMsg32(ByRef MsgData As String) As Boolean
    
            Dim bChar As Byte
            Dim i As Short
            Dim Z As Char
            Dim SndBuf(256) As Byte 'array of bytes to hold message
    
    
            For i = 1 To Len(MsgData)
                Z = Mid(MsgData, i, 1)  'Mid is 1 based
                bChar = System.Convert.ToByte(Z)  ' convert char back to byte
                SndBuf(i-1) = bChar  'byte array is zero based
            Next i
    
            SndMsg32 = True
        End Function
    VB2008 and 2010 throw an exception if the value of one of those chars is 128. Zero is OK, and so are 127 and 129, but 128 gets caught with a message - "Value was either too large or too small for an unsigned byte."

    So, what's up with the number 128?

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Why won't Convert.ToByte convert 128?

    The Chr method works with the ANSI Code Page that is in effect for the current thread that you code is running on. In the Western hemisphere, it is most often Windows-1252. Windows-1252 places the Euro sign at character code 128.

    VB stores your string as a sequence of Char objects. In VB, Char objects correspond to Unicode code points in the Unicode BMP. They are 16 bit values. The Euro sign is mapped to Unicode code point U+20AC, and that is the value that Chr(128) is stored as internally in your string.

    The System.Convert.ToByte Method works with the 16 bit Char values, so you are basically trying to stuff the value 0x20AC into an unsigned Byte, and it won't fit.

    Try using bChar = Asc(Z) instead.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Why won't Convert.ToByte convert 128?

    I do not have this error when I compile this:
    Code:
    Imports System
    Public Module Module1
    	Public Sub Main()
    		Console.WriteLine(Convert.ToByte(128))
    	End Sub
    End Module
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Why won't Convert.ToByte convert 128?

    That's because the overload of the Convert.ToByte method you are using there is the one that takes a 32 bit Integer as an argument (not a Char), and 128 as an Integer will happily fit into an unsigned Byte.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Why won't Convert.ToByte convert 128?

    Ah, ok. That makes sense then.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2015
    Location
    Connecticut, USA
    Posts
    147

    Re: Why won't Convert.ToByte convert 128?

    Quote Originally Posted by Inferrd View Post
    The Chr method works with the ANSI Code Page that is in effect for the current thread that you code is running on. In the Western hemisphere, it is most often Windows-1252. Windows-1252 places the Euro sign at character code 128.

    VB stores your string as a sequence of Char objects. In VB, Char objects correspond to Unicode code points in the Unicode BMP. They are 16 bit values. The Euro sign is mapped to Unicode code point U+20AC, and that is the value that Chr(128) is stored as internally in your string.

    The System.Convert.ToByte Method works with the 16 bit Char values, so you are basically trying to stuff the value 0x20AC into an unsigned Byte, and it won't fit.

    Try using bChar = Asc(Z) instead.
    Thanks. That worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width