java encoding.utf8.getbytes,为什么不是Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(x))== x`...

In .NET why isn't it true that:

Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(x))

returns the original byte array for an arbitrary byte array x?

It is mentioned in answer to another question but the responder doesn't explain why.

解决方案

Character encodings (UTF8, specificly) may have different forms for the same code point.

So when you convert to a string and back, the actual bytes may represent a different (canonical) form.

See also String.Normalize(NormalizationForm.System.Text.NormalizationForm.FormD)

See also:

Some Unicode sequences are considered equivalent because they represent the same character. For example, the following are considered equivalent because any of these can be used to represent "ắ":

"\u1EAF"

"\u0103\u0301"

"\u0061\u0306\u0301"

However, ordinal, that is, binary, comparisons consider these sequences different because they contain different Unicode code values. Before performing ordinal comparisons, applications must normalize these strings to decompose them into their basic components.

That page comes with a nice sample that shows you what encodings are always normalized

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验三Socket通信实验报告 (1)实验目的和要求 1. 掌握VB、VC++、VS或JAVA等集成开发环境编写网络程序的方法; 2. 掌握客户/服务器(C/S)应用的工作方式; 3. 学习网络中进程之间通信的原理和实现方法; 4. 理解单播、组播和广播的原理并比较其不同之处; 5. 要求本机既是客户端又是服务器端; (2)实验内容 所编写的程序应具有如下功能: 1. 具有点对点通信功能,任意客户端之间能够发送消息; 2. 具有群组通信功能,客户端能够向组内成员同时发送消息,其他组成员不能收到; 3. 具有广播功能,客户端能够向所有其他成员广播消息; (3)编程语言和环境 1. 编程语言C/C++/C#/Java等均可; 2. 编程环境Windows(MS Visual系列,VC/VB/VS.Net;)和Linux(编辑器vi+编译器GCC)均可; (4)实验主要功能实现说明 以下为针对三个实验内容实现方法的简要说明,示例所用语言为C。 基于C的面向连接的socket编程模型 1. 点对点通信功能 实现网络点对点通讯程序的关键步骤就是实现信息在网络中的发送和接收。数据接收 使用的是Socket,数据发送使用的是NetworkStream。 1.1利用Socket来接收信息 TcpListener tlListen1 = new TcpListener ( 8889 ) ; //侦听端口号 tlListen1.Start ( ) ; Socket skSocket = tlListen1.AcceptSocket ( ) ; //接受远程计算机的连接请求,并获得用以接收数据的Socket实例 EndPoint tempRemoteEP = skSocket.RemoteEndPoint ; //获得远程计算机对应的网络远程终结点 while ( true ) { Byte [] byStream = new Byte[80] ; //定义从远程计算机接收到数据存放的数据缓冲区 int i = skSocket.ReceiveFrom ( byStream , ref tempRemoteEP ) ; //接收数据,并存放到定义的缓冲区中 string sMessage = System.Text.Encoding.UTF8.GetString ( byStream ) ; //以指定的编码,从缓冲区中解析出内容 MessageBox.Show ( sMessage ) ; //显示传送来的数据 } 1.2利用NetworkStream来传送信息 TcpClient tcpc = new TcpClient ( "10.138.198.213" , 8888 ) ; //对IP地址为"10.138.198.213"的计算机的8888端口提出连接申请 NetworkStream tcpStream = tcpc.GetStream ( ) ; //如果连接申请建立,则获得用以传送数据的数据流 string sMsg = "您好,见到您很高兴" ; StreamWriter reqStreamW = new StreamWriter ( tcpStream ) ; //以特定的编码往向数据流中写入数据 ,默认为UTF8编码 reqStreamW.Write ( sMsg ) ; //将字符串写入数据流中 reqStreamW.Flush ( ) ; //清理当前编写器的所有缓冲区,并使所有缓冲数据写入基础流 2. 群组通信功能 组播编程需要UDP,有两个类支持组播网络编程Socket和UdpClient.一台计算机要加 入某一个组,然后接收发往这个组的信息。Socket类要调用SetSocketOption函数加入和 离开某一个组。UdpClient类有直接的加入和离开某个组的成员函数可以调用。而向某个 组发信息,则没有什么特殊的,只需把发送数据的目的地址设为组播地址就可以了。 发送端: Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.0.0.1"), 3000); EndPoint ep = (EndPoint)iep; byte[] b = Encoding.ASCII.GetBytes("just a test!"); s.SendTo(b, ep); s.Close(); 接收端: Socket s = new Socket(AddressFamily.InterNetwork, SocketTyp

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值