In example
string newString = Convert.ToBase64String(byte[] array)
How would I go about converting newString to get a byte[] (byte array)?
Convert.FromBase64String(newString)
byte[] data = Convert.FromBase64String(newString);
string decodedString = Encoding.UTF8.GetString(data);
byte[] array= Convert.FromBase64String(newString);
Related
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 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
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
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you convert a string to ascii to binary in C#?
How to convert string such as "Hello" to Binary sequence as 1011010 ?
Try this:
string str = "Hello";
byte []arr = System.Text.Encoding.ASCII.GetBytes(str);
string result = string.Empty;
foreach(char ch in yourString)
{
result += Convert.ToString((int)ch,2);
}
this will translate "Hello" to 10010001100101110110011011001101111
string testString = "Hello";
UTF8Encoding encoding = new UTF8Encoding();
byte[] buf = encoding.GetBytes(testString);
StringBuilder binaryStringBuilder = new StringBuilder();
foreach (byte b in buf)
{
binaryStringBuilder.Append(Convert.ToString(b, 2));
}
Console.WriteLine(binaryStringBuilder.ToString());
Use the BitConverter to get the bytes of the string and then format these bytes to their binary representation:
byte[] bytes = System.Text.Encoding.Default.GetBytes( "Hello" );
StringBuilder sb = new StringBuilder();
foreach ( byte b in bytes )
{
sb.AppendFormat( "{0:B}", b );
}
string binary = sb.ToString();
How can I take a List and turn it into a byte array.
I thought there might be some clever LINQ options for it but am unsure eg/List.ForEach
Depends on which encoding you want to use to convert the string to a byte[] but here's a sample for ASCII. It can be substituted for pretty much any encoding type
List<string> data = ...
byte[] dataAsBytes = data
.SelectMany(s => Text.Encoding.ASCII.GetBytes(s))
.ToArray();
with a simple foreach loop:
(pseudocode)
List<byte[]> bytes = new List<byte[]>();
ForEach string el in somelist
{
byte[] arr;
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
arr = encoding.GetBytes(el);
bytes.add(arr);
}