我正在学习 C#,我试图解决的以下问题是将一个数字存储为字符串并获取 a、r、g、b 值。
所以我想从
string s = "4280427042";
到
int a = 255;
int r = 22;
int g = 22;
int b = 22;
什么是最好的方法?
最佳答案
您可以将字符串转换为无符号的 32 位整数,然后使用 BitConverter
获取单个字节,如下所示:
string s = "4280427042";
uint argb = Convert.ToUInt32(s);
byte[] values = BitConverter.GetBytes(argb);
int a = values[3];
int r = values[2];
int g = values[1];
int b = values[0];
The order of bytes in the array returned by the GetBytes method depends on whether the computer architecture is little-endian or big-endian.
这意味着您需要注意字节的顺序。您可以使用 BitConverter.IsLittleEndian
来帮助你。
关于c# - 从 argb 数获取单一颜色值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35227371/