如何将字符串MAC地址转换为十六进制 [英] how to Convert string MAC Address to Hex
问题描述
如何将MAC地址 string
如 00:18:4d:D0:9d:62
转换为十六进制字符串,如 466F7572746820466C6F6F72
在C#中?谢谢.
How can I convert a MAC address string
like 00:18:4d:D0:9d:62
to a hex string like 466F7572746820466C6F6F72
in C#? Thanks.
推荐答案
MAC地址几乎是一个十六进制字符串.实际上,如果删除':'字符,则表示您使用的是十六进制字符串.
The MAC address is very nearly a hex string. In fact, if you remove the ':' characters, you have a hex string.
string hex = macAddress.Replace(":", "");
原始问题
00:18:4d:D0:9d:62
00:18:4d:D0:9d:62
只是
00184dD09d62
00184dD09d62
,并且与(或只要)不相同
and not the same as (or as long as)
466F7572746820466C6F6F72
466F7572746820466C6F6F72
我对这个问题的原始解读(可能是错误的)是OP希望使用与十六进制数字等效的以10为基数的.如果需要任何其他表示形式,则可以使用字符串删除冒号.替换,然后使用 Convert.ToUInt64
My original reading of the question (which is probably incorrect) was that the OP wanted the base 10 equivalent of the hex number. If any other representation is desired, one can remove the colons with string.Replace and then parse the number using Convert.ToUInt64
string hex = macAddress.Replace(":", "");
uint64 macAsNumber = Convert.ToUInt64(hex, 16);
然后可以将数字转换为所需格式的字符串.
The number can then be converted to a string in whatever format is desired.
这篇关于如何将字符串MAC地址转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!