By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,315 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,315 IT Pros & Developers. It's quick & easy.

Hex encoding to Base64 encoding

P: n/a
DISCLAIMER: I know what the words mean (i.e. by definition), but I in know
way pretend to understand the specifics of either, therefore I may need a
basic primer before I can accomplish this task, but the instructions I
received do not seem correct.

I need to covert a hex string to base64 and was told to
1. convert from the HEX string to a byte array
2. convert the byte array to Base64 encoding
3. the two encodings would produce the same byte array

I search MSDN and this forum and came up with the following (the database
field is NVARCHAR, therefore I used the UnicodeEncoding object):

Did I not understand the instructions or is this impossible?

static void Main(){
string sHex = "38-B8-B7-22-2A-E6-C6-D9-42-C7-63-40-69-DB-E0-66",
sHexBytes="", sBase64Bytes="", sBse64="";
string sLine =
"\n*********************************************** ***********\n";

byte[] aHexByte, a64Byte;

UnicodeEncoding uencode = new UnicodeEncoding();
//ASCIIEncoding ascii = new ASCIIEncoding();

//get byte array from V3 hex encoded string
//aHexByte = uencode.GetBytes(sHex.Replace("-",""));
aHexByte = uencode.GetBytes(sHex);

//convert this to Base64 encoding
sBse64 = Convert.ToBase64String(aHexByte);

//get byte array from the Base64 encode string
a64Byte = uencode.GetBytes(sBse64);

//display on screen
foreach(byte b in aHexByte){
sHexBytes += b.ToString() + "/";
}

foreach(byte b in a64Byte){
sBase64Bytes += b.ToString() + "/";
}

Console.WriteLine("Hex encoded password:\n" + sHex + sLine);
Console.WriteLine("Bytes from Hex encode password:\n" + sHexBytes + sLine);
Console.WriteLine("Base64 encoding of Bytes from Hex string:\n" + sBse64 +
sLine);
Console.WriteLine("Bytes from Base64 encoded strng:\n" + sBase64Bytes +
sLine);

Console.ReadLine();
}
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Kevin,

This isn't going to do what you want. Technically, the sHex string is
not a hex string, but rather, a series of hex numbers delimited with the '-'
character.

Also, you are converting the string to bytes using a text encoding. I
believe what you want are the actual bytes. In order to do that, you want
something like this:

static void Main()
{
// The hex string.
string sHex = "38-B8-B7-22-2A-E6-C6-D9-42-C7-63-40-69-DB-E0-66";

// Split into the individual bytes. Each two digit hex number is one
byte.
string[] byteStrings = sHex.Split(new char[1]{'-'});

// Now cycle through the strings and convert each one to a byte.
Allocate
// the byte array first.
byte[] bytes = new byte[byteStrings.Length];

// Cycle through the strings.
for (int index = 0; index < bytes.Length; index++)
{
// Perform the conversion.
bytes[index] = byte.Parse(byteStrings[index],
NumberStyles.AllowHexSpecifier);
}

// Now, convert the byte array to base 64.
string base64 = Convert.ToBase64String(bytes);

}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"kevin" <ke***@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
DISCLAIMER: I know what the words mean (i.e. by definition), but I in know
way pretend to understand the specifics of either, therefore I may need a
basic primer before I can accomplish this task, but the instructions I
received do not seem correct.

I need to covert a hex string to base64 and was told to
1. convert from the HEX string to a byte array
2. convert the byte array to Base64 encoding
3. the two encodings would produce the same byte array

I search MSDN and this forum and came up with the following (the database
field is NVARCHAR, therefore I used the UnicodeEncoding object):

Did I not understand the instructions or is this impossible?

static void Main(){
string sHex = "38-B8-B7-22-2A-E6-C6-D9-42-C7-63-40-69-DB-E0-66",
sHexBytes="", sBase64Bytes="", sBse64="";
string sLine =
"\n*********************************************** ***********\n";

byte[] aHexByte, a64Byte;

UnicodeEncoding uencode = new UnicodeEncoding();
//ASCIIEncoding ascii = new ASCIIEncoding();

//get byte array from V3 hex encoded string
//aHexByte = uencode.GetBytes(sHex.Replace("-",""));
aHexByte = uencode.GetBytes(sHex);

//convert this to Base64 encoding
sBse64 = Convert.ToBase64String(aHexByte);

//get byte array from the Base64 encode string
a64Byte = uencode.GetBytes(sBse64);

//display on screen
foreach(byte b in aHexByte){
sHexBytes += b.ToString() + "/";
}

foreach(byte b in a64Byte){
sBase64Bytes += b.ToString() + "/";
}

Console.WriteLine("Hex encoded password:\n" + sHex + sLine);
Console.WriteLine("Bytes from Hex encode password:\n" + sHexBytes +
sLine);
Console.WriteLine("Base64 encoding of Bytes from Hex string:\n" + sBse64 +
sLine);
Console.WriteLine("Bytes from Base64 encoded strng:\n" + sBase64Bytes +
sLine);

Console.ReadLine();
}

Nov 17 '05 #2

P: n/a
That helped a ton!

"Nicholas Paldino [.NET/C# MVP]" wrote:
Kevin,

This isn't going to do what you want. Technically, the sHex string is
not a hex string, but rather, a series of hex numbers delimited with the '-'
character.

Also, you are converting the string to bytes using a text encoding. I
believe what you want are the actual bytes. In order to do that, you want
something like this:

static void Main()
{
// The hex string.
string sHex = "38-B8-B7-22-2A-E6-C6-D9-42-C7-63-40-69-DB-E0-66";

// Split into the individual bytes. Each two digit hex number is one
byte.
string[] byteStrings = sHex.Split(new char[1]{'-'});

// Now cycle through the strings and convert each one to a byte.
Allocate
// the byte array first.
byte[] bytes = new byte[byteStrings.Length];

// Cycle through the strings.
for (int index = 0; index < bytes.Length; index++)
{
// Perform the conversion.
bytes[index] = byte.Parse(byteStrings[index],
NumberStyles.AllowHexSpecifier);
}

// Now, convert the byte array to base 64.
string base64 = Convert.ToBase64String(bytes);

}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"kevin" <ke***@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
DISCLAIMER: I know what the words mean (i.e. by definition), but I in know
way pretend to understand the specifics of either, therefore I may need a
basic primer before I can accomplish this task, but the instructions I
received do not seem correct.

I need to covert a hex string to base64 and was told to
1. convert from the HEX string to a byte array
2. convert the byte array to Base64 encoding
3. the two encodings would produce the same byte array

I search MSDN and this forum and came up with the following (the database
field is NVARCHAR, therefore I used the UnicodeEncoding object):

Did I not understand the instructions or is this impossible?

static void Main(){
string sHex = "38-B8-B7-22-2A-E6-C6-D9-42-C7-63-40-69-DB-E0-66",
sHexBytes="", sBase64Bytes="", sBse64="";
string sLine =
"\n*********************************************** ***********\n";

byte[] aHexByte, a64Byte;

UnicodeEncoding uencode = new UnicodeEncoding();
//ASCIIEncoding ascii = new ASCIIEncoding();

//get byte array from V3 hex encoded string
//aHexByte = uencode.GetBytes(sHex.Replace("-",""));
aHexByte = uencode.GetBytes(sHex);

//convert this to Base64 encoding
sBse64 = Convert.ToBase64String(aHexByte);

//get byte array from the Base64 encode string
a64Byte = uencode.GetBytes(sBse64);

//display on screen
foreach(byte b in aHexByte){
sHexBytes += b.ToString() + "/";
}

foreach(byte b in a64Byte){
sBase64Bytes += b.ToString() + "/";
}

Console.WriteLine("Hex encoded password:\n" + sHex + sLine);
Console.WriteLine("Bytes from Hex encode password:\n" + sHexBytes +
sLine);
Console.WriteLine("Base64 encoding of Bytes from Hex string:\n" + sBse64 +
sLine);
Console.WriteLine("Bytes from Base64 encoded strng:\n" + sBase64Bytes +
sLine);

Console.ReadLine();
}


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.