【问题标题】:Single quoted string to uint in c# [duplicate]c#中将单引号字符串转换为uint [重复]
【发布时间】:2013-10-30 01:56:55
【问题描述】:

我正在将一些 C++ 转换为 C# 代码,我看到了以下定义:

#define x 'liaM'

首先,这个单引号常量是什么意思?我是否在 c# 中将其设为字符串常量?

其次,在 C++ 中,这个常量被赋值给一个 uint 变量。它是如何工作的?

uint m = x;

【问题讨论】:

标签: c# c++ string


【解决方案1】:

这有时称为FOURCC。有一个 Windows API 可以将字符串转换为名为 mmioStringToFOURCC 的 FOURCC,这里有一些 C# 代码可以做同样的事情:

public static int ChunkIdentifierToInt32(string s)
{
    if (s.Length != 4) throw new ArgumentException("Must be a four character string");
    var bytes = Encoding.UTF8.GetBytes(s);
    if (bytes.Length != 4) throw new ArgumentException("Must encode to exactly four bytes");
    return BitConverter.ToInt32(bytes, 0);
}

【讨论】:

  • 为什么不直接使用BitConverter.ToInt32(char[] { 'l', 'i', 'a', 'M' }); 并完全跳过字符串?
  • @BenVoigt 是的,甚至更好。我编写了上面的代码作为调用 mmioStringToFOURCC
  • 其实我的评论应该用byte[]
猜你喜欢
  • 2011-05-11
  • 2022-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 2021-10-17
  • 2019-02-05
  • 1970-01-01
相关资源
最近更新 更多