Using an int as the numerical representation of a string in C#

Posted by bluewall21 on Stack Overflow See other posts from Stack Overflow or by bluewall21
Published on 2010-05-13T08:56:07Z Indexed on 2010/05/13 9:14 UTC
Read the original article Hit count: 142

Filed under:
|
|

I'm trying to use an integer as the numerical representation of a string, for example, storing "ABCD" as 0x41424344. However, when it comes to output, I've got to convert the integer back into 4 ASCII characters. Right now, I'm using bit shifts and masking, as follows:

int value = 0x41424344;
string s = new string (
              new char [] { 
                 (char)(value >> 24), 
                 (char)(value >> 16 & 0xFF),
                 (char)(value >> 8 & 0xFF),
                 (char)(value & 0xFF) });

Is there a cleaner way to do this? I've tried various casts, but the compiler, as expected, complained about it.

© Stack Overflow or respective owner

Related posts about c#

Related posts about string