Syste.String to Byte[]

Syste.String to Byte[]

Post by Vladimír Lehock » Tue, 08 Oct 2002 16:42:06



Try one the classes System.Text.ASCIIEncoding, System.Text.UTF7Encoding,
System.Text.UTF8Encoding or System.Text.UnicodeEncoding

Vlado


Quote:> Hi,

> I'm surprised to find that I don't have an answer to this seemingly
> straightforawrd question.
> I'm getting a string (managed string, or system.string, if you
please)..and
> need to convert it to a Byte[] before passing it to a Crypto Service
> Provider. Now, How do I do that?

> Pleae help,

> Regards
> - Mrinal

 
 
 

Syste.String to Byte[]

Post by Mrina » Tue, 08 Oct 2002 16:41:25


Hi,

I'm surprised to find that I don't have an answer to this seemingly
straightforawrd question.
I'm getting a string (managed string, or system.string, if you please)..and
need to convert it to a Byte[] before passing it to a Crypto Service
Provider. Now, How do I do that?

Pleae help,

Regards
- Mrinal

 
 
 

Syste.String to Byte[]

Post by Deepak Gulati [MS » Tue, 08 Oct 2002 16:39:48


Hi Mrinal,

Look at the GetByte method of the UnicodeEncoding class..Here is a code
snippet.
using System;
using System.Text;

namespace Y
{
 class X
 {
  public static void Main()
  {
   UnicodeEncoding ue = new UnicodeEncoding();
   byte[] b = ue.GetBytes("Hello World");
  }

 }

Quote:}

HTH,
Deepak

---------------------------------
This posting is provided "As-Is" with no implied warranties


Quote:> Hi,

> I'm surprised to find that I don't have an answer to this seemingly
> straightforawrd question.
> I'm getting a string (managed string, or system.string, if you
please)..and
> need to convert it to a Byte[] before passing it to a Crypto Service
> Provider. Now, How do I do that?

> Pleae help,

> Regards
> - Mrinal

 
 
 

Syste.String to Byte[]

Post by Martin Deche » Tue, 08 Oct 2002 16:57:52


Can't you use a StreamWriter on a MemoryStream or on a CryptoStream and pass
the string to the StreamWriter.Write() method? Something like this:

public static byte[] Encrypt(string s)
{
RijndaelManaged r = new RijndaelManaged();
// Probably want to set the key and initial vector here:
// r.Key = new byte[]{...};
// r.IV = new byte[]{...};
ICryptoTransform ct = r.Crea*cryptor();
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms, System.Text.Encoding.Unicode);
sw.Write(s);
sw.Flush();
return ct.TransformFinalBlock(ms.ToArray(), 0, (int)ms.Length);

Quote:}

This is a working example.

Greetigns
Marty

Quote:> Hi,

> I'm surprised to find that I don't have an answer to this seemingly
> straightforawrd question.
> I'm getting a string (managed string, or system.string, if you
please)..and
> need to convert it to a Byte[] before passing it to a Crypto Service
> Provider. Now, How do I do that?

> Pleae help,

> Regards
> - Mrinal

 
 
 

Syste.String to Byte[]

Post by S.Annamala » Tue, 08 Oct 2002 17:24:05


Hi,

code sample:

using System.Text;

byte[] byt = Encoding.ASCII.GetBytes("ABCD");

Regards,
S.Annamalai.


Quote:> Hi,

> I'm surprised to find that I don't have an answer to this seemingly
> straightforawrd question.
> I'm getting a string (managed string, or system.string, if you
please)..and
> need to convert it to a Byte[] before passing it to a Crypto Service
> Provider. Now, How do I do that?

> Pleae help,

> Regards
> - Mrinal

 
 
 

Syste.String to Byte[]

Post by Shiv » Tue, 08 Oct 2002 17:29:30


Use ASCIIEncoding.GetBytes() or UnicodeEncoding.GetBytes().


Hi,

I'm surprised to find that I don't have an answer to this seemingly
straightforawrd question.
I'm getting a string (managed string, or system.string, if you please)..and
need to convert it to a Byte[] before passing it to a Crypto Service
Provider. Now, How do I do that?

Pleae help,

Regards
- Mrinal

 
 
 

Syste.String to Byte[]

Post by Mrina » Tue, 08 Oct 2002 17:57:37


Hey,
Thanks Deepak, Vladimir. Thatz a neat solution.

Just the other side of the coin. the byte arry that i get after encryption
has to be converted back to string. now, there is a GetString method in
unicodeEncoding class that accepts byte array. the trouble is encryption
results in some hex data. When I display this as String, it's unreadable.
How to get wround this problem?

Thanks once again for the quick response.
Regards
- Mrinal


Quote:> Hi,

> I'm surprised to find that I don't have an answer to this seemingly
> straightforawrd question.
> I'm getting a string (managed string, or system.string, if you
please)..and
> need to convert it to a Byte[] before passing it to a Crypto Service
> Provider. Now, How do I do that?

> Pleae help,

> Regards
> - Mrinal

 
 
 

Syste.String to Byte[]

Post by _CorExeMain [.NET MVP » Tue, 08 Oct 2002 17:50:52


Hi!

byte [] bArray = Encoding.ASCII.GetBytes(strToBeConvertedtoByteArray);

Ofcourse, you will have to refer to System.Text namespace.

--
Kumar Gaurav Khanna
*************************************************************************
"I can't be Garbage Collected... I am pinned to .NET"



Quote:> Hi,

> I'm surprised to find that I don't have an answer to this seemingly
> straightforawrd question.
> I'm getting a string (managed string, or system.string, if you
please)..and
> need to convert it to a Byte[] before passing it to a Crypto Service
> Provider. Now, How do I do that?

> Pleae help,

> Regards
> - Mrinal

 
 
 

Syste.String to Byte[]

Post by Mrina » Tue, 08 Oct 2002 18:03:07


Hi All,
Thanks for the promt answer.
Now my problem has transalted to cnverting back the encrypted byte array to
a human readable string. basically the encrypted byte array contains hex
data.......... and using UnicodeEncryption.GetString(Byte[]) doesn't give a
readable string.

Regards


> Use ASCIIEncoding.GetBytes() or UnicodeEncoding.GetBytes().



> Hi,

> I'm surprised to find that I don't have an answer to this seemingly
> straightforawrd question.
> I'm getting a string (managed string, or system.string, if you
please)..and
> need to convert it to a Byte[] before passing it to a Crypto Service
> Provider. Now, How do I do that?

> Pleae help,

> Regards
> - Mrinal

 
 
 

Syste.String to Byte[]

Post by S.Annamala » Tue, 08 Oct 2002 18:06:07


Hi,

    You Decode the string before displaying to the user.

Regards,
S.Annamalai.

> Hey,
> Thanks Deepak, Vladimir. Thatz a neat solution.

> Just the other side of the coin. the byte arry that i get after encryption
> has to be converted back to string. now, there is a GetString method in
> unicodeEncoding class that accepts byte array. the trouble is encryption
> results in some hex data. When I display this as String, it's unreadable.
> How to get wround this problem?

> Thanks once again for the quick response.
> Regards
> - Mrinal



> > Hi,

> > I'm surprised to find that I don't have an answer to this seemingly
> > straightforawrd question.
> > I'm getting a string (managed string, or system.string, if you
> please)..and
> > need to convert it to a Byte[] before passing it to a Crypto Service
> > Provider. Now, How do I do that?

> > Pleae help,

> > Regards
> > - Mrinal

 
 
 

Syste.String to Byte[]

Post by Mrina » Tue, 08 Oct 2002 18:29:59


Hi Annamalai,

When I encrypt my input using Crypto Service Provide, it returns a byte
array (length = 16). now i need to convert it to a string which contains the
hex representation of these bytes. This means there'll be 32 hex characters
representing these 16 bytes. My question is, How to get sucha  string.

If I use UnicodeEncoder.GetString(byte[]), it returns a string containing 8
Unicode charatcters. How to get a string containing 32 chaarteres (every two
character being a hex representation of a byte).

Regards
- Mrinal


> Hi,

>     You Decode the string before displaying to the user.

> Regards,
> S.Annamalai.


> > Hey,
> > Thanks Deepak, Vladimir. Thatz a neat solution.

> > Just the other side of the coin. the byte arry that i get after
encryption
> > has to be converted back to string. now, there is a GetString method in
> > unicodeEncoding class that accepts byte array. the trouble is encryption
> > results in some hex data. When I display this as String, it's
unreadable.
> > How to get wround this problem?

> > Thanks once again for the quick response.
> > Regards
> > - Mrinal



> > > Hi,

> > > I'm surprised to find that I don't have an answer to this seemingly
> > > straightforawrd question.
> > > I'm getting a string (managed string, or system.string, if you
> > please)..and
> > > need to convert it to a Byte[] before passing it to a Crypto Service
> > > Provider. Now, How do I do that?

> > > Pleae help,

> > > Regards
> > > - Mrinal

 
 
 

Syste.String to Byte[]

Post by Shiv » Tue, 08 Oct 2002 18:56:16


Did you try with ASCIIEncoding.GetString()? If the encoded byte stream
contains any byte value representing non-displayable character, then you may
get some unreadable character in the returned string.

You may also consider using BitConverter.ToString() method.


Hi All,
Thanks for the promt answer.
Now my problem has transalted to cnverting back the encrypted byte array to
a human readable string. basically the encrypted byte array contains hex
data.......... and using UnicodeEncryption.GetString(Byte[]) doesn't give a
readable string.

Regards


> Use ASCIIEncoding.GetBytes() or UnicodeEncoding.GetBytes().



> Hi,

> I'm surprised to find that I don't have an answer to this seemingly
> straightforawrd question.
> I'm getting a string (managed string, or system.string, if you
please)..and
> need to convert it to a Byte[] before passing it to a Crypto Service
> Provider. Now, How do I do that?

> Pleae help,

> Regards
> - Mrinal

 
 
 

Syste.String to Byte[]

Post by _CorExeMain [.NET MVP » Tue, 08 Oct 2002 18:56:25


Hi!

Did you try using the ASCII encoding class? UnicodeEncoding shall render
characters in unicode which aren't printable.

--
Kumar Gaurav Khanna
*************************************************************************
"I can't be Garbage Collected... I am pinned to .NET"



> Hi All,
> Thanks for the promt answer.
> Now my problem has transalted to cnverting back the encrypted byte array
to
> a human readable string. basically the encrypted byte array contains hex
> data.......... and using UnicodeEncryption.GetString(Byte[]) doesn't give
a
> readable string.

> Regards



> > Use ASCIIEncoding.GetBytes() or UnicodeEncoding.GetBytes().



> > Hi,

> > I'm surprised to find that I don't have an answer to this seemingly
> > straightforawrd question.
> > I'm getting a string (managed string, or system.string, if you
> please)..and
> > need to convert it to a Byte[] before passing it to a Crypto Service
> > Provider. Now, How do I do that?

> > Pleae help,

> > Regards
> > - Mrinal

 
 
 

Syste.String to Byte[]

Post by _CorExeMain [.NET MVP » Fri, 11 Oct 2002 03:42:24


hi!

Use String.Format to created a formatted string, using "2x" in the format
specifier, to suit your need.

--
"I can't be Garbage Collected... I am pinned to .NET"

Kumar Gaurav Khanna
Microsoft MVP - .NET | Early Achiever MCSE Windows 2000 | MCSE NT 4.0 |
MCP+I
WWW: http://www.wintoolzone.com/


> Hi Annamalai,

> When I encrypt my input using Crypto Service Provide, it returns a byte
> array (length = 16). now i need to convert it to a string which contains
the
> hex representation of these bytes. This means there'll be 32 hex
characters
> representing these 16 bytes. My question is, How to get sucha  string.

> If I use UnicodeEncoder.GetString(byte[]), it returns a string containing
8
> Unicode charatcters. How to get a string containing 32 chaarteres (every
two
> character being a hex representation of a byte).

> Regards
> - Mrinal



> > Hi,

> >     You Decode the string before displaying to the user.

> > Regards,
> > S.Annamalai.


> > > Hey,
> > > Thanks Deepak, Vladimir. Thatz a neat solution.

> > > Just the other side of the coin. the byte arry that i get after
> encryption
> > > has to be converted back to string. now, there is a GetString method
in
> > > unicodeEncoding class that accepts byte array. the trouble is
encryption
> > > results in some hex data. When I display this as String, it's
> unreadable.
> > > How to get wround this problem?

> > > Thanks once again for the quick response.
> > > Regards
> > > - Mrinal



> > > > Hi,

> > > > I'm surprised to find that I don't have an answer to this seemingly
> > > > straightforawrd question.
> > > > I'm getting a string (managed string, or system.string, if you
> > > please)..and
> > > > need to convert it to a Byte[] before passing it to a Crypto Service
> > > > Provider. Now, How do I do that?

> > > > Pleae help,

> > > > Regards
> > > > - Mrinal

 
 
 

Syste.String to Byte[]

Post by Mrina » Fri, 11 Oct 2002 15:37:24


Hi Gaurav,
Could you please give an example of its use?

Regards
Mrinal



> hi!

> Use String.Format to created a formatted string, using "2x" in the format
> specifier, to suit your need.

> --
> "I can't be Garbage Collected... I am pinned to .NET"

> Kumar Gaurav Khanna
> Microsoft MVP - .NET | Early Achiever MCSE Windows 2000 | MCSE NT 4.0 |
> MCP+I
> WWW: http://www.wintoolzone.com/



> > Hi Annamalai,

> > When I encrypt my input using Crypto Service Provide, it returns a byte
> > array (length = 16). now i need to convert it to a string which contains
> the
> > hex representation of these bytes. This means there'll be 32 hex
> characters
> > representing these 16 bytes. My question is, How to get sucha  string.

> > If I use UnicodeEncoder.GetString(byte[]), it returns a string
containing
> 8
> > Unicode charatcters. How to get a string containing 32 chaarteres (every
> two
> > character being a hex representation of a byte).

> > Regards
> > - Mrinal



> > > Hi,

> > >     You Decode the string before displaying to the user.

> > > Regards,
> > > S.Annamalai.


> > > > Hey,
> > > > Thanks Deepak, Vladimir. Thatz a neat solution.

> > > > Just the other side of the coin. the byte arry that i get after
> > encryption
> > > > has to be converted back to string. now, there is a GetString method
> in
> > > > unicodeEncoding class that accepts byte array. the trouble is
> encryption
> > > > results in some hex data. When I display this as String, it's
> > unreadable.
> > > > How to get wround this problem?

> > > > Thanks once again for the quick response.
> > > > Regards
> > > > - Mrinal



> > > > > Hi,

> > > > > I'm surprised to find that I don't have an answer to this
seemingly
> > > > > straightforawrd question.
> > > > > I'm getting a string (managed string, or system.string, if you
> > > > please)..and
> > > > > need to convert it to a Byte[] before passing it to a Crypto
Service
> > > > > Provider. Now, How do I do that?

> > > > > Pleae help,

> > > > > Regards
> > > > > - Mrinal

 
 
 

1. Converts wide (double-byte) characters in string to narrow (single-byte) charact

I need a function to convert wide (double-byte) characters
in string to narrow (single-byte) characters.
for instance, convert "3" to "3".

Does any function support in c#??
i know a function strconv("",vbWide) in vb....

or
any attributes in TextBox, i just set "that attribute"
then once the TextBox gets focus, what do users key in is
wide chacters.

thanks  

2. ON Technology CMM

3. String to byte[] in ISO-8859-1 format.

4. What really goes on behind the seen.

5. Convert String to byte[] in C#

6. Shuttle Matrix Printers

7. iPAQ Com Port byte[] to string

8. Tape drives for Ataris

9. Converting String to Byte for PropertyItem data ...

10. byte[] to string

11. In c#,what function can convert byte[] to string or int[],and so on

12. How to convert a String array to a byte array?

13. string to byte[]?