如果您对如何使用BOM编码/解码UTF-16LE字节数组?和utf-8bom编码感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解如何使用BOM编码/解码UTF-16LE字节数组?的各种细节,并对
如果您对如何使用BOM编码/解码UTF-16LE字节数组?和utf-8 bom编码感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解如何使用BOM编码/解码UTF-16LE字节数组?的各种细节,并对utf-8 bom编码进行深入的分析,此外还有关于byte字节数组转为16进制字符串、c# – 如何引导字节数组?、C#对UTF-16字节数组执行字符串操作、html – 没有BOM编码的坏UTF-8的实用技巧。
本文目录一览:- 如何使用BOM编码/解码UTF-16LE字节数组?(utf-8 bom编码)
- byte字节数组转为16进制字符串
- c# – 如何引导字节数组?
- C#对UTF-16字节数组执行字符串操作
- html – 没有BOM编码的坏UTF-8
如何使用BOM编码/解码UTF-16LE字节数组?(utf-8 bom编码)
我需要对UTF-16字节数组进行编码/解码java.lang.String
。字节数组是通过字节顺序标记(BOM)给我的,我需要使用BOM编码字节数组。
另外,由于我正在与Microsoft客户端/服务器打交道,因此我希望以小字节序(与LE
BOM一起)发出编码,以避免任何误解。我确实意识到,使用BOM可以在大端模式下工作,但是我不想在Windows世界中游走。
例如,以下是一种使用BOM 编码java.lang.String
为UTF-16
little endian 的方法:
public static byte[] encodeString(String message) { byte[] tmp = null; try { tmp = message.getBytes("UTF-16LE"); } catch(UnsupportedEncodingException e) { // should not possible AssertionError ae = new AssertionError("Could not encode UTF-16LE"); ae.initCause(e); throw ae; } // use brute force method to add BOM byte[] utf16lemessage = new byte[2 + tmp.length]; utf16lemessage[0] = (byte)0xFF; utf16lemessage[1] = (byte)0xFE; System.arraycopy(tmp, 0, utf16lemessage, 2, tmp.length); return utf16lemessage;}
用Java做到这一点的最佳方法是什么?理想情况下,我想避免将整个字节数组复制到一个新的字节数组中,该数组在开始时分配了两个额外的字节。
解码这样的字符串也是如此,但是使用java.lang.String
构造函数会更直接:
public String(byte[] bytes, int offset, int length, String charsetName)
答案1
小编典典“ UTF-16”字符集名称将始终使用BOM进行编码,并且将使用大/小端顺序对数据进行解码,但是“ UnicodeBig”和“
UnicodeLittle”可用于按特定字节顺序进行编码。不使用BOM表使用UTF-16LE或UTF- 16BE-有关如何使用“ \
uFEFF”手动处理BOM表的信息,请参阅此文章。有关字符集字符串名称或(最好是)字符集类的规范命名,请参见此处。还要注意,绝对只需要支持有限的编码子集。
byte字节数组转为16进制字符串
package com.security.utils;
/**
-
@author Administrator
-
byte字节数组转为16进制字符串 */ public class BytesToHexString {
public static String bytesToHexString(byte[]bytes){ String str=""; for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length()<2) { hex = hex + "0"; } str = str + hex; } return str; } @SuppressWarnings("unused") private static String bytesToHexString2(byte[] bytes) { String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; StringBuffer sb = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { int temp = bytes[i]; if (temp < 0) { temp += 256; } //高位 int h = temp % 16; //低位 int l = temp / 16; sb.append(hexDigits[h] + hexDigits[l]); } return sb.toString(); } public static void main(String[] args) { try { System.out.println(bytesToHexString("123456".getBytes("UTf-8"))); System.out.println(bytesToHexString2("123456".getBytes("UTF-8"))); } catch (Exception e) { e.printStackTrace(); } } }
c# – 如何引导字节数组?
我的场景是,我有一个非托管驱动程序,从设备读取一些数据,并将其写入大阵列,托管应用程序只读取该数据.
这样的事情
byte[] dataArray = new byte[10*1024*1024];
我想引导dataArray,以便GC不会移动它.
当我刚刚运行应用程序,我得到一个DataAbortApplication,并且在互联网上阅读之后,实际发生了什么,我发现我应该引导dataArray来避免这个错误.
我该怎么办?
解决方法
unsafe void UsingFixed() { var dataArray = new byte[10*1024*1024]; fixed (byte* array = dataArray) { // array is pinned until the end of the 'fixed' block } }
然而,这听起来像是要让阵列固定更长的时间.您可以使用GCHandle完成此操作:
void UsingGCHandles() { var dataArray = new byte[10*1024*1024]; var handle = GCHandle.Alloc(dataArray,GCHandleType.Pinned); // retrieve a raw pointer to pass to the native code: IntPtr ptr = handle.ToIntPtr(); // later,possibly in some other method: handle.Free(); }
C#对UTF-16字节数组执行字符串操作
如何解决C#对UTF-16字节数组执行字符串操作?
| 我正在将文件读入“ 0”。该文件包含许多采用以下格式的UTF-16字符串(百万): 第一个字节包含和字符串长度(以字符为单位)(范围0 .. 255) 以下字节包含采用UTF-16编码的字符串字符(每个char用2个字节表示,表示byteCount = charCount * 2)。 我需要对文件中的所有字符串执行标准字符串操作,例如:IndexOf
,EndsWith
和StartsWith
,以及with4ѭ和StringComparison.Ordinal
。
现在,我的代码首先将每个字符串从字节数组转换为System.String
类型。我发现以下代码是最有效的方法:
// position/length validation removed to minimize the code
string result;
byte charLength = _buffer[_bufferI++];
int byteLength = charLength * 2;
fixed (byte* pBuffer = &_buffer[_bufferI])
{
result = new string((char*)pBuffer,charLength);
}
_bufferI += byteLength;
return result;
new string(char*,int,int)
仍然很慢,因为它会对每个字符串执行不必要的复制。
Profiler说它的“ѭ9”表现慢。
我需要一种方法来执行字符串操作,而不必为每个字符串复制字节。
有没有一种方法可以直接在字节数组上执行字符串操作?
有没有一种方法可以在不复制字节的情况下创建新字符串?
解决方法
Encoding.UTF16.GetString
。
如果使用指针,则可以尝试一次获取多个字符串,这样就不必为每个字符串固定缓冲区。
, 您可以使用带有Encoding.UTF16的StreamReader来读取文件,因此之间没有\“字节开销\”:
using (StreamReader sr = new StreamReader(filename,Encoding.UTF16))
{
string line;
while ((line = sr.ReadLine()) != null)
{
//Your Code
}
}
, 您可以在字节数组上创建扩展方法,以直接在字节数组上处理大多数这些字符串操作,并避免转换成本。不确定要执行的所有字符串操作是什么,因此不确定是否可以通过这种方式完成所有这些操作。
html – 没有BOM编码的坏UTF-8
我使用记事本将所有文件转换为UTF-8而没有BOM编码.我对BOM没有任何问题,但没有BOM编码的UTF根本不起作用,就好像我的网站是用ANSI编码的.所有特殊字符显示为:Â,Ã或á
可能是什么原因以及如何解决?
http://chusmix.com/?ciudad=Pilar
谢谢
< meta http-equiv =“content-type”content =“text / html; charset = UTF-8”/>
更新
对于HTML5,请改用此标记:
< meta charset =“utf-8”>
关于如何使用BOM编码/解码UTF-16LE字节数组?和utf-8 bom编码的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于byte字节数组转为16进制字符串、c# – 如何引导字节数组?、C#对UTF-16字节数组执行字符串操作、html – 没有BOM编码的坏UTF-8等相关内容,可以在本站寻找。
本文标签: