Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single quoted string to uint in c# [duplicate]

Tags:

c++

string

c#

I was translating some C++ to C# code and I saw the below definiton:

#define x 'liaM'

First, what does this single quoted constant mean? Do I make it a string constant in c#?

Second, this constant is assigned as value to a uint variable in C++. How does that work?

uint m = x;
like image 380
Tyler Durden Avatar asked Oct 21 '13 14:10

Tyler Durden


1 Answers

This is sometimes called a FOURCC. There's a Windows API that can convert from a string into a FOURCC called mmioStringToFOURCC and here's some C# code to do the same thing:

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);
}
like image 99
Mark Heath Avatar answered Nov 07 '22 10:11

Mark Heath