十六进制到二进制 [英] Hexadecimal to Binary
问题描述
你好,新闻组:
我有一个关于十六进制到二进制转换的问题。
假设我有一个很长的十六进制字符串,我想把它转换为
a二进制字符串。如何用最少的代码实现这一目标?我已经看过
其他帖子,但它们被限制为十六进制字符串的大小,
因为它们使用Convert.ToString(...)。
EXA:
string str =" FA38";
string binStr = SomeClass.Hex2Bin(str);
这会产生以下输出...
" 1111101000111000"
谢谢你,
Trecius
Hello, Newsgroupians:
I''ve a question regarding Hexadecimal to binary conversion.
Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I''ve seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToString(...).
EXA:
string str = "FA38";
string binStr = SomeClass.Hex2Bin(str);
This would produce the following output...
"1111101000111000"
Thank you,
Trecius
推荐答案
Trecius< Tr ***** @ discuss.microsoft.comwrote:
Trecius <Tr*****@discussions.microsoft.comwrote:
我有一个关于十六进制到二进制转换的问题。
假设我有一个很长的十六进制字符串,我想把它转换为
a二进制字符串。如何用最少的代码实现这一目标?我已经看过
其他帖子,但它们被限制为十六进制字符串的大小,
因为它们使用Convert.ToString(...)。
EXA:
string str =" FA38";
string binStr = SomeClass.Hex2Bin(str);
这将产生以下输出......
" 1111101000111000"
I''ve a question regarding Hexadecimal to binary conversion.
Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I''ve seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToString(...).
EXA:
string str = "FA38";
string binStr = SomeClass.Hex2Bin(str);
This would produce the following output...
"1111101000111000"
鉴于只有16个十六进制数字,并且每个十六进制数字映射到
正好4个字符,它相对简单。没有
怀疑使用比这更短的代码来做这件事,但这种方式非常好
简单:
使用System;
使用System.Text;
class测试
{
static void Main()
{
string binStr = HexToBin(" FA38");
Console.WriteLine(binStr);
}
静态只读字符串[] Nybbles =
{" 0000"," 0001"," 0010"," 0011",
0100,0101,0110,0111,
" 1000"," 1001"," 1010"," 1011",
" 1100"," 1101"," 1110"," 1111"};
静态字符串HexToBin(字符串输入)
{
StringBuilder builder = new StringBuilder(input.Length * 4);
foreach(输入中的字符)
{
if(c> =''0''&& c< =''9'')
{
builder.Append(Nybbles [c - ''0'']);
}
else if(c> ='''''&& c< =''f'')
{
builder.Append(Nybbles [c - ''''+ 10]);
}
else if(c> =''A''&& c< =''F'')
{
builder.Append(Nybbles [c - ''A''+ 10]);
}
else
{
抛出新的FormatException("无效的十六进制数字:" + c);
}
}
return builder.ToString();
}
}
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~skeet
博客: http://www.msmvps.com/jon_skeet
C#深度: http://csharpindepth.com
Given that there are only 16 hex digits, and each hex digit maps to
exactly 4 characters, it''s relatively straightforward. There are no
doubt ways to do it with shorter code than this, but this way is pretty
simple:
using System;
using System.Text;
class Test
{
static void Main()
{
string binStr = HexToBin("FA38");
Console.WriteLine(binStr);
}
static readonly string[] Nybbles =
{"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"};
static string HexToBin(string input)
{
StringBuilder builder = new StringBuilder(input.Length*4);
foreach (char c in input)
{
if (c >= ''0'' && c <=''9'')
{
builder.Append(Nybbles[c-''0'']);
}
else if (c >= ''a'' && c <= ''f'')
{
builder.Append(Nybbles[c-''a''+10]);
}
else if (c >= ''A'' && c <= ''F'')
{
builder.Append(Nybbles[c-''A''+10]);
}
else
{
throw new FormatException("Invalid hex digit: "+c);
}
}
return builder.ToString();
}
}
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Trecius写道:
Trecius wrote:
$ b $我有一个关于十六进制到二进制转换的问题。
假设我有一个很长的十六进制字符串,我想将它转换为
二进制字符串。如何用最少的代码实现这一目标?我已经看过
其他帖子,但它们被限制为十六进制字符串的大小,
因为它们使用Convert.ToString(...)。
EXA:
string str =" FA38";
string binStr = SomeClass.Hex2Bin(str);
这将产生以下输出......
" 1111101000111000"
I''ve a question regarding Hexadecimal to binary conversion.
Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I''ve seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToString(...).
EXA:
string str = "FA38";
string binStr = SomeClass.Hex2Bin(str);
This would produce the following output...
"1111101000111000"
有16个十六进制数字,对应16个4位模式。所以
白痴的做法(也称为最简单的事情,可能是b $ b工作)将是
private static readonly string [] binarySequences = new string [] {
" 0000"," 0001"," 0010"," 0011",
0100,0101,0110,0111,
1000,1001,1010,1011和1011。 ,
1100,1101,1110,1111,
};
static int hexDigitToInt(char hexDigit){
return hexDigit> =''0''&& hexDigit< =''9''? hexDigit - ''0'':hexDigit
- ''A''+ 10;
}
静态字符串Hex2Bin(字符串hexDigits) {
StringBuilder结果=新的StringBuilder(hexDigits.Length * 4);
foreach(hexDigits中的字符数字){
result.Append( binarySequences [hexDigitToInt(digit)]);
}
返回result.ToString();
}
虽然是白痴的方式,但这实际上并不坏。 (一旦你把
用于验证,那就是。)
-
J.
There are 16 hexadecimal digits, corresponding to 16 4-bit patterns. So the
idiot''s way of doing it (also called "the simplest thing that could possibly
work") would be
private static readonly string[] binarySequences = new string[] {
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};
static int hexDigitToInt(char hexDigit) {
return hexDigit >= ''0'' && hexDigit <= ''9'' ? hexDigit - ''0'' : hexDigit
- ''A'' + 10;
}
static string Hex2Bin(string hexDigits) {
StringBuilder result = new StringBuilder(hexDigits.Length * 4);
foreach (char digit in hexDigits) {
result.Append(binarySequences[hexDigitToInt(digit)]);
}
return result.ToString();
}
And despite being the idiot''s way, this is actually not bad. (Once you put
in validation, that is.)
--
J.
>
Trecius写道:
Trecius wrote:
假设我有一个很长的十六进制字符串,我想把它转换为
a二进制字符串。如何用最少的代码实现这一目标?我已经看过
其他帖子,但它们被限制为十六进制字符串的大小,
因为它们使用Convert.ToString(...)。
EXA:
string str =" FA38";
string binStr = SomeClass.Hex2Bin(str);
这将产生以下输出......
" 1111101000111000"
Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I''ve seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToString(...).
EXA:
string str = "FA38";
string binStr = SomeClass.Hex2Bin(str);
This would produce the following output...
"1111101000111000"
C#2.0 / .NET 2.0:
公共静态字符串Hex2Bin20(字符串s)
{
StringBuilder sb = new StringBuilder();
foreach(s.ToCharArray()中的char c)
{
sb.Append(Convert.ToString(Convert.ToInt32(c.ToStr ing(),16),
2).PadLeft(4,''0'' ));
}
返回sb.ToString();
}
C#3.0 / .NET 3.5:
公共静态字符串Hex2Bin35(字符串s)
{
返回String.Join("" ;,(来自s.ToCharArray()中的c选择
Convert.ToString(Convert.ToInt32(c.ToString(),16),2).PadLeft(4,
''0''))。ToArray());
}
Arne
PS :我想我也会在3.5上使用2.0版本 - 它更具可读性。
C# 2.0 / .NET 2.0:
public static string Hex2Bin20(string s)
{
StringBuilder sb = new StringBuilder();
foreach(char c in s.ToCharArray())
{
sb.Append(Convert.ToString(Convert.ToInt32(c.ToStr ing(), 16),
2).PadLeft(4, ''0''));
}
return sb.ToString();
}
C# 3.0 / .NET 3.5:
public static string Hex2Bin35(string s)
{
return String.Join("", (from c in s.ToCharArray() select
Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4,
''0'')).ToArray());
}
Arne
PS: I think I would use the 2.0 version on 3.5 as well - it is
more readable.
这篇关于十六进制到二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!