Hex to string

G

Guest

Hi,

Have a symmetric encryption method that returns a base64 string. I then get
the hex representation of that string with the code below

public static string Base64ToHex(string input)
{
StringBuilder sb = new StringBuilder();
byte[] inputBytes = Encoding.UTF8.GetBytes(input);

foreach(byte b in inputBytes)
{
sb.Append(string.Format("{0:x2}", b));
}

return sb.ToString();
}

My question is: How do I get the hex string back to base64 string?

Ex.
base64: NuZ5uF0MHtJ54nMc+79t/NR9GAmmaY3vUJu7fZyvww0=
hexfrombase64: 4e755a357546304d48744a35346e4d632b3739742f4e523947416d6d6159
3376554a7537665a79767777303d
base64fromhex: NuZ5uF0MHtJ54nMc+79t/NR9GAmmaY3vUJu7fZyvww0=
 
J

Jon Skeet [C# MVP]

Senna said:
Have a symmetric encryption method that returns a base64 string. I then get
the hex representation of that string with the code below

public static string Base64ToHex(string input)
{
StringBuilder sb = new StringBuilder();
byte[] inputBytes = Encoding.UTF8.GetBytes(input);

foreach(byte b in inputBytes)
{
sb.Append(string.Format("{0:x2}", b));
}

return sb.ToString();
}

My question is: How do I get the hex string back to base64 string?

Ex.
base64: NuZ5uF0MHtJ54nMc+79t/NR9GAmmaY3vUJu7fZyvww0=
hexfrombase64: 4e755a357546304d48744a35346e4d632b3739742f4e523947416d6d6159
3376554a7537665a79767777303d
base64fromhex: NuZ5uF0MHtJ54nMc+79t/NR9GAmmaY3vUJu7fZyvww0=

Well, you can use Byte.Parse, or do it manually fairly easily. I have
to ask though - why are you converting into base64 and then into hex?
Why not just convert it from the binary (which is the natural low-down
result of the encryption) into hex in the first place, if you
absolutely need hex?
 
J

Jon Skeet [C# MVP]

Senna said:
Have a symmetric encryption method that returns a base64 string. I then get
the hex representation of that string with the code below

public static string Base64ToHex(string input)
{
StringBuilder sb = new StringBuilder();
byte[] inputBytes = Encoding.UTF8.GetBytes(input);

foreach(byte b in inputBytes)
{
sb.Append(string.Format("{0:x2}", b));
}

return sb.ToString();
}

My question is: How do I get the hex string back to base64 string?

Ex.
base64: NuZ5uF0MHtJ54nMc+79t/NR9GAmmaY3vUJu7fZyvww0=
hexfrombase64: 4e755a357546304d48744a35346e4d632b3739742f4e523947416d6d6159
3376554a7537665a79767777303d
base64fromhex: NuZ5uF0MHtJ54nMc+79t/NR9GAmmaY3vUJu7fZyvww0=

My previous answer was definitely somewhat suboptimal, I'm afraid.
Here's a method to actually do what you want.

Note that because you used UTF-8 and everything in Base64 is within the
ASCII character set, we don't need to do any conversion beyond casting
the hex value to a char. (Nor did you need to for your conversion,
btw.)

It could certainly be faster, but it'll do you fine, I'm sure:

static int ParseHexDigit(char c)
{
if (c >= '0' && c <= '9')
{
return c-'0';
}
if (c >= 'a' && c <= 'f')
{
return c-'a'+10;
}
if (c >= 'A' && c <= 'F')
{
return c-'A'+10;
}
throw new ArgumentException ("Invalid hex character");
}

public static string ParseHex(string hex)
{
char[] result = new char[hex.Length/2];

int hexIndex=0;
for (int i=0; i < result.Length; i++)
{
result = (char)(ParseHexDigit(hex[hexIndex++])*16+
ParseHexDigit(hex[hexIndex++]));
}
return new string (result);
}
 
J

Jon Skeet [C# MVP]

Senna said:
In some cases I need to have the encrypted string as querystring parameter
and some of the base64 charachers doen't go well in a url. I tried to
urlencode but it just get wrong. So I thougth I would convert it to a hex
string instead, that is url friendly.

Then I would suggest going straight from byte array to hex then, rather
than using Base64 encoding. Alternatively, something I've used
successfully in the past is a variation on Base64 which uses a URL-
friendly set of symbols. You could just use the existing Base64 methods
and then replace the dodgy characters, or you could write your own
Base64 methods.
Thats the reason. :) You don't think you could show the whole method code?
Would really help out a lot.

See my other post.
 
J

Joris Dobbelsteen

result = (char)(ParseHexDigit(hex[hexIndex++])*16+
ParseHexDigit(hex[hexIndex++]));

<snip>

This would be rather unsafe IMHO...
I would rather make this:

char tmp;

tmp = (char)(ParseHexDigit(hex[hexIndex++])*16;
tmp += (char)(ParseHexDigit(hex[hexIndex++]);
result = tmp;
 
J

Jon Skeet [C# MVP]

Joris Dobbelsteen said:
result = (char)(ParseHexDigit(hex[hexIndex++])*16+
ParseHexDigit(hex[hexIndex++]));

<snip>

This would be rather unsafe IMHO...
I would rather make this:

char tmp;

tmp = (char)(ParseHexDigit(hex[hexIndex++])*16;
tmp += (char)(ParseHexDigit(hex[hexIndex++]);
result = tmp;


In C, you would be right - in C#, the order of everything is well-
defined. Assuming it's the ordering which is the reason you think it's
unsafe...
 

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