Converting from string to array of bytes 
Author Message
 Converting from string to array of bytes

Is there a function in the Framework to convert a string to an array of
bytes?

Richard Rosenheim



Thu, 28 Jul 2005 09:24:13 GMT  
 Converting from string to array of bytes
Generally you would use the Encoding class for this, choosing an encoding to match how you want the
translation to take place. What is the form you expect for the reulting byte array?

--
MichKa

This posting is provided "AS IS" with
no warranties, and confers no rights.


Quote:
> Is there a function in the Framework to convert a string to an array of
> bytes?

> Richard Rosenheim



Thu, 28 Jul 2005 09:45:16 GMT  
 Converting from string to array of bytes
I have a string that I want to pass to a function that accepts an array of
bytes.  I could code the "translation" as:

    For i = 0 to MyStr.Length - 1
        MyByteArray(i) = Asc(MyStr.Chars(i))
    Next
    FunctionXYZ(MyByteArray)

I'm just attempting to determine if there's a generic built-in routine as
that would be the preferred method, before using my own code.

Richard Rosenheim



Quote:
> Generally you would use the Encoding class for this, choosing an encoding

to match how you want the
Quote:
> translation to take place. What is the form you expect for the reulting
byte array?

> --
> MichKa

> This posting is provided "AS IS" with
> no warranties, and confers no rights.




Quote:
> > Is there a function in the Framework to convert a string to an array of
> > bytes?

> > Richard Rosenheim



Thu, 28 Jul 2005 13:16:20 GMT  
 Converting from string to array of bytes

Quote:

> Is there a function in the Framework to convert a string to an array of
> bytes?

> Richard Rosenheim

Yes, use
        bytearray = New System.Text.ASCIIEncoding().GetBytes(strMyString)

Cheers,
  Jason



Thu, 28 Jul 2005 17:38:26 GMT  
 Converting from string to array of bytes
You would use an encoding object, like I said.

To get the literal results that you would get below, you would use
Encoding.Default (to get the default system code page) but on the
whole I would recommend a better choice since that will change
behavior between machines when the CP_ACP changes.

You would use the GetBytes method off that object -- you pass in the
string and it spits out the bytes.

--
MichKa

This posting is provided "AS IS" with
no warranties, and confers no rights.



Quote:
> I have a string that I want to pass to a function that accepts an
array of
> bytes.  I could code the "translation" as:

>     For i = 0 to MyStr.Length - 1
>         MyByteArray(i) = Asc(MyStr.Chars(i))
>     Next
>     FunctionXYZ(MyByteArray)

> I'm just attempting to determine if there's a generic built-in
routine as
> that would be the preferred method, before using my own code.

> Richard Rosenheim


in

> > Generally you would use the Encoding class for this, choosing an
encoding
> to match how you want the
> > translation to take place. What is the form you expect for the
reulting
> byte array?

> > --
> > MichKa

> > This posting is provided "AS IS" with
> > no warranties, and confers no rights.



> > > Is there a function in the Framework to convert a string to an
array of
> > > bytes?

> > > Richard Rosenheim



Thu, 28 Jul 2005 23:09:05 GMT  
 Converting from string to array of bytes
Michael, Jason...thanks for the replies.

Richard Rosenheim



Quote:

> > Is there a function in the Framework to convert a string to an array of
> > bytes?

> > Richard Rosenheim

> Yes, use
>         bytearray = New System.Text.ASCIIEncoding().GetBytes(strMyString)

> Cheers,
>   Jason



Fri, 29 Jul 2005 01:34:20 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Converting string array to byte array

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

3. Convert integers and strings to byte array

4. Convert Byte Array to a String

5. Converting between String and Byte Array

6. convert Byte array to string and vice veras

7. Convert Strings to Byte arrays and back

8. Convert a string to byte array

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