473,581 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hex encoding to Base64 encoding

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.GetByte s(sHex.Replace( "-",""));
aHexByte = uencode.GetByte s(sHex);

//convert this to Base64 encoding
sBse64 = Convert.ToBase6 4String(aHexByt e);

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

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

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

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

Console.ReadLin e();
}
Nov 17 '05 #1
2 29285
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.Len gth];

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

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

}

Hope this helps.

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

"kevin" <ke***@discussi ons.microsoft.c om> wrote in message
news:DB******** *************** ***********@mic rosoft.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.GetByte s(sHex.Replace( "-",""));
aHexByte = uencode.GetByte s(sHex);

//convert this to Base64 encoding
sBse64 = Convert.ToBase6 4String(aHexByt e);

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

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

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

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

Console.ReadLin e();
}

Nov 17 '05 #2
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.Len gth];

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

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

}

Hope this helps.

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

"kevin" <ke***@discussi ons.microsoft.c om> wrote in message
news:DB******** *************** ***********@mic rosoft.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.GetByte s(sHex.Replace( "-",""));
aHexByte = uencode.GetByte s(sHex);

//convert this to Base64 encoding
sBse64 = Convert.ToBase6 4String(aHexByt e);

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

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

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

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

Console.ReadLin e();
}


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
26124
by: Rodney Pont | last post by:
I've got the example below to set up phpOpenTracker to log exit URL's but I'm having trouble getting it to work. I have played with the quotes and changed the \\2 to $3 and got the url in there but I can't get it to base64 encode it. I'm new to PHP and any help on getting the encoding to work would be appreciated. <?php function...
14
3520
by: Sebastian Meyer | last post by:
Hi newsgroup, i am trying to replace german special characters in strings like str = re.sub('ö', 'oe', str) When i work with this, i always get the message UniCode Error: ASCII decoding error : ordinal not in range(128) Yes i have googled, i searched the faq, manual and python library and searched all known soruces of information. I...
3
5388
by: wenmang | last post by:
Hi, I ma thinking whether to use Base64 encoding to encode the binary content in the XML file. I have done some simple calculations, it seems to me that the size for encoded content increases by ~30%, is this the drawback for using the encoding scheme? Thanks.
5
437
by: bmth | last post by:
Hi all I am trying to contvert a message from my server to unicode. I initially got a base64 endoced byte message. After some debugging I saw the base64 string had "\0" in the end wich resulted in an error when converting from base64. So I cut it away and can now see my string in byte. I tried calling Encoding.Unicode.GetString() and sent...
8
7571
by: Xarky | last post by:
Hi, I am downloading a GIF file(as a mail attachement) with this file format, Content-Transfer-Encoding: base64; Now I am writing the downloaded data to a file with this technique: streamWriter = new StreamWriter(@startupPath+"\\"+filename, false); streamWriter.WriteLine(data); I am not specifying any file Encoding. When I try to open...
6
3248
by: brianbasquille | last post by:
Hello all, Am writing an small basic e-mail client and i'm having difficulty encoding images to Base64. When i send an attachment image with it to Outlook Express, it seems as if it only converts a certain amount of the image to Base64. The message does appear as attached but doesn't render correctly and is a much smaller filesize than...
8
8482
by: Andy | last post by:
Hello All: I have a windows application that I need to encode a string using Unicode. The example I have been given to use is a Web-Version. Below is the webcode. Response.ContentEncoding=System.Text.Encoding.Unicode; Response.ContentType = "application/postscript"; Response.Buffer =true;...
13
14740
by: aruna.eies.eng | last post by:
i am currently trying to convert data into binary data.for that i need to know how to achieve it in c language and what are the libraries that we can use. so if any one can send me a sample code or send me the library file which helps that is really grateful. aruna.
1
6829
by: maxxxxel | last post by:
Hi Can anyone help me with some asp code , I changed the code to use CDO.message instead of the old cdont.sys to send mail from a ASP webpage which works fine. Our problem is that when we send mail externally to a internet email site like Gmail the PDF is sent but is corrupted because CDOSYS ends up using binary encoding rather than Base64...
0
7868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8175
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6553
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3805
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.