c# - 从 argb 数获取单一颜色值

标签 c# colors hex

我正在学习 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];

引自this MSDN reference :

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/

相关文章:

c# - 如何卸载windows服务

java - 如何在android中随机生成浅色和柔和的颜色?

java - 如何将这段代码翻译成 GUI 可以读取的内容

c# - 表达式与 nameof

C# 在 CategorizedAlphabetical 排序的 ProperyGrid 中选择第一行

c# - Firefox 的等待超时 [WatiN]

c++ - C++ 十六进制输出到字符串

javafx - 将灰度效果应用于 javafx 应用程序

algorithm - 创建色轮的函数

c++ - 在 C++/C++ 中的十六进制范围内查找十六进制数