473,508 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hex to string

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: 4e755a357546304d48744a35346e4d632b3739742f4e523947 416d6d6159
3376554a7537665a79767777303d
base64fromhex: NuZ5uF0MHtJ54nMc+79t/NR9GAmmaY3vUJu7fZyvww0=
Nov 16 '05 #1
5 23290
Senna <Se***@discussions.microsoft.com> wrote:
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: 4e755a357546304d48744a35346e4d632b3739742f4e523947 416d6d6159
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Senna <Se***@discussions.microsoft.com> wrote:
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: 4e755a357546304d48744a35346e4d632b3739742f4e523947 416d6d6159
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[i] = (char)(ParseHexDigit(hex[hexIndex++])*16+
ParseHexDigit(hex[hexIndex++]));
}
return new string (result);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Senna <Se***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<snip>
result[i] = (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[i] = tmp;
Nov 16 '05 #5
Joris Dobbelsteen <RE********************@jAoris2k.aTth.cXx> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<snip>
result[i] = (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[i] = 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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

16
6704
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
31156
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
7987
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
8147
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
4760
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
4276
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
3162
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
50136
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
3035
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
4723
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
7223
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,...
0
7114
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...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7488
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...
0
5623
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,...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
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 ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.