C# convert byte[] to string with a charset - c#

In C# How can we convert byte[] to string with a charset.eg utf8,SHIFT_JIS,and more
.I know Encoding.UTF8
byte[] inputBytes =SupportClass.ToByteArray(readBytes);
StringBuilder result;
result.Append(System.Text.Encoding.UTF8.GetString(inputBytes,0,inputBytes.Length));//get unreadable code.
my question is how can I get the result from inputBytes with a special charset,like java
StringBuffer result.append(new String(buffer, "SJIS"));

System.Text.Encoding enc = System.Text.Encoding.GetEncoding("shift-jis");
result.Append(enc.GetString(inputBytes,0,inputBytes.Length));
See this article:
http://msdn.microsoft.com/en-us/library/aa332097(v=vs.71).aspx

Instead of Encoding.UTF8, use Encoding.GetEncoding.
E.g.
private static readonly Encoding SHIFT_JIS = Encoding.GetEncoding("Shift_JIS");
SHIFT_JIS.GetString(inputBytes,0,inputBytes.Length)

Related

Sending encrypted data via TCP (“Bad Data” exception)

How can i send illegal charecters from tpc client to tcp server.
This is an example of what the encrypted gibberish looks like:
https://i.stack.imgur.com/wfZdm.png
How can i send this pice of gibberish to either my client or server?
This is my encryption & decryption code
public static string Decrypt(string data)
{
byte[] dataToDecrypt = StringToByteArray(data);
byte[] decryptedData;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKey);
decryptedData = rsa.Decrypt(dataToDecrypt, false);
}
UnicodeEncoding byteConverter = new UnicodeEncoding();
return ByteArrayToString(decryptedData);
}
public static string Encrypt(string data, string publicKey)
{
UnicodeEncoding byteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = StringToByteArray(data);
byte[] encryptedData;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
encryptedData = rsa.Encrypt(dataToEncrypt, false);
}
return ByteArrayToString(encryptedData);
}
public static byte[] StringToByteArray(string data)
{
return Encoding.ASCII.GetBytes(data);
}
public static string ByteArrayToString(byte[] bytes)
{
return Encoding.ASCII.GetString(bytes);
}
I have made it so the client and the server share eachothers public keys but i am getting Exception "Bad data". One more thing if i send encrypted data from a client to the server which data is 128 bytes the server receives only 78 bytes for example
There's a few things wrong with your code:
You shouldn't be using String at all.
String is meant for text, not arbitrary binary data (I assume you got this impression from C or PHP where their string types are really just synonyms for - or thin-wrappers over - a byte-array).
Keep the Byte[] buffer you get from rs.Encrypt and pass that directly to your Socket, TcpClient or NetworkStream that you're using. You'll need to define a binary protocol with length-prefix though.
Encoding.ASCII.GetBytes will convert the UTF-16LE-encoded characters in the String data instance to 7-bit ASCII, it does this by replacing characters with values above 0x7F with '?' - this is not what you want! (and this is what's causing the garbage output on your screen: those "illegal characters" are byte-values above 0x7F that are outside ASCII's 7-bit range. From the documentation:
It uses replacement fallback to replace each string that it cannot encode and each byte that it cannot decode with a question mark ("?") character.
If you really do want to transmit data over the network using human-readable text then use Base64 encoding: Convert.ToBase64String( Byte[] buffer ) and convert it back using Convert.FromBase64String( String s ) at the receiving end - but you'll still need to length-prefix or delimit your data.

How do I write a byteArray to a specific path

I currently have a method which is taking a picture and saving it..
Once saved I call this method to encrypt the file from a string path
but im not sure how to save it.. I wanted to do
string path = #"C:/somePath"
File.WriteAllBytes(path);
but that doesnt work obv. So how do I properly save a bytearray?
string key = GetUniqueKey(32);
byte[] encKey = Encoding.UTF8.GetBytes(key);
byte[] imgBytes = File.ReadAllBytes(path);
byte[] ebytes = encrypt.AESEncrypt(imgBytes, encKey);
File.WriteAllBytes();
If you actually check the parameters it's asking for, you need to provide the bytes as well as the path.
So:
string path = #"C:/somePath/somefile.png"
string key = GetUniqueKey(32);
byte[] encKey = Encoding.UTF8.GetBytes(key);
byte[] imgBytes = File.ReadAllBytes(path);
byte[] ebytes = encrypt.AESEncrypt(imgBytes, encKey);
File.WriteAllBytes(path, ebytes);
Method's signature is:
File.WriteAllBytes(string path, byte[] bytes)

Is it possible to read correctly text file with wrong BOM in C#?

I have a text file that has wrong BOM (FF FE, but the real encoding of text is ISO-8859-9).
I try this code:
Encoding encoding = Encoding.GetEncoding("iso-8859-9");
string content = File.ReadAllText(#"D:\Documents\test.txt", encoding);
But it doesn't work - the charset is broken.
Can anybody help me to read this file?
As Adriano already mentioned in his comment, you probably need to skip the first two bytes of the file. This can be done by using
byte[] rawdata = File.ReadAllBytes("...");
byte[] correctedRawdata = rawdata.Skip(2).ToArray();
Encoding encoder = Encoding.GetEncoding("iso-8859-9");
string text = encoder.GetString(correctedRawdata);

How To Convert "Byted" String Back to Byte Without String [duplicate]

I have a Base64 byte[] array which is transferred from a stream which i need to convert it to a normal byte[] how to do this ?
You have to use Convert.FromBase64String to turn a Base64 encoded string into a byte[].
This may be helpful
byte[] bytes = System.Convert.FromBase64String(stringInBase64);
Try
byte[] incomingByteArray = receive...; // This is your Base64-encoded bute[]
byte[] decodedByteArray =Convert.FromBase64String (Encoding.ASCII.GetString (incomingByteArray));
// This work because all Base64-encoding is done with pure ASCII characters
You're looking for the FromBase64Transform class, used with the CryptoStream class.
If you have a string, you can also call Convert.FromBase64String.
I've written an extension method for this purpose:
public static byte[] FromBase64Bytes(this byte[] base64Bytes)
{
string base64String = Encoding.UTF8.GetString(base64Bytes, 0, base64Bytes.Length);
return Convert.FromBase64String(base64String);
}
Call it like this:
byte[] base64Bytes = .......
byte[] regularBytes = base64Bytes.FromBase64Bytes();
I hope it helps someone.

Converting a string encoded in utf8 to unicode in C#

I've got this string returned via HTTP Post from a URL in a C# application, that contains some chinese character eg:
Gelatos® Colors Gift Set中文
Problem is I want to convert it to
Gelatos® Colors Gift Set中文
Both string are actually identical but encoded differently. I understand in C# everything is UTF16. I've tried reading alof of postings here regarding converting from one encoding to the other but no luck.
Hope someone could help.
Here's the C# code:
WebClient wc = new WebClient();
json = wc.DownloadString("http://mysite.com/ext/export.asp");
textBox2.Text = "Receiving orders....";
//convert the string to UTF16
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
Encoding utf8 = Encoding.UTF8;
byte[] asciiBytes = ascii.GetBytes(json);
byte[] utf8Bytes = utf8.GetBytes(json);
byte[] unicodeBytes = Encoding.Convert(utf8, unicode, utf8Bytes);
string sOut = unicode.GetString(unicodeBytes);
System.Windows.Forms.MessageBox.Show(sOut); //doesn't work...
Here's the code from the server:
<%#CodePage = 65001%>
<%option explicit%>
<%
Session.CodePage = 65001
Response.charset ="utf-8"
Session.LCID = 1033 'en-US
.....
response.write (strJSON)
%>
The output from the web is correct. But I was just wondering if some changes is done on the http stream to the C# application.
thanks.
Download the web pages as bytes in the first place. Then, convert the bytes to the correct encoding.
By first converting it using a wrong encoding you are probably losing data. Especially using ASCII.
If the server is really returning UTF-8 text, you can configure your WebClient by setting its Encoding property. This would eliminate any need for subsequent conversions.
using (WebClient wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
json = wc.DownloadString("http://mysite.com/ext/export.asp");
}

Categories