将字符串转换为字节数组 [英] convert string to byte array

查看:107
本文介绍了将字符串转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将字符串转换为字节数组。


以下代码返回{107,62,194,139,64}的字节数组


如何将此字符串转换为{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);


谢谢

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

推荐答案

9月16日,5:59 * am,Peter < czu ... @nospam.nospamwrote:
On Sep 16, 5:59*am, "Peter" <czu...@nospam.nospamwrote:

尝试将字符串转换为字节数组。


以下代码返回{107,62,194,139,64}的字节数组
Trying to convert string to byte array.

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



不,它没有 - 最后一个字节是65,而不是64.之所以如此你得到了

194和139是由于UTF-8编码的工作原理。

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.


如何将此字符串转换为字节数组{107,62,139,65}
how can I convert this string to a byte array of {107, 62, 139, 65}



您可以使用ISO-8859-1(Encoding.GetEncoding(28591))。


但是,如果你试图在

字符串中编码任意不透明的二进制数据,我强烈建议使用base64。


Jon

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


Peter写道:
Peter wrote:

尝试将字符串转换为字节数组。


以下代码返回{107,62,194,139,64}的字节数组


如何转换这个字符串为{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);


谢谢


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



I不要认为你可以(或应该)将其转换为{107,

62,139,65}的字节数组


你是什么做就是把4 / unicode / chars连接在一起,你应该解码的是什么?b $ b是一个unicode字节数组,但那个

虽然;-)


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);


这将给你/ correct / result ... ie

{107,0,62, 0,139,0,65,0}


Rgds Tim。


-

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.

--


看看BitConverter,特别是ToSingle方法。
Have a look at BitConverter, specifically the ToSingle Method.


这篇关于将字符串转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆