由于某种原因,我无法寻找我的utf16文件.它产生'UnicodeException:UTF-16流不以BOM开头'.我的代码:
f = codecs.open(ai_file, 'r', 'utf-16')
seek = self.ai_map[self._cbClass.Text] #seek is valid int
f.seek(seek)
while True:
ln = f.readline().strip()
Run Code Online (Sandbox Code Playgroud)
我尝试过随机的东西,比如先从流中读一些东西,没有帮助.我检查了寻求使用十六进制编辑器的偏移量 - 字符串从字符开始,而不是空字节(我猜它的好兆头,对吧?)那么如何在python中寻找utf-16?
出于性能原因,我的应用程序在内存中有大约1,000,000个字符串.我的应用程序消耗~200 MB RAM.
我想减少字符串消耗的内存量.
我知道.NET代表UTF-16编码的字符串(每个字符2个字节).我的应用程序中的大多数字符串都包含纯英文字符,因此以UTF-8编码存储它们的效率将是UTF-16的2倍.
有没有办法在UTF-8编码的内存中存储字符串,同时允许标准的字符串函数?(我的需求主要包括带StringComparison.OrdinalIgnoreCase的IndexOf).
我正在开发一个推特应用程序,偶然发现了utf-8(16)的世界.似乎大多数javascript字符串函数对代理对都是盲目的.我必须重新编码一些东西才能让它具有广泛的字符意识.
我有这个函数来解析字符串到数组,同时保留代理对.然后我将重新编码几个函数来处理数组而不是字符串.
function sortSurrogates(str){
var cp = []; // array to hold code points
while(str.length){ // loop till we've done the whole string
if(/[\uD800-\uDFFF]/.test(str.substr(0,1))){ // test the first character
// High surrogate found low surrogate follows
cp.push(str.substr(0,2)); // push the two onto array
str = str.substr(2); // clip the two off the string
}else{ // else BMP code point
cp.push(str.substr(0,1)); // push one onto array
str = str.substr(1); // clip one from string
}
} // loop
return cp; …Run Code Online (Sandbox Code Playgroud) 我可以将文件读取为bytes数组
但是当我将它转换为字符串时
它将utf16字节视为ascii
如何正确转换?
package main
import ("fmt"
"os"
"bufio"
)
func main(){
// read whole the file
f, err := os.Open("test.txt")
if err != nil {
fmt.Printf("error opening file: %v\n",err)
os.Exit(1)
}
r := bufio.NewReader(f)
var s,b,e = r.ReadLine()
if e==nil{
fmt.Println(b)
fmt.Println(s)
fmt.Println(string(s))
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
假
[255 254 91 0 83 0 99 0 114 0 105 0 112 0 116 0 32 0 73 0 110 0 102 0 111 0 93 0 13 0]
S …
我有一个可能包含任何unicode字符的变量字符串.其中一个unicode字符是汉.
The thing is that this "han" character has "".length() == 2但是作为单个字符写在字符串中.
考虑下面的代码,我如何迭代所有字符并比较每个字符,同时考虑它可能包含一个长度大于1的字符的事实?
for ( int i = 0; i < string.length(); i++ ) {
char character = string.charAt( i );
if ( character == '' ) {
// Fail, it interprets as 2 chars =/
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
这个问题不重复.这将询问如何迭代字符串的每个字符,同时考虑包含的.length() > 1字符(字符不是作为char类型而是作为书写符号的表示).这个问题不需要先前知道如何迭代Java String的unicode代码点,尽管提到的答案也可能是正确的.
TLDR
Java使用两个字符来表示UTF-16。使用Arrays.sort(不稳定的排序)会使字符排序混乱。我应该将char []转换为int []还是有更好的方法?
细节
Java将字符表示为UTF-16。但是Character类本身会包装char(16位)。对于UTF-16,它将是2的数组char(32位)。
使用内置的排序功能对一串UTF-16字符进行排序会使数据混乱。(Arrays.sort使用双重数据透视快速排序,Collections.sort使用Arrays.sort进行繁重的工作。)
具体来说,您是将char []转换为int []还是有更好的排序方式?
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] utfCodes = {128513, 128531, 128557};
String emojis = new String(utfCodes, 0, 3);
System.out.println("Initial String: " + emojis);
char[] chars = emojis.toCharArray();
Arrays.sort(chars);
System.out.println("Sorted String: " + new String(chars));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Initial String:
Sorted String: ????
Run Code Online (Sandbox Code Playgroud) 如果你有一个网站要翻译成世界上的每种语言,因此有一个包含所有这些翻译的数据库,哪种字符编码最好?UTF-128?
如果是这样,所有浏览器都了解所选的编码?字符编码是直接实现还是有隐藏因素?
提前致谢.
我想知道如何在C/C++中规范化字符串(包含utf-8/utf-16).在.NET中有一个函数String.Normalize.
我过去使用过UTF8-CPP,但它没有提供这样的功能. ICU和Qt提供字符串规范化,但我更喜欢轻量级解决方案.
对此有任何"轻量级"解决方案吗?
我在Jon Skeet的博客上看到了这篇文章,他谈到了字符串翻转.我想尝试他自己展示的例子,但它似乎有效...这让我相信我不知道如何创建一个包含代理对的字符串,这实际上会导致字符串反转失败.如何实际创建一个带有代理对的字符串,以便我自己可以看到失败?
在C程序中,我使用wprintf在Windows控制台中打印Unicode(UTF-16)文本.这很好,但是当程序的输出重定向到日志文件时,日志文件的UTF-16编码已损坏.在Windows命令提示符中完成重定向时,所有换行符都编码为窄ASCII换行符(0d0a).在PowerShell中完成重定向时,将插入空字符.
是否可以将输出重定向到正确的UTF-16日志文件?
示例程序:
#include <stdio.h>
#include <windows.h>
#include <fcntl.h>
#include <io.h>
int main () {
int prevmode;
prevmode = _setmode(_fileno(stdout), _O_U16TEXT);
fwprintf(stdout,L"one\n");
fwprintf(stdout,L"two\n");
fwprintf(stdout,L"three\n");
_setmode(_fileno(stdout), prevmode);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在命令提示符中重定向输出.参见0d0a,它应该是0d00 0a00:
c:\test>.\testu16.exe > o.txt
c:\test>xxd o.txt
0000000: 6f00 6e00 6500 0d0a 0074 0077 006f 000d o.n.e....t.w.o..
0000010: 0a00 7400 6800 7200 6500 6500 0d0a 00 ..t.h.r.e.e....
Run Code Online (Sandbox Code Playgroud)
在PowerShell中重定向输出.查看所有插入的0000.
PS C:\test> .\testu16.exe > p.txt
PS C:\test> xxd p.txt
0000000: fffe 6f00 0000 6e00 0000 6500 0000 0d00 ..o...n...e.....
0000010: 0a00 0000 7400 …Run Code Online (Sandbox Code Playgroud)