hex2bin

  • Thread starter Thread starter i
  • Start date Start date
I

i

Hi,

I'm looking for a way to reverse the effect of this particular
function:

public static string bin2hex(string bindata)
{
byte[] bytes =
Encoding.GetEncoding(1252).GetBytes(bindata);
string hexString = "";
for (int ii = 0; ii < bytes.Length; ii++)
{
hexString += bytes[ii].ToString("x2");
}
return hexString;
}

The signature of the function I'm trying to write would be public
static string hex2bin(string hexdata), but no luck so far.

Any ideas?

Thanks in advance.
 
i said:
I'm looking for a way to reverse the effect of this particular
function:

public static string bin2hex(string bindata)
{
byte[] bytes =
Encoding.GetEncoding(1252).GetBytes(bindata);
string hexString = "";
for (int ii = 0; ii < bytes.Length; ii++)
{
hexString += bytes[ii].ToString("x2");
}
return hexString;
}

The signature of the function I'm trying to write would be public
static string hex2bin(string hexdata), but no luck so far.

It's unfortunate that whoever wrote the function didn't use
Convert.ToBase64(), so that you could more easily use
Convert.FromBase64(). Also, the function above will be slow if bindata
is long, because it is appending to a string rather than using
StringBuilder (the code above is has quadratic performance, i.e. time
taken is proportional to bindata.Length squared in the big-Oh sense).

What you've got to do is take characters out of the string, two at a
time, and convert them into a byte value. One (slow) way to do that is
with int.Parse(bindata.Substring(...), NumberStyles.HexNumber). Another
is to switch on the character values and do the conversion yourself.

Here's one solution:

---8<---
static string hex2bin(string hexdata)
{
if (hexdata == null)
throw new ArgumentNullException("hexdata");
if (hexdata.Length % 2 != 0)
throw new ArgumentException("hexdata should have even length");

byte[] bytes = new byte[hexdata.Length / 2];
for (int i = 0; i < hexdata.Length; i += 2)
bytes[i / 2] = (byte) (HexValue(hexdata) * 0x10
+ HexValue(hexdata[i + 1]));
return Encoding.GetEncoding(1252).GetString(bytes);
}

static int HexValue(char c)
{
int ch = (int) c;
if (ch >= (int) '0' && ch <= (int) '9')
return ch - (int) '0';
if (ch >= (int) 'a' && ch <= (int) 'f')
return ch - (int) 'a' + 10;
if (ch >= (int) 'A' && ch <= (int) 'F')
return ch - (int) 'A' + 10;
throw new ArgumentException("Not a hexadecimal digit.");
}
--->8---

-- Barry
 
//Simple Functions to convert Hex2Bin, Bin2Hex, Dec2Hex, Hex2Dec, Dec2Bin, Bin2Dec in C#

public string hex2Bin(string strHex)
{
int decNumber = hex2Dec(strHex);
return dec2Bin(decNumber);
}
public string bin2Hex(string strBin)
{
int decNumber = bin2Dec(strBin);
return dec2Hex(decNumber);
}

private string dec2Hex(int val)
{
return val.ToString("X");
//return Convert.ToString(val,16);
}

private int hex2Dec(string strHex)
{
return Convert.ToInt16(strHex, 16);
}
private string dec2Bin(int val)
{
return Convert.ToString(val, 2);
}
public int bin2Dec(string strBin)
{
return Convert.ToInt16(strBin, 2);
}


Hi,



I'm looking for a way to reverse the effect of this particular



function:



public static string bin2hex(string bindata)



{



byte[] bytes =



Encoding.GetEncoding(1252).GetBytes(bindata);



string hexString = "";



for (int ii = 0; ii < bytes.Length; ii++)



{



hexString += bytes[ii].ToString("x2");



}



return hexString;



}



The signature of the function I'm trying to write would be public



static string hex2bin(string hexdata), but no luck so far.



Any ideas?



Thanks in advance.
I'm looking for a way to reverse the effect of this particular
function:

public static string bin2hex(string bindata)

byte[] bytes =
Encoding.GetEncoding(1252).GetBytes(bindata);

string hexString = "";
for (int ii = 0; ii < bytes.Length; ii++)

hexString += bytes[ii].ToString("x2");

return hexString;

The signature of the function I'm trying to write would be public
static string hex2bin(string hexdata), but no luck so far.



It's unfortunate that whoever wrote the function didn't use

Convert.ToBase64(), so that you could more easily use

Convert.FromBase64(). Also, the function above will be slow if bindata

is long, because it is appending to a string rather than using

StringBuilder (the code above is has quadratic performance, i.e. time

taken is proportional to bindata.Length squared in the big-Oh sense).



What you've got to do is take characters out of the string, two at a

time, and convert them into a byte value. One (slow) way to do that is

with int.Parse(bindata.Substring(...), NumberStyles.HexNumber). Another

is to switch on the character values and do the conversion yourself.



Here's one solution:



---8<---

static string hex2bin(string hexdata)

{

if (hexdata == null)

throw new ArgumentNullException("hexdata");

if (hexdata.Length % 2 != 0)

throw new ArgumentException("hexdata should have even length");



byte[] bytes = new byte[hexdata.Length / 2];

for (int i = 0; i < hexdata.Length; i += 2)

bytes[i / 2] = (byte) (HexValue(hexdata) * 0x10

+ HexValue(hexdata[i + 1]));

return Encoding.GetEncoding(1252).GetString(bytes);

}



static int HexValue(char c)

{

int ch = (int) c;

if (ch >= (int) '0' && ch <= (int) '9')

return ch - (int) '0';

if (ch >= (int) 'a' && ch <= (int) 'f')

return ch - (int) 'a' + 10;

if (ch >= (int) 'A' && ch <= (int) 'F')

return ch - (int) 'A' + 10;

throw new ArgumentException("Not a hexadecimal digit.");

}

--->8---



-- Barry

Submitted via EggHeadCafe - Software Developer Portal of Choice
Flat file Database with LINQ and ASP.NET jQuery Page Methods
http://www.eggheadcafe.com/tutorial...with-linq-and-aspnet-jquery-page-methods.aspx
 
Back
Top