convert string to byte array

P

Peter

Trying to convert string to byte array.

the following code returns byte array of {107, 62, 194, 139, 64}

how can I convert this string to a byte array of {107, 62, 139, 65}

System.Text.UTF8Encoding str = new System.Text.UTF8Encoding();

string s = new string((char)107, 1);
s += new string((char)62, 1);
s += new string((char)139, 1);
s += new string((char)65, 1);

byte[] binScanData = str.GetBytes(s);

Thanks


Peter
 
J

Jon Skeet [C# MVP]

Trying to convert string to byte array.

the following code returns byte array of {107, 62, 194, 139, 64}

No it doesn't - the last byte is 65, not 64. The reason why you get
194 and 139 is due to how UTF-8 encoding works.
how can I convert this string to a byte array of {107, 62, 139, 65}

You could use ISO-8859-1 (Encoding.GetEncoding(28591)).

However, if you're trying to encode arbitrary opaque binary data in a
string, I *strongly* recommend using base64 instead.

Jon
 
T

Tim Jarvis

Peter said:
Trying to convert string to byte array.

the following code returns byte array of {107, 62, 194, 139, 64}

how can I convert this string to a byte array of {107, 62, 139, 65}

System.Text.UTF8Encoding str = new System.Text.UTF8Encoding();

string s = new string((char)107, 1);
s += new string((char)62, 1);
s += new string((char)139, 1);
s += new string((char)65, 1);

byte[] binScanData = str.GetBytes(s);

Thanks


Peter

I don't think you can (or should) convert that to a byte array of {107,
62, 139, 65}

what you are doing is concatenating 4 /unicode/ chars together, what
you really should be decoding to is a unicode byte array, but that
won't give you what you want, it will give you what you are encoding
though ;-)

System.Text.UnicodeEncoding str = new System.Text.UnicodeEncoding();
string s = new string((char)107, 1);
s += new string((char)62, 1);
s += new string((char)139, 1);
s += new string((char)65, 1);

byte[] binScanData = str.GetBytes(s);

and this will give you the /correct/ result...i.e.
{107,0,62,0,139,0,65,0}

Rgds Tim.

--
 
Z

Zhi-Xin Ye [MSFT]

Dear Peter,

The code use should returns byte array of {107,62,139,65}, this is because
139 is greater than 127, the UTF-8 encoding uses two bytes to reprent it.

However, to retrieve the array you need, you can do something like this:

char[] cs = s.ToCharArray();
byte[] binScanData = new byte[cs.Length];
for(int j =0;j<cs.Length;j++)
{
binScanData[j] = (byte)cs[j];
}

Should you have any questions, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Tim Jarvis

Zhi-Xin Ye said:
Dear Peter,

The code use should returns byte array of {107,62,139,65}, this is
because 139 is greater than 127, the UTF-8 encoding uses two bytes to
reprent it.

However, to retrieve the array you need, you can do something like
this:

char[] cs = s.ToCharArray();
byte[] binScanData = new byte[cs.Length];
for(int j =0;j<cs.Length;j++)
{
binScanData[j] = (byte)cs[j];
}

The problem is though with this solution and also with Jon's is that
although it will work with the original posters char type casts of
107,62,139,65 if any value greater than 255 is cast, say (char)500,
both solutions will lose information. The bottom line is that a char
and a byte simply do not occupy the same storage space therefore
comparing a char array and a byte array are like comparing apples and
oranges.
 
J

Jon Skeet [C# MVP]

Tim Jarvis said:
The problem is though with this solution and also with Jon's is that
although it will work with the original posters char type casts of
107,62,139,65 if any value greater than 255 is cast, say (char)500,
both solutions will lose information. The bottom line is that a char
and a byte simply do not occupy the same storage space therefore
comparing a char array and a byte array are like comparing apples and
oranges.

Absolutely. There are additional oddities involved in terms of
combining characters etc which may not be preserved - that's why I
always use Base64 to encode arbitrary binary data as text.
 
P

Peter

Jon Skeet said:
Absolutely. There are additional oddities involved in terms of
combining characters etc which may not be preserved - that's why I
always use Base64 to encode arbitrary binary data as text.

--
Jon Skeet - <[email protected]>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

The char array is actually a an array of floats stored as one long string in
the database, so in this case each char will never be more then 255.
The float array was moved in memory from the float array into string using
VB6 and CopyMemory Win32 API and stored in a database. I am trying to
retrieve it and convert it back to float array using C#. The data is stored
in Access database in a memo field, so I can't retrieve it as byte[] array,
I have to retrieve it as a string and somehow move it to float array.
 
J

Jon Skeet [C# MVP]

Peter said:
The char array is actually a an array of floats stored as one long string in
the database, so in this case each char will never be more then 255.
The float array was moved in memory from the float array into string using
VB6 and CopyMemory Win32 API and stored in a database. I am trying to
retrieve it and convert it back to float array using C#. The data is stored
in Access database in a memo field, so I can't retrieve it as byte[] array,
I have to retrieve it as a string and somehow move it to float array.

Okay, the first thing to realise is that this scheme is *incredibly*
brittle. If you get the chance, do migrate away from it.

I don't know how Access handles Unicode, but that's likely to have a
significant impact on how you retrieve the data - in particular which
encoding you need to specify. If you believe that after fetching from
Access into a .NET string each character accurately represents one byte
with the trivial mapping (i.e. unicode char x for byte x) then using
Encoding.GetEncoding(28591) should work - or just cast each byte.

It's well worth creating a test record containing every byte, 0-255, so
you can verify that you can read it again.
 
T

Tim Jarvis

Jon said:
Okay, the first thing to realise is that this scheme is incredibly
brittle. If you get the chance, do migrate away from it.

Also hunt down the person responsible and give him a wedgie...

;-)

--
 
P

Peter

Jon Skeet said:
Peter said:
The char array is actually a an array of floats stored as one long string
in
the database, so in this case each char will never be more then 255.
The float array was moved in memory from the float array into string
using
VB6 and CopyMemory Win32 API and stored in a database. I am trying to
retrieve it and convert it back to float array using C#. The data is
stored
in Access database in a memo field, so I can't retrieve it as byte[]
array,
I have to retrieve it as a string and somehow move it to float array.

Okay, the first thing to realise is that this scheme is *incredibly*
brittle. If you get the chance, do migrate away from it.

I don't know how Access handles Unicode, but that's likely to have a
significant impact on how you retrieve the data - in particular which
encoding you need to specify. If you believe that after fetching from
Access into a .NET string each character accurately represents one byte
with the trivial mapping (i.e. unicode char x for byte x) then using
Encoding.GetEncoding(28591) should work - or just cast each byte.

It's well worth creating a test record containing every byte, 0-255, so
you can verify that you can read it again.

--
Jon Skeet - <[email protected]>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

can you point me to an example of how I would use
Encoding.GetEncoding(28591)? All I can fine is the serial port examples.

Thank you
 
J

Jon Skeet [C# MVP]

Peter said:
can you point me to an example of how I would use
Encoding.GetEncoding(28591)? All I can fine is the serial port examples.

Where you're currently using UTF8Encoding, use
Encoding.GetEncoding(28591).
 
Joined
Sep 20, 2011
Messages
1
Reaction score
0
"Jon Skeet [C# MVP]" <[email protected]> wrote in message
news:[email protected]...
> Peter <[email protected]> wrote:
>> can you point me to an example of how I would use
>> Encoding.GetEncoding(28591)? All I can fine is the serial port examples.

>
> Where you're currently using UTF8Encoding, use
> Encoding.GetEncoding(28591).
>
> --
> Jon Skeet - <[email protected]>
> Web site: http://www.pobox.com/~skeet
> Blog: http://www.msmvps.com/jon.skeet
> C# in Depth: http://csharpindepth.com



Thank you

Encoding.GetEncoding(1252) did the trick, now I can convert the memo field
in Access to varbinary(MAX) field in MS SQL.
Thank you peter, it works for me. I struggled for a week, now i resolved, thanks a lot.:cheers:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top