Convert MS Access color code to Hex in C #

Is there a way to convert MS Access color codes to Hex in C #?

eg - (white) 16777215 -> #FFFFFF - (black) 0 -> #000000 - (blue) 16711680 -> #0000FF 

Here are some lookup tables I found on stackoverflow

+6
source share
2 answers

You can convert to hex like this:

  string hexValue = "#" + 16777215.ToString("X"); 

Or wrap it in a method:

  public static string AccessToHex(int colorCode) { return "#" + colorCode.ToString("X"); } 
+7
source

You need to convert the value to hexadecimal, and then flip the first two digits with the last two. For example, converting a raw value of 16711680 to blue gives the hexadecimal value of FF0000. However, the value for blue is 0000FF; swap required (so yes, another answer is wrong ...)

The value is also supplemented to always have 6 required digits.

 string rawHex = msAccessColorCode.ToString("X").PadLeft(6, '0'); string hexColorCode = "#" + rawHex.Substring(4, 2) + rawHex.Substring(2, 2) + rawHex.Substring(0, 2); 

To do the opposite (hex β†’ Ms Acces), simply follow the steps in reverse. Discard the extra character # , discard the first / last two values ​​and convert this number from base 16 to base 10.

 string input = "#0000FF"; string hexColorCode = input.TrimStart('#'); string rawHex = hexColorCode.Substring(4, 2) + hexColorCode.Substring(2, 2) + hexColorCode.Substring(0, 2); string result = Convert.ToInt32(rawHex, 16).ToString(); //16711680 

Please note that Intew.Max set to 0x7FFFFFFF (and our color codes close at 0xFFFFFF ), so it is completely safe to use Convert.ToInt32 here instead of Int64 .

+5
source

Source: https://habr.com/ru/post/943558/


All Articles