我正在尝试打印出一串 UTF-16 字符。我不久前发布了这个问题,给出的建议是使用 iconv 转换为 UTF-32 并将其打印为 wchar_t 字符串。
我做了一些研究,并设法编写了以下代码:
// *c is the pointer to the characters (UTF-16) i'm trying to print
// sz is the size in bytes of the input i'm trying to print
iconv_t icv;
char in_buf[sz];
char* in;
size_t in_sz;
char out_buf[sz * 2];
char* out;
size_t out_sz;
icv = iconv_open("UTF-32", "UTF-16");
memcpy(in_buf, c, sz);
in = in_buf;
in_sz = sz;
out = out_buf;
out_sz = sz * 2;
size_t ret = iconv(icv, &in, &in_sz, …Run Code Online (Sandbox Code Playgroud) 我的 iconv 代码昨天运行得很好。今天突然在应该翻译的部分显示了那些问号(\xef\xbf\xbd)。
\n\nsetlocale(LC_ALL, \'nl_NL\');\n $title = str_replace(\' & \', \' & \', $feed[$x][\'title\']);\n $title = (iconv(\'UTF-8\', \'ISO-8859-1//TRANSLIT//IGNORE\', $title));\n $link = $feed[$x][\'link\'];\n $description = $feed[$x][\'desc\'];\n $description = (iconv(\'UTF-8\', \'ISO-8859-1//TRANSLIT//IGNORE\', $description));\n $datedag = date(\'d\', strtotime($feed[$x][\'date\']));\n $datemaand = date(\'F\', strtotime($feed[$x][\'date\']));\n $datejaar = date(\'Y\', strtotime($feed[$x][\'date\']));\n echo \'<div style="border:1px solid black;background-color:#d90000;"><p><strong><a href="\'.$link.\'" title="\'.$title.\'" target="_blank">\'.$title.\'</a></strong><br />\';\n echo \'<small><em>Geplaatst op \'.$datedag.\' \'.$datemaand.\', \'.$datejaar.\'</em></small></p>\';\n echo \'<p>\'.$description.\'</p></div><br />\';\nRun Code Online (Sandbox Code Playgroud)\n\n现在,我直到出现问号才添加 setlocale,但没有成功。
\n\n问题:我做错了什么?
\n我有一个使用 gbk 编码的日志文件,我必须像这样读取数据:
tail -n 2000 nohup.out | iconv -f gbk -t utf-8
Run Code Online (Sandbox Code Playgroud)
但是当我使用tail -f它时不会打印任何内容:
tail -f nohup.out | iconv -f gbk -t utf-8
Run Code Online (Sandbox Code Playgroud) 给出以下代码:
\n<?php\n$mb_name = "\xe6\xb9\x8a\xe5\xb4\x8e \xe7\xb4\x97\xe5\xa4\x8f";\n$tmp_mb_name = iconv(\'UTF-8\', \'UTF-8//IGNORE\', $mb_name);\nif($tmp_mb_name != $mb_name) {\n echo "tmp_mb_name: {$tmp_mb_name}\\n";\n echo "mb_name: {$mb_name}\\n";\n exit;\n} else {\n echo "no problem!\\n";\n}\nRun Code Online (Sandbox Code Playgroud)\n我在3v4l.org中测试并输出no problem!
然而,在php:7.4-fpm-alpine docker图像中,它输出以下内容:
tmp_mb_name: \nmb_name: \xe6\xb9\x8a\xe5\xb4\x8e \xe7\xb4\x97\xe5\xa4\x8f\n\nRun Code Online (Sandbox Code Playgroud)\n根据php.net:
\n\n\n如果附加字符串 //IGNORE,则无法在目标字符集中表示的字符将被默默丢弃。
\n
为什么无法在 php alpine 图像中$mb_name表示?UTF-8
是否可以读取非utf8编码的网页?例如windows-1251.我尝试使用node-iconv转换结果:
var convertedBody = new Iconv('windows-1251','utf-8').convert(responseBody));
Run Code Online (Sandbox Code Playgroud)
但我得到例外:
Error: EILSEQ, Illegal character sequence.
at IncomingMessage.<anonymous> (/root/nodejstest/test2.js:22:19)
at IncomingMessage.emit (events.js:59:20)
at HTTPParser.onMessageComplete (http.js:111:23)
at Socket.ondata (http.js:1183:22)
at Socket._onReadable (net.js:654:27)
at IOWatcher.onReadable [as callback] (net.js:156:10)
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在尝试使用该命令编译pyspatialite 2.6.1
sudo python setup.py install
Run Code Online (Sandbox Code Playgroud)
它成功编译,但生成的二进制文件缺少符号.当我从中导入dbapi2模块时,我收到以下错误.
Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyspatialite import dbapi2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyspatialite/dbapi2.py", line 27, in <module>
from pyspatialite._spatialite import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyspatialite/_spatialite.so, 2): Symbol not found: _iconv
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyspatialite/_spatialite.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyspatialite/_spatialite.so
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
我在处理在Windows机器上生成的列表数据的文本文件时遇到问题.我在Ruby 1.8工作.当从文件处理SECOND行时,以下给出错误("\ 000"(Iconv :: InvalidCharacter)).第一行正确转换.
require 'iconv'
conv = Iconv.new("UTF-8//IGNORE","UTF-16")
infile = File.open(tabfile, "r")
while (line = infile.gets)
line = conv.iconv(line.strip) # FAILS HERE
puts line
# DO MORE STUFF HERE
end
Run Code Online (Sandbox Code Playgroud)
奇怪的是,它读取并转换文件中的第一行没有问题.我在Iconv构造函数中有// IGNORE标志 - 我认为这应该可以抑制这种错误.
我已经进入了一段时间.任何建议都将受到高度赞赏.
谢谢!
编辑:hobbs解决方案修复此问题.谢谢.只需将代码更改为:
require 'iconv'
conv = Iconv.new("UTF-8//IGNORE","UTF-16")
infile = File.open(tabfile, "r")
while (line = infile.gets("\x0a\x00"))
line = conv.iconv(line.strip) # NO LONGER FAILS HERE
# DOES MORE STUFF HERE
end
Run Code Online (Sandbox Code Playgroud)
现在我只需要找到一种方法来自动确定哪个获取分隔符.
我知道Iconv用于转换字符串的编码.根据我的理解,Kconv也是出于同样的目的(我错了吗?).
我的问题是:它们之间有什么区别,我应该用什么来编码转换.
btw发现了一些信息,即Iconv将从1.9.3版本弃用.
我需要以"普通"标准ASCII格式转换法语,瑞典语和其他语言的字符.
我不知道如何解释,这是一个例子:
...
在bash Unix中我会使用iconv.我怎么能在ColdFusion9/Java中做?
我正在使用php中的多语言应用程序.
一切都很好,直到最近才被要求支持中文字符.我用来支持UTF-8字符的操作如下:
所有数据库表现在都是UTF-8
HTML模板包含标记 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
控制器发出一个标题,指定用于http响应的编码(utf-8)
一切都很好,直到我开始做一些字符串操作(substr和喜欢)
使用中文它将无法工作,因为中文表示为多字节,因此如果你执行一个正常的子字符串(substr),它将在分配的一个字节的中间大量剪切一个"字母",然后f*ck得到结果屏幕.
我通过在引导程序中添加它来解决所有问题
mb_internal_encoding("UTF-8");
并更换所有的strlen,substr,strstr与他们mb_同行.
在php中完全支持UTF-8还需要做些什么?