Convert from Base 64 to hexadecimal value
-
I have a base64 input("NjU0MDgA=") in a XML file which I am trying to convert to hexadecimal with the following code in C#: ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytes = Encoding.ASCII.GetBytes("NjU0MDgA="); string hex = BitConverter.ToString(bytes); hex.Replace("-", String.Empty); The 'hex' output is in the following format: 4D5459334D5445344D4467413D However, required output should be in the format: #44ff11 (example)
-
I have a base64 input("NjU0MDgA=") in a XML file which I am trying to convert to hexadecimal with the following code in C#: ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytes = Encoding.ASCII.GetBytes("NjU0MDgA="); string hex = BitConverter.ToString(bytes); hex.Replace("-", String.Empty); The 'hex' output is in the following format: 4D5459334D5445344D4467413D However, required output should be in the format: #44ff11 (example)
I'm confused. NjU0MDgA= decoded is "65408" which is FF80 in hex.
-
I'm confused. NjU0MDgA= decoded is "65408" which is FF80 in hex.
Apologies for any confusion.. I just gave sample values. I am trying to get the values you mentioned programmatically.
-
I have a base64 input("NjU0MDgA=") in a XML file which I am trying to convert to hexadecimal with the following code in C#: ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytes = Encoding.ASCII.GetBytes("NjU0MDgA="); string hex = BitConverter.ToString(bytes); hex.Replace("-", String.Empty); The 'hex' output is in the following format: 4D5459334D5445344D4467413D However, required output should be in the format: #44ff11 (example)
I use
Encoding.GetString ( System.Convert.FromBase64String ( Source ) )
, which yieldsTmpVME1EZ0E9
from your sample data. -
I use
Encoding.GetString ( System.Convert.FromBase64String ( Source ) )
, which yieldsTmpVME1EZ0E9
from your sample data.And hex should be "4E6A55304D4467413D"
-
And hex should be "4E6A55304D4467413D"
I'm not following you. From
TmpVME1EZ0E9
I get546D70564D4531455A304539
The sequence you state yieldsNjU0MDgA=
; I think you need to read up on Base64; it isn't just base64 values of ASCII characters. http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.110).aspx[^] http://en.wikipedia.org/wiki/Base_64[^] -
I'm not following you. From
TmpVME1EZ0E9
I get546D70564D4531455A304539
The sequence you state yieldsNjU0MDgA=
; I think you need to read up on Base64; it isn't just base64 values of ASCII characters. http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.110).aspx[^] http://en.wikipedia.org/wiki/Base_64[^]Thanks for your help. I was able to solve it. Following is what I ended up with: byte[] decoded = Convert.FromBase64String("UUEncoded string to be decoded"); String value = ASCIIEncoding.ASCII.GetString(decoded); int output = Convert.ToInt32(value); // Convert int to hex format and reverse the byte String.Format("{0:X02}{1:X02}{2:X02}", (output & 0x0000FF) >> 0, (output & 0x00FF00) >> 8, (output & 0xFF0000) >> 16);