gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:57:12 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com