关于C#数据类型的问题
C# 2.0 中是否定义了 Binary
和 ASCII
数据类型?
我计划检测几个变量是 Binary 或 ASCII 或 Integer(sbyte、byte、short、ushort、long、ulong) 类型。
我可以使用 typeof(sbyte) 等。
但我未能实现这个 typeof(binary)
或 typeof(ascii)
。 我需要的是类似 typeof 函数来检测变量是二进制还是 ASCII 类型?
[更新]
format code Octal
Binary 001000 10
ASCII 010000 20
Did Binary
and ASCII
data type had been defined in C# 2.0?
I plan to detect several variables are Binary or ASCII or Integer(sbyte, byte, short, ushort, long, ulong) types.
I can use typeof(sbyte)
ect.
But I failed to implement as this typeof(binary)
or typeof(ascii)
.
What I need is something like this typeof function to detect the variables are Binary or ASCII type?
[update]
format code Octal
Binary 001000 10
ASCII 010000 20
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,您会将文本数据存储在字符串中,将二进制数据存储在
byte[]
中。 不要尝试通过应用任意编码将二进制数据存储在字符串中。例如,如果您尝试使用Encoding.UTF8.GetString(binaryData)
那么很可能您将无法再次取回原始数据。如果您需要在字符串中存储二进制数据,您应该使用类似
Convert.ToBase64String
(然后是Convert.FromBase64String
)的东西,它在每个 4 中存储 3 个字节的二进制数据。字符串中的字符。如果您需要将数据存储在字符串中和原始数据的类型,则应该单独存储类型,或者保留复合形式,例如“type:data”。示例字符串可能是:
Normally you would store text data in a string, and binary data in a
byte[]
. Do not try to store binary data in a string by applying an arbitrary encoding. For example, if you try to useEncoding.UTF8.GetString(binaryData)
then it's highly likely that you won't be able to get the original data back again.If you need to store binary data in a string, you should use something like
Convert.ToBase64String
(and thenConvert.FromBase64String
) which stores 3 bytes of binary data in each 4 characters in the string.If you need to store data in a string and the type of the original data, you should either store the type separately, or keep a compound form such as "type:data". Sample strings might then be:
查看 System.Text。编码和系统。文本.解码器。
Have a look at System.Text.Encoding and System.Text.Decoder.
您需要尝试将其解析为合适的数据类型。优先设置它们。
例如:
要自动检测编码,请参阅 确定 C# 中的字符串编码
You need to attempt-parse it into a fitting datatype. Set them up with priority.
Something like:
To autodetect encoding see Determine a string's encoding in C#