简体   繁体   中英

C#/CSS: Convert bytes to CSS hex string

Let's say I have these byte s:

byte red = 0;
byte green = 0;
byte blue = 255;

And I want to turn it into the six-character string hex representation you see in CSS (eg " #0000ff "):

How can I do this?

Color c = Color.FromArgb(red, green, blue);
var hexColor = System.Drawing.ColorTranslator.ToHtml(c);

should produce "0000FF"

See MSDN

Bala R is spot on for Windows Forms, but if you're using ASP.NET here's the quick-and-dirty way:

string cssColor = String.Format("#{0:X2}{1:X2}{2:X2}", red, green, blue);

use

byte red = 0;
byte green = 0;
byte blue= 255;
byte[] color={red,green,blue};
string s = BitConverter.ToString(color);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM