Hex encoding to Base64 encoding

G

Guest

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();
}
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

kevin said:
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();
}
 
G

Guest

That helped a ton!

Nicholas Paldino said:
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]
- (e-mail address removed)

kevin said:
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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top