encoding有什么区别

encoding有什么区别

本文介绍了红宝石中的#encode和#force_encoding有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白Ruby中<$ c $的 #encode #force_encoding 之间的区别c> String 类。我了解 kam .force_encoding( UTF-8)将强制 kam 使用UTF -8编码,但 #encode(encoding)有何不同?

I really do not understand the difference between #encode and #force_encoding in Ruby for the String class. I understand that "kam".force_encoding("UTF-8") will force "kam" to be in UTF-8 encoding, but how is #encode(encoding) different?

推荐答案

差异很大。 force_encoding 设置给定的字符串编码,但不更改字符串本身,即不更改其在内存中的表示形式。

Difference is pretty big. force_encoding sets given string encoding, but does not change the string itself, i.e. does not change it representation in memory:

'łał'.bytes #=> [197, 130, 97, 197, 130]
'łał'.force_encoding('ASCII').bytes #=> [197, 130, 97, 197, 130]
'łał'.force_encoding('ASCII')   #=> "\xC5\x82a\xC5\x82"

编码假定当前编码是正确的,并尝试更改字符串,以便在第二种编码中以相同的方式读取:

Encode is assuming that the current encoding is correct and tries to change the string so it reads same way in second encoding:

'łał'.encode('UTF-16') #=> 'łał'
'łał'.encode('UTF-16').bytes #=> [254, 255, 1, 65, 0, 97, 1, 66]

简而言之, force_encoding 更改从字节读取字符串的方式,而 encode 更改不更改输出的字符串写入方式(如果可能)

In short, force_encoding changes the way string is being read from bytes, and encode changes the way string is written without changing the output (if possible)

这篇关于红宝石中的#encode和#force_encoding有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:01