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.
Related
Is this the proper way to Encode and Decode a byte array using Lz4net?
byte[] filedata = File.ReadAllBytes(#"C:\Test.txt");
byte[] encodedfileData = LZ4.LZ4Codec.Encode(filedata, 0, filedata.Length);
byte[] decodedfileData = LZ4.LZ4Codec.Decode(encodedfileData, 0, encodedfileData.Length, 0);
decodedfileData returns 0 bytes
I have gone through LZ4 github, but I am not getting any idea what's wrong. So what is the proper way to Encode and Decode a byte array using LZ4?
You can try this:
byte[] filedata = File.ReadAllBytes(#"C:\Test.txt");
byte[] compressed = LZ4.LZ4Codec.Wrap(in);
byte[] uncompressed = LZ4.LZ4Codec.UnWrap(compressed);
You need to put the unpacked size (filedata.Length) as the last parameter:
byte[] decodedfileData = LZ4.LZ4Codec.Decode(
encodedfileData,
0,
encodedfileData.Length,
filedata.Length);
I've found this sample code to convert an image to base 64, I'm not sure how to pass an image into it though. I want to be able to give a path to a specific directory. I've managed to find a file with this code:
byte[] ImageData = File.ReadAllBytes("storage/emulated/0/DCIM/Camera/img.jpg");
But I need to pass that into the following code.
public string ImageToBase64(Image image,
System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
Hope you can help. Thanks.
byte[] ImageData = File.ReadAllBytes(path_to_file);
string base64String = Convert.ToBase64String(ImageData);
I am trying to convert an image to Base64 string in monotouch iOS app. I used below coding to do it. but error occurred.
using (Image image = Image.FromFile(Path))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
now probelm is, Save does not have any definition. And RawFormat does not have definiton and no extension method. what can i do now?
if the image already exists on disk, it is simpler to do
byte[] data = File.ReadAllBytes(path);
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
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