Convert a string to byte array 
Author Message
 Convert a string to byte array

Hi
How is a string converted to an array of Byte , like which is required by
the first argument of the FileStream's Write method (array as Byte() ) ?


Fri, 02 Apr 2004 05:35:27 GMT  
 Convert a string to byte array
Simple, inefficient way:

Function StringToBytes(inString as String) As Byte()
  Dim TempBytes() As Byte
  For i = 0 to Len(inString) - 1
    Redim Preserve TempBytes(i)
    TempBytes(i) = Asc(Mid(inString, i + 1, 1))
  Next
  Return TempBytes
End Function

Why don't you declare a StreamWriter or TextWriter instead, though?

Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
Dim wt As System.IO.TextWriter = New System.IO.TextWriter

Cromas


Quote:
> Hi
> How is a string converted to an array of Byte , like which is required by
> the first argument of the FileStream's Write method (array as Byte() ) ?



Fri, 02 Apr 2004 08:45:57 GMT  
 Convert a string to byte array
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemtextencodingclassgetbytest
opic5.htm

Ronald Laeremans
Visual C++ compiler team


Quote:
> Simple, inefficient way:

> Function StringToBytes(inString as String) As Byte()
>   Dim TempBytes() As Byte
>   For i = 0 to Len(inString) - 1
>     Redim Preserve TempBytes(i)
>     TempBytes(i) = Asc(Mid(inString, i + 1, 1))
>   Next
>   Return TempBytes
> End Function

> Why don't you declare a StreamWriter or TextWriter instead, though?

> Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
> Dim wt As System.IO.TextWriter = New System.IO.TextWriter

> Cromas



> > Hi
> > How is a string converted to an array of Byte , like which is required
by
> > the first argument of the FileStream's Write method (array as Byte() ) ?



Fri, 02 Apr 2004 09:39:32 GMT  
 Convert a string to byte array
Imports System.Text.Encoder

Dim oEncoder As New System.Text.ASCIIEncoding()
Dim bytes as byte()=oEncoder.GetBytes(myString)

HTH
Corrado



Fri, 02 Apr 2004 20:00:34 GMT  
 Convert a string to byte array
Thanx for the link, it increased my knowledge but I'm having another problem
and still not succeeded in my conversion function ::
I inherited a class from System.Text.Encoding and overrided its 6
mustoverride functions. Then I created the object of my new class and did
the following:

Dim str As String = "hello world"
Dim varsys As New bytestr() 'bytsstr is my new class  which inherits from
Encoding
Dim bstr(str.Length) As Byte
bstr = varsys.GetBytes(str)

The above code should return the appropriate bytes array, but nothing goes
into the bstr. Whats wrong here ?



ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemtextencodingclassgetbytest

Quote:
> opic5.htm

> Ronald Laeremans
> Visual C++ compiler team



> > Simple, inefficient way:

> > Function StringToBytes(inString as String) As Byte()
> >   Dim TempBytes() As Byte
> >   For i = 0 to Len(inString) - 1
> >     Redim Preserve TempBytes(i)
> >     TempBytes(i) = Asc(Mid(inString, i + 1, 1))
> >   Next
> >   Return TempBytes
> > End Function

> > Why don't you declare a StreamWriter or TextWriter instead, though?

> > Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
> > Dim wt As System.IO.TextWriter = New System.IO.TextWriter

> > Cromas



> > > Hi
> > > How is a string converted to an array of Byte , like which is required
> by
> > > the first argument of the FileStream's Write method (array as Byte() )
?



Sat, 03 Apr 2004 03:25:50 GMT  
 Convert a string to byte array
Why on earth are you creating your own encoding? Why not use an existing
encodign object?

--
MichKa

Michael Kaplan
(principal developer of the MSLU)
Trigeminal Software, Inc.  -- http://www.trigeminal.com/
the book -- http://www.i18nWithVB.com/


Quote:
> Thanx for the link, it increased my knowledge but I'm having another
problem
> and still not succeeded in my conversion function ::
> I inherited a class from System.Text.Encoding and overrided its 6
> mustoverride functions. Then I created the object of my new class and did
> the following:

> Dim str As String = "hello world"
> Dim varsys As New bytestr() 'bytsstr is my new class  which inherits from
> Encoding
> Dim bstr(str.Length) As Byte
> bstr = varsys.GetBytes(str)

> The above code should return the appropriate bytes array, but nothing goes
> into the bstr. Whats wrong here ?




ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemtextencodingclassgetbytest

- Show quoted text -

Quote:
> > opic5.htm

> > Ronald Laeremans
> > Visual C++ compiler team



> > > Simple, inefficient way:

> > > Function StringToBytes(inString as String) As Byte()
> > >   Dim TempBytes() As Byte
> > >   For i = 0 to Len(inString) - 1
> > >     Redim Preserve TempBytes(i)
> > >     TempBytes(i) = Asc(Mid(inString, i + 1, 1))
> > >   Next
> > >   Return TempBytes
> > > End Function

> > > Why don't you declare a StreamWriter or TextWriter instead, though?

> > > Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
> > > Dim wt As System.IO.TextWriter = New System.IO.TextWriter

> > > Cromas



> > > > Hi
> > > > How is a string converted to an array of Byte , like which is
required
> > by
> > > > the first argument of the FileStream's Write method (array as
Byte() )
> ?



Sat, 03 Apr 2004 03:43:43 GMT  
 Convert a string to byte array
THANX :)

Quote:
> Imports System.Text.Encoder

> Dim oEncoder As New System.Text.ASCIIEncoding()
> Dim bytes as byte()=oEncoder.GetBytes(myString)

> HTH
> Corrado



Sat, 03 Apr 2004 03:39:35 GMT  
 Convert a string to byte array
Well, hopefully you are working only in English, EVER -- since the Encoding
object ytou picked will not support anything else. :-(

--
MichKa

Michael Kaplan
(principal developer of the MSLU)
Trigeminal Software, Inc.  -- http://www.trigeminal.com/
the book -- http://www.i18nWithVB.com/


Quote:
> THANX :)


> > Imports System.Text.Encoder

> > Dim oEncoder As New System.Text.ASCIIEncoding()
> > Dim bytes as byte()=oEncoder.GetBytes(myString)

> > HTH
> > Corrado



Sat, 03 Apr 2004 03:49:42 GMT  
 Convert a string to byte array
Hey I'm not trying to create my own encoding, the article/page of the .NET
help which Ronald Laermans[msft] referred me to pointed to the getbytes
method of the system.text.encoding class. When I started to use it the
compiler gave a check warning that I cannot create the object of a class
declared with MustInherit, so ofcourse if I want to use any of the methods
of the system.text.encoding class I'll have to derive it into some of my own
custom class. But the story doesnt end here, if the class is mustinherit
then there is a high probability that there'll also be some MustOverride
methods there, and indeed there are, and they r 6 of them. And the rest of
it I mentioned in my previous post. So far I've got the answer from Corrado
who have done it through system.text.encoder, not system.text.encoding, and
then u said that It'll only work with English, I think r u talking about
system.text.encoder. And still, I want to know that Am I doing right by
inheriting and then using it, and how do I get the byte array from
system.text.encoding's GetBytes method ?
 - Abubakar.


Quote:
> Why on earth are you creating your own encoding? Why not use an existing
> encodign object?

> --
> MichKa

> Michael Kaplan
> (principal developer of the MSLU)
> Trigeminal Software, Inc.  -- http://www.trigeminal.com/
> the book -- http://www.i18nWithVB.com/



> > Thanx for the link, it increased my knowledge but I'm having another
> problem
> > and still not succeeded in my conversion function ::
> > I inherited a class from System.Text.Encoding and overrided its 6
> > mustoverride functions. Then I created the object of my new class and
did
> > the following:

> > Dim str As String = "hello world"
> > Dim varsys As New bytestr() 'bytsstr is my new class  which inherits
from
> > Encoding
> > Dim bstr(str.Length) As Byte
> > bstr = varsys.GetBytes(str)

> > The above code should return the appropriate bytes array, but nothing
goes
> > into the bstr. Whats wrong here ?




ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemtextencodingclassgetbytest

- Show quoted text -

Quote:
> > > opic5.htm

> > > Ronald Laeremans
> > > Visual C++ compiler team



> > > > Simple, inefficient way:

> > > > Function StringToBytes(inString as String) As Byte()
> > > >   Dim TempBytes() As Byte
> > > >   For i = 0 to Len(inString) - 1
> > > >     Redim Preserve TempBytes(i)
> > > >     TempBytes(i) = Asc(Mid(inString, i + 1, 1))
> > > >   Next
> > > >   Return TempBytes
> > > > End Function

> > > > Why don't you declare a StreamWriter or TextWriter instead, though?

> > > > Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
> > > > Dim wt As System.IO.TextWriter = New System.IO.TextWriter

> > > > Cromas



> > > > > Hi
> > > > > How is a string converted to an array of Byte , like which is
> required
> > > by
> > > > > the first argument of the FileStream's Write method (array as
> Byte() )
> > ?



Sat, 03 Apr 2004 07:08:13 GMT  
 Convert a string to byte array
Hey I'm not trying to create my own encoding, the article/page of the .NET
help which Ronald Laermans[msft] referred me to pointed to the getbytes
method of the system.text.encoding class. When I started to use it the
compiler gave a check warning that I cannot create the object of a class
declared with MustInherit, so ofcourse if I want to use any of the methods
of the system.text.encoding class I'll have to derive it into some of my own
custom class. But the story doesnt end here, if the class is mustinherit
then there is a high probability that there'll also be some MustOverride
methods there, and indeed there are, and they r 6 of them. And the rest of
it I mentioned in my previous post. So far I've got the answer from Corrado
who have done it through system.text.encoder, not system.text.encoding, and
then u said that It'll only work with English, I guess u r talking about
encoder class, and still, I want to know that Am I doing right by inheriting
and then using it, and how do I get the byte array from
system.text.encoding's getbytes method that takes a string and returns byte
array.
 - Abubakar.


Quote:
> Why on earth are you creating your own encoding? Why not use an existing
> encodign object?

> --
> MichKa

> Michael Kaplan
> (principal developer of the MSLU)
> Trigeminal Software, Inc.  -- http://www.trigeminal.com/
> the book -- http://www.i18nWithVB.com/



> > Thanx for the link, it increased my knowledge but I'm having another
> problem
> > and still not succeeded in my conversion function ::
> > I inherited a class from System.Text.Encoding and overrided its 6
> > mustoverride functions. Then I created the object of my new class and
did
> > the following:

> > Dim str As String = "hello world"
> > Dim varsys As New bytestr() 'bytsstr is my new class  which inherits
from
> > Encoding
> > Dim bstr(str.Length) As Byte
> > bstr = varsys.GetBytes(str)

> > The above code should return the appropriate bytes array, but nothing
goes
> > into the bstr. Whats wrong here ?




ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemtextencodingclassgetbytest

- Show quoted text -

Quote:
> > > opic5.htm

> > > Ronald Laeremans
> > > Visual C++ compiler team



> > > > Simple, inefficient way:

> > > > Function StringToBytes(inString as String) As Byte()
> > > >   Dim TempBytes() As Byte
> > > >   For i = 0 to Len(inString) - 1
> > > >     Redim Preserve TempBytes(i)
> > > >     TempBytes(i) = Asc(Mid(inString, i + 1, 1))
> > > >   Next
> > > >   Return TempBytes
> > > > End Function

> > > > Why don't you declare a StreamWriter or TextWriter instead, though?

> > > > Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
> > > > Dim wt As System.IO.TextWriter = New System.IO.TextWriter

> > > > Cromas



> > > > > Hi
> > > > > How is a string converted to an array of Byte , like which is
> required
> > > by
> > > > > the first argument of the FileStream's Write method (array as
> Byte() )
> > ?



Sat, 03 Apr 2004 21:12:51 GMT  
 Convert a string to byte array
System.Text.Encoding is indeed an abstract class.  However, there are
already several implementation classes that are ready for you to use
(ASCIIEncoding, UnicodeEncoding, UTF7Encoding, and UTF8Encoding).  Try
one of those.


Quote:
> Hey I'm not trying to create my own encoding, the article/page of the .NET
> help which Ronald Laermans[msft] referred me to pointed to the getbytes
> method of the system.text.encoding class. When I started to use it the
> compiler gave a check warning that I cannot create the object of a class
> declared with MustInherit, so ofcourse if I want to use any of the methods
> of the system.text.encoding class I'll have to derive it into some of my own
> custom class. But the story doesnt end here, if the class is mustinherit
> then there is a high probability that there'll also be some MustOverride
> methods there, and indeed there are, and they r 6 of them. And the rest of
> it I mentioned in my previous post. So far I've got the answer from Corrado
> who have done it through system.text.encoder, not system.text.encoding, and
> then u said that It'll only work with English, I think r u talking about
> system.text.encoder. And still, I want to know that Am I doing right by
> inheriting and then using it, and how do I get the byte array from
> system.text.encoding's GetBytes method ?
>  - Abubakar.

Tim


Sat, 03 Apr 2004 22:41:30 GMT  
 Convert a string to byte array
You would actually create an Encoding object from either one of the ones
alreaday there in System.Text.Encoding or throuigh
System.Text.Encoding.GetEncoding(<code page>) if none of the built in ones
would work.

This is how you would use an existing one without creating a new one. :-)

--
MichKa

Michael Kaplan
(principal developer of the MSLU)
Trigeminal Software, Inc.  -- http://www.trigeminal.com/
the book -- http://www.i18nWithVB.com/


Quote:
> Hey I'm not trying to create my own encoding, the article/page of the .NET
> help which Ronald Laermans[msft] referred me to pointed to the getbytes
> method of the system.text.encoding class. When I started to use it the
> compiler gave a check warning that I cannot create the object of a class
> declared with MustInherit, so ofcourse if I want to use any of the methods
> of the system.text.encoding class I'll have to derive it into some of my
own
> custom class. But the story doesnt end here, if the class is mustinherit
> then there is a high probability that there'll also be some MustOverride
> methods there, and indeed there are, and they r 6 of them. And the rest of
> it I mentioned in my previous post. So far I've got the answer from
Corrado
> who have done it through system.text.encoder, not system.text.encoding,
and
> then u said that It'll only work with English, I guess u r talking about
> encoder class, and still, I want to know that Am I doing right by
inheriting
> and then using it, and how do I get the byte array from
> system.text.encoding's getbytes method that takes a string and returns
byte
> array.
>  - Abubakar.

in

> > Why on earth are you creating your own encoding? Why not use an existing
> > encodign object?

> > --
> > MichKa

> > Michael Kaplan
> > (principal developer of the MSLU)
> > Trigeminal Software, Inc.  -- http://www.trigeminal.com/
> > the book -- http://www.i18nWithVB.com/



> > > Thanx for the link, it increased my knowledge but I'm having another
> > problem
> > > and still not succeeded in my conversion function ::
> > > I inherited a class from System.Text.Encoding and overrided its 6
> > > mustoverride functions. Then I created the object of my new class and
> did
> > > the following:

> > > Dim str As String = "hello world"
> > > Dim varsys As New bytestr() 'bytsstr is my new class  which inherits
> from
> > > Encoding
> > > Dim bstr(str.Length) As Byte
> > > bstr = varsys.GetBytes(str)

> > > The above code should return the appropriate bytes array, but nothing
> goes
> > > into the bstr. Whats wrong here ?




ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemtextencodingclassgetbytest

- Show quoted text -

Quote:
> > > > opic5.htm

> > > > Ronald Laeremans
> > > > Visual C++ compiler team



> > > > > Simple, inefficient way:

> > > > > Function StringToBytes(inString as String) As Byte()
> > > > >   Dim TempBytes() As Byte
> > > > >   For i = 0 to Len(inString) - 1
> > > > >     Redim Preserve TempBytes(i)
> > > > >     TempBytes(i) = Asc(Mid(inString, i + 1, 1))
> > > > >   Next
> > > > >   Return TempBytes
> > > > > End Function

> > > > > Why don't you declare a StreamWriter or TextWriter instead,
though?

> > > > > Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
> > > > > Dim wt As System.IO.TextWriter = New System.IO.TextWriter

> > > > > Cromas



> > > > > > Hi
> > > > > > How is a string converted to an array of Byte , like which is
> > required
> > > > by
> > > > > > the first argument of the FileStream's Write method (array as
> > Byte() )
> > > ?



Sat, 03 Apr 2004 22:43:22 GMT  
 Convert a string to byte array
I answer the questions that I see and know the answers to, and that I have
some sort of interest in. :-)

Others are much the same here.... no {*filter*} to only answer some
questions.....

--
MichKa

Michael Kaplan
(principal developer of the MSLU)
Trigeminal Software, Inc.  -- http://www.trigeminal.com/
the book -- http://www.i18nWithVB.com/


Quote:
> ok thanx. And can u answer one more thing, its that immediately next
> question to this one is my question about creating/using XP's new common
> controls in VB.NET and VB6, can u get somebody to answer those? Why isnt
ne1
> answering that question?
>  - Abubakar.


in

> > You would actually create an Encoding object from either one of the ones
> > alreaday there in System.Text.Encoding or throuigh
> > System.Text.Encoding.GetEncoding(<code page>) if none of the built in
ones
> > would work.

> > This is how you would use an existing one without creating a new one.
:-)

> > --
> > MichKa

> > Michael Kaplan
> > (principal developer of the MSLU)
> > Trigeminal Software, Inc.  -- http://www.trigeminal.com/
> > the book -- http://www.i18nWithVB.com/



> > > Hey I'm not trying to create my own encoding, the article/page of the
> .NET
> > > help which Ronald Laermans[msft] referred me to pointed to the
getbytes
> > > method of the system.text.encoding class. When I started to use it the
> > > compiler gave a check warning that I cannot create the object of a
class
> > > declared with MustInherit, so ofcourse if I want to use any of the
> methods
> > > of the system.text.encoding class I'll have to derive it into some of
my
> > own
> > > custom class. But the story doesnt end here, if the class is
mustinherit
> > > then there is a high probability that there'll also be some
MustOverride
> > > methods there, and indeed there are, and they r 6 of them. And the
rest
> of
> > > it I mentioned in my previous post. So far I've got the answer from
> > Corrado
> > > who have done it through system.text.encoder, not

system.text.encoding,

- Show quoted text -

Quote:
> > and
> > > then u said that It'll only work with English, I guess u r talking
about
> > > encoder class, and still, I want to know that Am I doing right by
> > inheriting
> > > and then using it, and how do I get the byte array from
> > > system.text.encoding's getbytes method that takes a string and returns
> > byte
> > > array.
> > >  - Abubakar.

> wrote
> > in

> > > > Why on earth are you creating your own encoding? Why not use an
> existing
> > > > encodign object?

> > > > --
> > > > MichKa

> > > > Michael Kaplan
> > > > (principal developer of the MSLU)
> > > > Trigeminal Software, Inc.  -- http://www.trigeminal.com/
> > > > the book -- http://www.i18nWithVB.com/



> > > > > Thanx for the link, it increased my knowledge but I'm having
another
> > > > problem
> > > > > and still not succeeded in my conversion function ::
> > > > > I inherited a class from System.Text.Encoding and overrided its 6
> > > > > mustoverride functions. Then I created the object of my new class
> and
> > > did
> > > > > the following:

> > > > > Dim str As String = "hello world"
> > > > > Dim varsys As New bytestr() 'bytsstr is my new class  which
inherits
> > > from
> > > > > Encoding
> > > > > Dim bstr(str.Length) As Byte
> > > > > bstr = varsys.GetBytes(str)

> > > > > The above code should return the appropriate bytes array, but
> nothing
> > > goes
> > > > > into the bstr. Whats wrong here ?




ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemtextencodingclassgetbytest

- Show quoted text -

Quote:
> > > > > > opic5.htm

> > > > > > Ronald Laeremans
> > > > > > Visual C++ compiler team



> > > > > > > Simple, inefficient way:

> > > > > > > Function StringToBytes(inString as String) As Byte()
> > > > > > >   Dim TempBytes() As Byte
> > > > > > >   For i = 0 to Len(inString) - 1
> > > > > > >     Redim Preserve TempBytes(i)
> > > > > > >     TempBytes(i) = Asc(Mid(inString, i + 1, 1))
> > > > > > >   Next
> > > > > > >   Return TempBytes
> > > > > > > End Function

> > > > > > > Why don't you declare a StreamWriter or TextWriter instead,
> > though?

> > > > > > > Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
> > > > > > > Dim wt As System.IO.TextWriter = New System.IO.TextWriter

> > > > > > > Cromas



> > > > > > > > Hi
> > > > > > > > How is a string converted to an array of Byte , like which
is
> > > > required
> > > > > > by
> > > > > > > > the first argument of the FileStream's Write method (array
as
> > > > Byte() )
> > > > > ?



Sun, 04 Apr 2004 00:49:55 GMT  
 Convert a string to byte array
ok thanx. And can u answer one more thing, its that immediately next
question to this one is my question about creating/using XP's new common
controls in VB.NET and VB6, can u get somebody to answer those? Why isnt ne1
answering that question?
 - Abubakar.



Quote:
> You would actually create an Encoding object from either one of the ones
> alreaday there in System.Text.Encoding or throuigh
> System.Text.Encoding.GetEncoding(<code page>) if none of the built in ones
> would work.

> This is how you would use an existing one without creating a new one. :-)

> --
> MichKa

> Michael Kaplan
> (principal developer of the MSLU)
> Trigeminal Software, Inc.  -- http://www.trigeminal.com/
> the book -- http://www.i18nWithVB.com/



> > Hey I'm not trying to create my own encoding, the article/page of the
.NET
> > help which Ronald Laermans[msft] referred me to pointed to the getbytes
> > method of the system.text.encoding class. When I started to use it the
> > compiler gave a check warning that I cannot create the object of a class
> > declared with MustInherit, so ofcourse if I want to use any of the
methods
> > of the system.text.encoding class I'll have to derive it into some of my
> own
> > custom class. But the story doesnt end here, if the class is mustinherit
> > then there is a high probability that there'll also be some MustOverride
> > methods there, and indeed there are, and they r 6 of them. And the rest
of
> > it I mentioned in my previous post. So far I've got the answer from
> Corrado
> > who have done it through system.text.encoder, not system.text.encoding,
> and
> > then u said that It'll only work with English, I guess u r talking about
> > encoder class, and still, I want to know that Am I doing right by
> inheriting
> > and then using it, and how do I get the byte array from
> > system.text.encoding's getbytes method that takes a string and returns
> byte
> > array.
> >  - Abubakar.

wrote
> in

> > > Why on earth are you creating your own encoding? Why not use an
existing
> > > encodign object?

> > > --
> > > MichKa

> > > Michael Kaplan
> > > (principal developer of the MSLU)
> > > Trigeminal Software, Inc.  -- http://www.trigeminal.com/
> > > the book -- http://www.i18nWithVB.com/



> > > > Thanx for the link, it increased my knowledge but I'm having another
> > > problem
> > > > and still not succeeded in my conversion function ::
> > > > I inherited a class from System.Text.Encoding and overrided its 6
> > > > mustoverride functions. Then I created the object of my new class
and
> > did
> > > > the following:

> > > > Dim str As String = "hello world"
> > > > Dim varsys As New bytestr() 'bytsstr is my new class  which inherits
> > from
> > > > Encoding
> > > > Dim bstr(str.Length) As Byte
> > > > bstr = varsys.GetBytes(str)

> > > > The above code should return the appropriate bytes array, but
nothing
> > goes
> > > > into the bstr. Whats wrong here ?




ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemtextencodingclassgetbytest

- Show quoted text -

Quote:
> > > > > opic5.htm

> > > > > Ronald Laeremans
> > > > > Visual C++ compiler team



> > > > > > Simple, inefficient way:

> > > > > > Function StringToBytes(inString as String) As Byte()
> > > > > >   Dim TempBytes() As Byte
> > > > > >   For i = 0 to Len(inString) - 1
> > > > > >     Redim Preserve TempBytes(i)
> > > > > >     TempBytes(i) = Asc(Mid(inString, i + 1, 1))
> > > > > >   Next
> > > > > >   Return TempBytes
> > > > > > End Function

> > > > > > Why don't you declare a StreamWriter or TextWriter instead,
> though?

> > > > > > Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
> > > > > > Dim wt As System.IO.TextWriter = New System.IO.TextWriter

> > > > > > Cromas



> > > > > > > Hi
> > > > > > > How is a string converted to an array of Byte , like which is
> > > required
> > > > > by
> > > > > > > the first argument of the FileStream's Write method (array as
> > > Byte() )
> > > > ?



Sun, 04 Apr 2004 00:36:30 GMT  
 Convert a string to byte array
Thanx, everything's working now. I used ASCIIEncoding class to convert
strings and also used utf7 n utf8 class, all of them works, I didnt notice
that they were derived from system.text.encoding. But there's one thing
confusing and it is that in the documentation (MSDN) they say on the first
line of the ASCIIencoding that its an abstract class, and this is also what
I see in the tooltip of the vbnet editor that its an abstract class. Why is
it written as such ? And the docs says that abstract classes are those which
have a word MustInherit with them. Whats up with the docs? I think they 've
written it wrong!
 - Abubakar.


Quote:
> You would actually create an Encoding object from either one of the ones
> alreaday there in System.Text.Encoding or throuigh
> System.Text.Encoding.GetEncoding(<code page>) if none of the built in ones
> would work.

> This is how you would use an existing one without creating a new one. :-)

> --
> MichKa

> Michael Kaplan
> (principal developer of the MSLU)
> Trigeminal Software, Inc.  -- http://www.trigeminal.com/
> the book -- http://www.i18nWithVB.com/



> > Hey I'm not trying to create my own encoding, the article/page of the
.NET
> > help which Ronald Laermans[msft] referred me to pointed to the getbytes
> > method of the system.text.encoding class. When I started to use it the
> > compiler gave a check warning that I cannot create the object of a class
> > declared with MustInherit, so ofcourse if I want to use any of the
methods
> > of the system.text.encoding class I'll have to derive it into some of my
> own
> > custom class. But the story doesnt end here, if the class is mustinherit
> > then there is a high probability that there'll also be some MustOverride
> > methods there, and indeed there are, and they r 6 of them. And the rest
of
> > it I mentioned in my previous post. So far I've got the answer from
> Corrado
> > who have done it through system.text.encoder, not system.text.encoding,
> and
> > then u said that It'll only work with English, I guess u r talking about
> > encoder class, and still, I want to know that Am I doing right by
> inheriting
> > and then using it, and how do I get the byte array from
> > system.text.encoding's getbytes method that takes a string and returns
> byte
> > array.
> >  - Abubakar.

wrote
> in

> > > Why on earth are you creating your own encoding? Why not use an
existing
> > > encodign object?

> > > --
> > > MichKa

> > > Michael Kaplan
> > > (principal developer of the MSLU)
> > > Trigeminal Software, Inc.  -- http://www.trigeminal.com/
> > > the book -- http://www.i18nWithVB.com/



> > > > Thanx for the link, it increased my knowledge but I'm having another
> > > problem
> > > > and still not succeeded in my conversion function ::
> > > > I inherited a class from System.Text.Encoding and overrided its 6
> > > > mustoverride functions. Then I created the object of my new class
and
> > did
> > > > the following:

> > > > Dim str As String = "hello world"
> > > > Dim varsys As New bytestr() 'bytsstr is my new class  which inherits
> > from
> > > > Encoding
> > > > Dim bstr(str.Length) As Byte
> > > > bstr = varsys.GetBytes(str)

> > > > The above code should return the appropriate bytes array, but
nothing
> > goes
> > > > into the bstr. Whats wrong here ?




ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemtextencodingclassgetbytest

- Show quoted text -

Quote:
> > > > > opic5.htm

> > > > > Ronald Laeremans
> > > > > Visual C++ compiler team



> > > > > > Simple, inefficient way:

> > > > > > Function StringToBytes(inString as String) As Byte()
> > > > > >   Dim TempBytes() As Byte
> > > > > >   For i = 0 to Len(inString) - 1
> > > > > >     Redim Preserve TempBytes(i)
> > > > > >     TempBytes(i) = Asc(Mid(inString, i + 1, 1))
> > > > > >   Next
> > > > > >   Return TempBytes
> > > > > > End Function

> > > > > > Why don't you declare a StreamWriter or TextWriter instead,
> though?

> > > > > > Dim ws As System.IO.StreamWriter = New System.IO.StreamWriter
> > > > > > Dim wt As System.IO.TextWriter = New System.IO.TextWriter

> > > > > > Cromas



> > > > > > > Hi
> > > > > > > How is a string converted to an array of Byte , like which is
> > > required
> > > > > by
> > > > > > > the first argument of the FileStream's Write method (array as
> > > Byte() )
> > > > ?



Sun, 04 Apr 2004 05:35:18 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Converting between String and Byte Array

2. Converting string array to byte array

3. Need to convert a 16 byte array of bytes to 4 byte integer

4. Converting from string to array of bytes

5. Convert integers and strings to byte array

6. Convert Byte Array to a String

7. convert Byte array to string and vice veras

8. Convert Strings to Byte arrays and back

9. Convert an array of bytes to a string

10. StrConv function - Converting a byte array to a string

11. Converting a byte array to a string

12. How to convert a byte array to string ?

 

 
Powered by phpBB® Forum Software