I use in my C++/CLI project ToBase64String to give a string like /MnwRx7kRZEQBxLZEkXndA== I want to convert this string to Hexadecimal representation, How I can do that in C++/CLI or C#?
FromBase64String will take the string to bytes
byte[] bytes = Convert.FromBase64String(string s);
Then, BitConverter.ToString() will convert a byte array to a hex string ( byte[] to hex string )
string hex = BitConverter.ToString(bytes);
Convert the string to a byte array and then do a byte to hex conversion
string stringToConvert = "/MnwRx7kRZEQBxLZEkXndA==";
byte[] convertedByte = Encoding.Unicode.GetBytes(stringToConvert);
string hex = BitConverter.ToString(convertedByte);
Console.WriteLine(hex);
public string Base64ToHex(string strInput)
{
try
{
var bytes = Convert.FromBase64String(strInput);
var hex = BitConverter.ToString(bytes);
return hex.Replace("-", "").ToLower();
}
catch (Exception)
{
return "-1";
}
}
On the contrary: https://stackoverflow.com/a/61224761/3988122
Related
Here is my problem, Im trying to Encode the response of my webservice with the following Code.
public static string ConvertToUTF8(string Cadena)
{
string mensajeex = Cadena;
Encoding utf8 = Encoding.UTF8;
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte array.
byte[] unicodeBytes = unicode.GetBytes(mensajeex);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, utf8, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string Utf8string = new string(asciiChars);
// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", mensajeex);
Console.WriteLine("Ascii converted string: {0}", Utf8string);
return Utf8string;
}
And actually it works! But when I try to Encode a string and then pass through an exception as a Message property like this
throw new Exception(XMLHelper.ConvertToUTF8(Message));
It give me the response wrong like:
El valor 'R' no es vĂ¡lido segú
Any ideas? Thanks
byte[] bytes = Encoding.ASCII.GetBytes(Input);
sbyte[] signed = Array.ConvertAll(bytes, b => unchecked((sbyte)b));
byte[] y = (byte[])(object)signed;
string base64String = Convert.ToBase64String(y, 0, bytes.Length);
string url = "data:image/png;base64," + base64String;
return base64String;
I am sending byte array of an image from android app and saving in database.But when i am trying to convert it in base64 string in c# it is not showing the image.any please help.
Here is the byte array string received from app
[B#27943481
And c# converted base64 string is
data:image/png;base64,W0JAMjc5NDM0ODE=
Please help to convert it in c#.
the string stored into the Input Property is encoded in Ascii? if so, use
byte[] bytes = Encoding.ASCII.GetBytes(Input); // or Encoding.UTF8.GetBytes(Input)
string base64String = Convert.ToBase64String(bytes);
string url = "data:image/png;base64," + base64String;
return base64String;
that should do the job.
I am writing a simple ftp client with c#.
I am not pro in c#. Is there any way to convert string to byte[] and write it to the socket?
for example for introducing username this is the socket content:
5553455220736f726f7573680d0a
and ASCII equivalent is:
USER soroush
I want a method to convert string. Something like this:
public byte[] getByte(string str)
{
byte[] ret;
//some code here
return ret;
}
Try
byte[] array = Encoding.ASCII.GetBytes(input);
// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
Encoding encoding = Encoding.UTF8; //or below line
//System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
and
// C# to convert a byte array to a string.
byte [] dBytes = ...
string str;
Encoding enc = Encoding.UTF8; //or below line
//System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
str = enc.GetString(dBytes);
i am using Encoding and decoding :
For Encoding:
private string EncodeServerName(string ServerName)
{
byte[] NameEncodein = new byte[ServerName.Length];
NameEncodein = System.Text.Encoding.UTF8.GetBytes(ServerName);
string EcodedName = Convert.ToBase64String(NameEncodein);
return EcodedName;
}
and Decoding:
public string DecoAndGetServerName(string Servername)
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder strDecoder = encoder.GetDecoder();
byte[] to_DecodeByte = Convert.FromBase64String(Servername);
int charCount = strDecoder.GetCharCount(to_DecodeByte, 0, to_DecodeByte.Length);
char[] decoded_char = new char[charCount];
strDecoder.GetChars(to_DecodeByte, 0, to_DecodeByte.Length, decoded_char,0);
string Name = new string(decoded_char);
return Name;
}
I am sending ServerName:DEV-SQL1\SQL2008
It is encoded:REVWLVNRTDFcU1FMMjAwOA==
Again i want to decode but getting Exception:in line:
byte[] to_DecodeByte = Convert.FromBase64String(Servername);
Exception IS:
`The input is not a valid Base-64 string as it contains a non-base 64 character,
more than two padding characters, or a non-white space character among the padding characters.`
How to solve this issue.
Please Help Me
Your code seems way too complex :-), here is one that works:
public static string EncodeServerName(string serverName)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(serverName));
}
public static string DecodeServerName(string encodedServername)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(encodedServername));
}
the same code works for me, which you written in DecoAndGetServerName().
the thing is, you need to pass ENCODED STRING to your DecoAndGetServerName() function,
which might be encoded like :
string Servername=Convert.ToBase64String(Encoding.UTF8.GetBytes("serverName"));
That's why you got that Error The input is not a valid Base-64 string as it contains a non-base 64 character,....
I have the following code:
string s = "2563MNBJP89256666666685755854";
Byte[] bytes = encoding.GetBytes(s);
string hex = "";
foreach (byte b in bytes)
{
int c=b;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(c.ToString()));
}
It prints the hex values . How can I add it in a vector ob bytes like this?
new byte [0x32,0x35..]
In hex I have : 323536....and so on. The next step is to add then in a byte[] vector in the following format 0x32,0x35..and so on; How to do this?
THX
Isn't bytes already the list of bytes you want?
C#: System.Text.Encoding.ASCII.GetBytes("test")
For C# you could try
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(strVar);//strVar is the string variable
in C# you can use Encoding.GetBytes Method