C# 将十进制(字符串)转换为常规字符串
我有一个使用十进制编码文本的字符串,如何使用 C# 解码其中的十进制?
该字符串看起来像:
"03211212122771217573772312402503648" - The original C# line compiled.
0 32 1 1 2 1 2 1 2 2 77 121 75 73 77 2 3 1 2 4 0 2 5 0 3 6 4 8 - The spaces between each ASCII character / decimal set.
之间的所有空格都是 ASCII 字符之间的空格。
谢谢。
编辑:
我想我的问题不清楚......
它目前采用字符串中的数字格式,编码为字符。
我想解码其中的数据以发送到服务器。
服务器不接受数字,而是需要原始数据,我该怎么做?例如:77 - M 121 - y 等...
我该怎么做?
I have a string with encoded text using decimal, how would I decode the decimal in it using C#?
The string looks like:
"03211212122771217573772312402503648" - The original C# line compiled.
0 32 1 1 2 1 2 1 2 2 77 121 75 73 77 2 3 1 2 4 0 2 5 0 3 6 4 8 - The spaces between each ASCII character / decimal set.
All of the spaces in between are spaces between ASCII characters.
Thanks.
Edit:
I guess my question wasn't clear...
It's currently in a number format in a string, encoded as char.
I want to decode the data in it to send to the server.
The server does not accept numbers, but instead, wants raw data, how would I go about doing this? Like: 77 - M 121 - y etc...
How would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是不可能的。通过连接不带分隔符的值,您省略了解码字符串所需的信息。
如果我们从头开始检查该字符串,您很快就无法确定它的含义。
如果您知道没有存储带前导零的值,例如
032
,则第一个值可以确定为0
。第二个值不再可能确定原始值是什么。它可能是
3
、32
、321
,甚至是321121212277121757377
。编辑:
如果在数字之间添加分隔符,则很容易解析字符串:
代码说明:
str.Split('-')
返回一个字符串数组,其中每个字符串包含一个数字。.Select(Int32.Parse)
会将每个字符串解析为一个数字,它是.Select(s => Int32.Parse(s))
的较短形式。不需要 lambda 表达式,因为Int32.Parse
方法已经适合为Select
方法创建委托。.ToArray()
将Select
返回的IEnumerable
转换为整数数组。That is not possible. By concatenating the values without a separator, you have omitted information that is needed to decode the string.
If we examine the string from the start, you don't get far before it's impossible to determine what it means.
The first value could be determined to be
0
, if you know that no values are stored with leading zeroes, e.g.032
.A the second value it's no longer possible to determine what the original was. It could have been
3
,32
,321
, or even321121212277121757377
.Edit:
If you add a separator between the numbers, it's easy to parse the string:
Explanation of the code:
str.Split('-')
returns an array of strings, where each string contains a number..Select(Int32.Parse)
will parse each string into a number, it's a shorter form of.Select(s => Int32.Parse(s))
. The lambda expression is not needed as theInt32.Parse
method already fits to create a delegate for theSelect
method..ToArray()
converts theIEnumerable<int>
thatSelect
returns into an array of integers.如果你在不同的数字之间添加空格,你可能会得到类似的结果:
If you put spaces between the different numbers, you could have something like: