十六进制的 String.Format

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

问题描述

使用下面的代码,着色总是给出#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);

推荐答案

{0:X} 中的数字0 指的是列表或参数中的位置.在这种情况下,0 表示使用第一个值,即 Blue.使用 {1:X} 作为第二个参数(Green),依此类推.

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


文档中描述了格式参数的语法:


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.

索引组件

强制索引组件,也称为参数说明符,是一个从 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:E} {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.

对齐组件

可选的对齐组件是一个有符号整数,表示首选的格式化字段宽度.如果alignment 的值小于格式化字符串的长度,则忽略对齐,并将格式化字符串的长度用作字段宽度.如果对齐为正,则字段中的格式化数据右对齐,如果对齐为负,则左对齐.如果需要填充,则使用空格.如果指定了对齐方式,则逗号是必需的.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)