C# 十六进制的字符串格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11618387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String.Format for Hex
提问by codematrix
With below code, the colorsting always gives #DDDD. Green, Red and Space values int he How to fix this?
使用下面的代码,着色总是给出#DDDD。绿色、红色和空间值 int he 如何解决这个问题?
string colorstring;
int Blue = 13;
int Green = 0;
int Red = 0;
int Space = 14;
colorstring = String.Format("#{0:X}{0:X}{0:X}{0:X}", Blue, Green, Red, Space);
采纳答案by Mark Byers
The number 0in {0:X}refers to the position in the list or arguments. In this case 0means use the first value, which is Blue. Use {1:X}for the second argument (Green), and so on.
数0以{0:X}是指在列表或参数的位置。在这种情况下0意味着使用第一个值,即Blue。使用{1:X}第二个参数(Green),等等。
colorstring = String.Format("#{0:X}{1:X}{2:X}{3:X}", Blue, Green, Red, Space);
The syntax for the format parameter is described in the documentation:
文档中描述了格式参数的语法:
Format Item Syntax
Each format item takes the following form and consists of the following components:
{ index[,alignment][:formatString]}The matching braces ("{" and "}") are required.
Index Component
The mandatory index component, also called a parameter specifier, is a number starting from 0 that identifies a corresponding item in the list of objects. That is, the format item whose parameter specifier is 0 formats the first object in the list, the format item whose parameter specifier is 1 formats the second object in the list, and so on.
Multiple format items can refer to the same element in the list of objects by specifying the same parameter specifier. For example, you can format the same numeric value in hexadecimal, scientific, and number format by specifying a composite format string like this: "{0:X} {0:E} {0:N}".
Each format item can refer to any object in the list. For example, if there are three objects, you can format the second, first, and third object by specifying a composite format string like this: "{1} {0} {2}". An object that is not referenced by a format item is ignored. A runtime exception results if a parameter specifier designates an item outside the bounds of the list of objects.
Alignment Component
The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.
Format String Component
The optional formatString component is a format string that is appropriate for the type of object being formatted. Specify a standard or custom numeric format string if the corresponding object is a numeric value, a standard or custom date and time format string if the corresponding object is a DateTime object, or an enumeration format string if the corresponding object is an enumeration value. If formatString is not specified, the general ("G") format specifier for a numeric, date and time, or enumeration type is used. The colon is required if formatString is specified.
格式项语法
每个格式项采用以下形式并由以下组件组成:
{ index[,alignment][:formatString]}需要匹配的大括号(“{”和“}”)。
索引组件
强制索引组件,也称为参数说明符,是一个从 0 开始的数字,用于标识对象列表中的相应项目。也就是说,参数说明符为 0 的格式项格式化列表中的第一个对象,参数说明符为 1 的格式项格式化列表中的第二个对象,依此类推。
通过指定相同的参数说明符,多个格式项可以引用对象列表中的相同元素。例如,您可以通过指定如下复合格式字符串来以十六进制、科学和数字格式格式化相同的数值:“{0:X} {0:E} {0:N}”。
每个格式项都可以引用列表中的任何对象。例如,如果有三个对象,您可以通过指定如下复合格式字符串来格式化第二个、第一个和第三个对象:“{1} {0} {2}”。格式项未引用的对象将被忽略。如果参数说明符指定对象列表边界之外的项目,则会导致运行时异常。
对齐组件
可选的对齐组件是一个有符号整数,表示首选的格式化字段宽度。如果alignment的值小于格式化字符串的长度,则忽略对齐并以格式化字符串的长度作为字段宽度。如果对齐为正,则字段中的格式化数据右对齐,如果对齐为负,则左对齐。如果需要填充,则使用空格。如果指定对齐方式,则需要逗号。
格式化字符串组件
可选的 formatString 组件是一个适合被格式化的对象类型的格式字符串。如果对应对象是数值,则指定标准或自定义数字格式字符串;如果对应对象是 DateTime 对象,则指定标准或自定义日期和时间格式字符串;如果对应对象是枚举值,则指定枚举格式字符串。如果未指定 formatString,则使用数字、日期和时间或枚举类型的通用(“G”)格式说明符。如果指定了 formatString,则需要冒号。
Note that in your case you only have the index and the format string. You have not specified (and do not need) an alignment component.
请注意,在您的情况下,您只有索引和格式字符串。您尚未指定(也不需要)对齐组件。
回答by JGeerWM
You can also pad the characters left by including a number following the X, such as this: string.format("0x{0:X8}", string_to_modify), which yields "0x00000C20".
您还可以通过在 后面包含一个数字来填充剩余的字符X,例如:string.format("0x{0:X8}", string_to_modify),产生"0x00000C20"。
回答by mschmoock
Translate composed UInt32 color Valueto CSS in .NET
Value在 .NET 中将组合的 UInt32 颜色转换为 CSS
I know the question applies to 3 input values (redgreenblue). But there may be the situation where you already have a composed 32bit Value. It looks like you want to send the data to some HTML CSS renderer (because of the #HEX format). Actually CSS wants you to print 6 or at least 3 zero filled hex digits here. so #{0:X06}or #{0:X03}would be required. Due to some strange behaviour, this always prints 8 digits instead of 6.
我知道这个问题适用于 3 个输入值 ( redgreenblue)。但是可能存在这样的情况,您已经拥有一个组合的 32bit Value. 看起来您想将数据发送到某个 HTML CSS 渲染器(因为 #HEX 格式)。实际上 CSS 想让你在这里打印 6 个或至少 3 个零填充的十六进制数字。所以#{0:X06}或#{0:X03}将需要。由于一些奇怪的行为,这总是打印 8 位而不是 6 位。
Solve this by:
解决这个问题:
String.Format("#{0:X02}{1:X02}{2:X02}", (Value & 0x00FF0000) >> 16, (Value & 0x0000FF00) >> 8, (Value & 0x000000FF) >> 0)
回答by Pranjal Jain
If we have built in functions to convert your integer values to COLOR then why to worry.
如果我们有内置函数将您的整数值转换为 COLOR 那么为什么要担心。
string hexValue = string.Format("{0:X}", intColor);
Color brushes = System.Drawing.ColorTranslator.FromHtml("#"+hexValue);
回答by Luke Puplett
More generally.
更普遍。
byte[] buf = new byte[] { 123, 2, 233 };
string s = String.Concat(buf.Select(b => b.ToString("X2")));

