为的String.Format十六进制 [英] String.Format for Hex

查看:320
本文介绍了为的String.Format十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码中,colorsting总是给#DDDD。绿色,红色和空间价值的诠释,他如何来解决这个问题。

With below code, the colorsting always gives #DDDD. Green, Red and Space values int he How to fix this?

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);



先谢谢了。

Thanks in advance.

推荐答案

0 {0:X} 是指在列表中的位置或参数。在这种情况下 0 表示使用第一个值,这是蓝色。使用 {1:X} 的第二个参数(绿色),依此类推。

The number 0 in {0:X} refers to the position in the list or arguments. In this case 0 means use the first value, which is Blue. Use {1:X} for the second argument (Green), and so on.

colorstring = String.Format("#{0:X}{1:X}{2:X}{3:X}", Blue, Green, Red, Space);






有关format参数的语法的描述文档

每个格式项采用以下形式,由以下几部分组成:

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.

指数成份

强制索引组件,也称为一个参数说明,是一个从0开始,在对象列表标识一个相应的项目。也就是说,格式项的参数说明是在列表中的第一个对象0格式,该格式项目的参数说明是在列表中的第二对象物1的格式,并依此类推。

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.

多个格式的项目可以通过指定相同的参数说明指代相同元件中的对象的列表。 {0:X} {0:电子} {0:N}。

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}".

每个格式项可以指列表中的任何对象。例如,如果有三个对象,你可以通过指定一个复合格式字符串像这样格式化第二,第一和第三对象:{1} {0} {2}。不是由一个格式项引用的对象被忽略。运行时异常的结果,如果参数说明符指定对象列表的范围之外的项目。

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.

对齐组件

可选的对齐组件是有符号整数,指示首选的格式化字段宽度。如果比对的值小于格式化字符串的长度,对准被忽略,并且所述格式化的字符串的长度被用作场宽度。在该领域的格式化数据是右对齐如果对准是正和左对齐如果取向为负。如果填充是必需的,使用白空间。如果指定对齐逗号是必需的。

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.

格式字符串组件

可选formatString的组件是一个格式字符串为对象的类型而形成,即是适当的。如果相应的对象是一个数值,如果相应的对象是一个DateTime对象或枚举格式字符串如果相应的对象是一个枚举值标准或自定义日期和时间格式字符串指定标准或自定义数字格式字符串。如果没有指定的formatString是,一般的(G)格式说明为一个数字,日期和时间,或枚举类型被使用。如果指定formatString中的冒号是必需的。

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.

请注意,在你的情况你只有索引和格式字符串。您还没有指定(也不需要)对齐组件。

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.

这篇关于为的String.Format十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆