By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,796 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,796 IT Pros & Developers. It's quick & easy.

convert string to byte array

P: n/a
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
Sep 16 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Sep 16, 5:59*am, "Peter" <czu...@nospam.nospamwrote:
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
Sep 16 '08 #2

P: n/a
Peter wrote:
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.

--

Sep 16 '08 #3

P: n/a
Have a look at BitConverter, specifically the ToSingle Method.
Sep 16 '08 #4

P: n/a
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 16 '08 #5

P: n/a
Zhi-Xin Ye [MSFT] wrote:
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.
Sep 16 '08 #6

P: n/a
Tim Jarvis <ti*@jarvis.com.auwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 17 '08 #7

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Tim Jarvis <ti*@jarvis.com.auwrote:
>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.

--
Jon Skeet - <sk***@pobox.com>
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.


Sep 18 '08 #8

P: n/a
Peter <cz****@nospam.nospamwrote:
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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 18 '08 #9

P: n/a
Jon Skeet [C# MVP] wrote:
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...

;-)

--

Sep 18 '08 #10

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Peter <cz****@nospam.nospamwrote:
>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 - <sk***@pobox.com>
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
Sep 19 '08 #11

P: n/a
Peter <cz****@nospam.nospamwrote:
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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 20 '08 #12

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Peter <cz****@nospam.nospamwrote:
>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 - <sk***@pobox.com>
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.
Sep 21 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.