Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Pro Visual Basic 2005
| Search | Today's Posts | Mark Forums Read
Pro Visual Basic 2005 For advanced Visual Basic coders working in version 2005. Beginning-level questions will be redirected to other forums, including Beginning VB 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Visual Basic 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2006, 05:23 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default Converting a String to a Byte array

This flew fine in VB6:
Code:
    Dim Text As String
    Dim abytText() As Byte

    Text = "Some Text"
    abytText = InText   ' abytText is now an array of bytes holding the text.
But in VB.NET ’05, using
Code:
    Dim data As String = "Some Stuff"
    Dim aByteText() As Byte = data
    causes a blue warning squiggle under the reference to the variable data. When I hover over it, the tip says, "Value of type 'String' cannot be converted to '1-dimensional array of Byte'."

I believe that if I used a conversion function to type Byte, it would argue because a string can't be recognized as a single-byte value. As they used to say in commercials for Jiff (gender notwithstanding), “What’s a mother to do?”
 
Old December 15th, 2006, 05:50 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Indeed:
Code:
    Dim aByteText() As Byte = CType(data.ToCharArray, Byte)
                                    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
                                    "Value of type '1-dimensional array of char' cannot be converted to 'Byte'."
 
Old February 20th, 2007, 11:03 PM
Authorized User
 
Join Date: Dec 2006
Location: Fort Collins, Colorado, USA.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to maxl
Default

You could use:

Imports system.text

Dim aByteText() as Byte = Encoding.UTF8.GetBytes(data)

Or UTF7, UTF32, Unicode...its all under the Encoding class.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing byte array into image file yukijocelyn C# 2005 1 August 6th, 2008 03:45 PM
Converting int to byte yukijocelyn C# 2005 1 May 30th, 2008 03:44 AM
display byte array of pdf in jsp mahendra.w JSP Basics 2 September 23rd, 2007 11:23 PM
display byte array of jpeg in jsp mahendra.w JSP Basics 1 September 4th, 2007 06:53 AM
converting image in picturebox to byte[] connect2sandep General .NET 0 July 26th, 2005 04:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.