生成十六进制数。 [英] Generate HEX number.

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

问题描述

我需要在C#代码中随机生成一个16位十六进制数字。


我该怎么做?


谢谢。

I need to randomly generate a 16 digit hex number in C# code.

How can I do this?

Thanks.

推荐答案

您可以生成常规整数。如果你想以十六进制字符串的形式表示它,只需格式化它:Int32.ToString(" X")。

------- -

Thi - http://thith.blogspot.com

You can generate a regular integer number. If you want to represent it
in form of a hex string, just format it: Int32.ToString("X").
--------
Thi - http://thith.blogspot.com


谢谢你的快速回复。


收获的是HEX必须是16位/字符,我如何实现

这个?

Thanks Thi for the quick response.

The catch is the HEX has to be 16 digit/character, how do I achieve
this?


Hi Zera,16位数 - > 8个字节。你可以生成8个字节然后

将它们连接起来。


要在每个字节的左边填0,你可以使用易于理解的

String.PadLeft方法并且不必研究格式的复杂性
String.Format使用的
说明符。


随机theRandom = new Random();

byte [] theBytes = new byte [8];

theRandom.NextBytes(theBytes);

StringBuilder buffer = new StringBuilder(16);

for(int i = 0; i< 8; i ++){

buffer.Append(theBytes [i] .ToString( X,PadLeft(2));

}


string myHexString = buffer.ToString();


问候,

Thi - http://thith.blogspot。 com

Hi Zera, 16 digits -> 8 bytes. You can generate 8 bytes then
concatenate them.

To pad 0 to the left of each byte, you can use the easy-to-understand
String.PadLeft method and don''t have to study the complexity of format
specifier used by String.Format.

Random theRandom = new Random();
byte[] theBytes = new byte[8];
theRandom.NextBytes(theBytes);
StringBuilder buffer = new StringBuilder(16);
for (int i = 0; i < 8; i++) {
buffer.Append(theBytes[i].ToString("X").PadLeft(2));
}

string myHexString = buffer.ToString();

Regards,
Thi - http://thith.blogspot.com


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

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