【转】Contrary to the answers here, you DON'T need to worry about encoding!

For those goals, I honestly do not understand why people keep telling you that you need the encodings. You certainly do NOT need to worry about encodings for this.

Just do this instead:

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

static string GetString(byte[] bytes)
{
    char[] chars = new char[bytes.Length / sizeof(char)];
    System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
    return new string(chars);
}

As long as your program (or other programs) don‘t try to interpret the bytes somehow, which you obviously didn‘t mention you intend to do, then there is nothing wrong with this approach! Worrying about encodings just makes your life more complicated for no real reason.

Additional benefit to this approach:

It doesn‘t matter if the string contains invalid characters, because you can still get the data and reconstruct the original string anyway!

It will be encoded and decoded just the same, because you are just looking at the bytes.

If you used a specific encoding, though, it would‘ve given you trouble with encoding/decoding invalid characters.

【转】Contrary to the answers here, you DON'T need to worry about encoding!

时间: 2024-10-11 01:44:57

【转】Contrary to the answers here, you DON'T need to worry about encoding!的相关文章

交互式数据包处理程序 Scapy 入门指南

概述 Scapy 是一个强大的交互式数据包处理程序(使用python编写).它能够伪造或者解码大量的网络协议数据包,能够发送.捕捉.匹配请求和回复包等等.它可以很容易地处理一些典型操作,比如端口扫描,tracerouting,探测,单元测试,攻击或网络发现(可替代hping,NMAP,arpspoof,ARP-SK,arping,tcpdump,tethereal,P0F等).最重要的他还有很多更优秀的特性--发送无效数据帧.注入修改的802.11数据帧.在WEP上解码加密通道(VOIP).AR

在XSL中定义HTML标签里的属性使用XSL的值

如何在XSL中定义HTML标签里的属性使用XSL的值?? [xml] <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="exam.xsl"?> <test> <test_type>exam</test_type> <testid id="1"> <say>什么事情啊?<

HDU 3038 - How Many Answers Are Wrong

Problem Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of intege

HDU3038 How Many Answers Are Wrong 【并查集】

How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3145 Accepted Submission(s): 1202 Problem Description TT and FF are - friends. Uh- very very good friends -__-b FF is a b

HDU - 3038 How Many Answers Are Wrong (带权并查集)

题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用,[a, b]和为s,所以a-1与b就可以确定一次关系,通过计算与根的距离可以判断出询问的正确性 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 200010; int f[MAXN],a

HDU 3038 How Many Answers Are Wrong(带权并查集)

传送门 Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_

RTNETLINK answers: File exists错误解决方法

>一.写在前面: 因为是我刚学习linux好多问题需要解决,bolg仅作为记录自己的在技术这条道路上的点点滴滴. 二.事件起因: 最近因为女友的原因消沉的好长时间,在马哥那里的课程的结束到现在已经将近45天时间都没有温习,今天拿出来突然老师问我会不会配置lvs,顿时傻眼了.忘记光了,看来这温故而知新的道理不是白来的,然后就出现今天的的事情了. 今天在安装REdhat的时候遇见和网卡有关的问题,起初没有解决系统级别的经验就是最原始的办法,重启虚拟机,不然就是重装.来来回回折腾了好多次都没有成功(没

How Many Answers Are Wrong

How Many Answers Are Wrong Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the

HDU 3038 How Many Answers Are Wrong(种类并查集)

题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[x] y - x = s 可以推出b - a = rank[y] - rank[x] + s; 并查集 延迟更新什么的,都忘了啊. 还有这题,如果是x--的话,记得更新0的根. #include <cstring> #include <cstdio> #include <stri