Hope to seek some help.
I am trying to add some text to Exif field ImageDescription(270). I am partially successful.
Here is the code
pitem.Id = 270;
pitem.Type = 2;
byte[] utf16Bytes = Encoding.Unicode.GetBytes("Testing ImageDescription from command line.");
byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16Bytes);
pitem.Value = utf8Bytes;//utf16Bytes
image.SetPropertyItem(pitem);
I then save image copy at new path, then I try to read back the property just added. THIS is where I am so failing..:-(
For some reason this field is not taking more then 6 characters, I have tried many things including change data to UTF-8 byte array, adding null-terminator (\0), even tried another field (305) with the same issue there as well, field will not take more then 6 characters.. I am not being able to see the full text being added to the field. Can some one guide..
Thanks
2 things:
(a) Look at PropertyItem.Type definition:
1: Specifies that Value is an array of bytes.
2: Specifies that Value is a null-terminated ASCII string. If you set the type data
member to ASCII type, you should set the Len property to the length of
the string including the null terminator. For example, the string
"Hello" would have a length of 6.
Looks like you are using it wrong - either it should be (1) - and then you should save array of bytes, or it should be (2) - but then you need to pass ASCII string to it.
Look at this question as well: Set image meta data before save
(b) Set ptype.Len property to the length of the string.
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.type.aspx
Related
We have been struggling with calculating the length of a string which contains Unicode characters e.g (Word characters such as ’) that get pasted into our systems's text-area's then get saved. When we need to return that same saved string and we need to calculate our Response content-length the normal c# string.Length does not calculate the length correctly since Unicode has more than one character.
We have tried using the System.Globalization.StringInfo class to read the amount of chars but to no avail as it still comes up short on the correct length of the request. Say for instance the Json response object's last closing curly bracket gets cut off by the browser since the length is too short.
If someone can perhaps shed any light on something that they have used that works it would be greatly appreciated. What we tried so far:
Response.AddHeader("content-length", content.GetType() == typeof(string) ?
new System.Globalization.StringInfo(content.ToString()).LengthInTextElements.
ToString() : ((byte[])content).Length.ToString());
Given an arbitrary block of bytes, which you know represents some text, and which is encoded using UTF8, the only way to know the actual character count is to decode the text. E.g. by passing it to Encoding.UTF8.GetString(). Then you just look at the length of the string returned.
That said, the Content-Length field of an HTTP response is supposed to indicate the length of the response in bytes. If you must set the length yourself, you should just use the total byte count. But if you are using e.g. HttpResponse I would expect this field to be set automatically on your behalf.
I have large amount of data which consists of tables,font,bold,size,etc. Those data will be stored as byte[] in Database.
when i retrieve those data i need to convert byte[] into string,because i need to some find & replace from this string,after i convert this string into byte[],am losing the original data structure which means, i can't able to see any tables,font,bold etc. properly. So how can i find and replace in byte[] by converting string and also to keep remain the data in original format.
The short answer is don't. Figure out the format of the data and see what you can do to do the manipulation. If the data is actually text, just stored as byte[], your approach would work, provided you encode the string correctly (ie. if your DB expects UTF-8, use UTF-8 encoding, if it's windows-1251, use that).
If you have a structure where a part of it is a string, what you're doing can't really work well. First, you probably want to modify just the relevant parts of the field. On MS SQL, you have handy functions for that. But even then, you should know what's actually stored there, not just assume that a string replace will magically work.
Now, a hack could be to use an explicit encoding that doesn't break the non-string data. That would be some single-byte encoding that doesn't do anything fancy. This is OK as long as you use the same encoding while reading the text data - however, if you use any variant of unicode, you're out of luck; due to features like string normalization, you can't really guarantee that what comes in comes out the same way, per-byte. It's generally a bad practice anyway.
Don't forget that it's quite possible the string you are looking for is actually somewhere outside of the text fields - even by pure chance, it can happen, and certain practices make that even more likely.
Again: figure out the data format inside that data field - then you can decide how to do what you want.
Try this
string result = System.Text.Encoding.UTF8.GetString(byteArray)
To make Byte[] to String
byte[] byteArray = new byte[10]; // put your byte array here
public void byteToString()
{
stringTemp = "";
stringTemp = BitConverter.ToString(byteArray).Replace("-", "");
}
And your data still in byteArray.. :)
If the byte Array contains binary data and is no string, try to convert it to base64:
Convert.ToBase64String(yourByteArray);
I have a text that is a property of an object.The object gets XmlSerialized and after that there is an element in the XML call Text that represents the text from the object.I am wondering how to turn it back to string.
THE TYPE OF SERIALIZATION: XmlSerializer serizlizer = new XmlSerializer(typeof(Act));
THE PROPERTY IN THE CLASS :
[System.Runtime.Serialization.OptionalFieldAttribute()]
private byte[] ActTextField;
In the xml file it looks something like that:
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAALQAAAAAAAAAAEAAALwAAAAEAAAD+////AAAAACwAAAD////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////spcEAJ2AJBAAA8BK/AAAAAAAAEAAAAAAABgAAYB4AAA4AYmpiavbg9uAAAAAAAAAAAAAAAAAAAAAAAAACBBYALiIAAJSKAQCUigEAzwYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//w8AAAAAAAAAAAD//w8AAAAAAAAAAAD//w8AAAAAAAAAAAAAAAAAAAAAAKQAAAAAANADAAAAAAAA0AMAANADAAAAAAAA0AMAAAAAAADQAwAAAAAAANADAAAAAAAA0AMAABQAAAAAAAAAAAAAAOQDAAAAAAAArAgAAAAAAACsCAAAAAAAAKwIAAAAAAAArAgAABQAAADACAAAFAAAAOQDAAAAAAAA/Q4AALYAAADgCAAAAAAAAOAIAAAAAAAA4AgAAAAAAADgCAAAAAAAAOAIAAAAAAAA4AgAAAAAAADgCAAAAAAAAOAIAAAAAAAAWA4AAAIAAABaDgAAAAAAAFoOAAAAAAAAWg4AAAAAAABaDgAAAAAAAFoOAAAAAAAAWg4AACQAAACzDwAAaAIAABsSAACSAAAAfg4AADkAAAAAAAAAAAAAAAAAAAAAAAAA0AMAAAAAAADgCAAAAAAAAAAAAAAAAAAAAAAAAAAAAADgCAAAAAAAAOAIAAAAAAAA4AgAAAAAAADgCAAAAAAAAH4OAAAAAAAAAAAAAAAAAADQAwAAAAAAANADAAAAAAAA4AgAAAAAAAAAAAAAAAAAAOAIAAAAAAAAtw4AABYAAAAkDgAAAAAAACQOAAAAAAAAJA4AAAAAAADgCAAAagMAANADAAAAAAAA4AgAAAAAAADQAwAAAAAAAOAIAAAAAAAAWA4AAAAAAAA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RCB
I can not even suppose what is its encoding and how to decode it.I tried to read it into byte array but it didn't actualy work after applying few decodings Encode.UTF8 , Encode.ASCII,
That looks like Base64 to me - just use
byte[] data = Convert.FromBase64String(base64Text);
It's odd that it's using base64 at all if this is really a text property though. I'd expect just the text.
To convert that binary data back to text you would need to know which encoding was used to convert it to the binary data to start with - and UTF-8 is the most likely - but all the repeated AAAAA... parts in there make this look pretty unlike text, to be honest.
EDIT: Now that we've seen the field declaration, we can see that it was a byte[] to start with, so that makes sense for it to be encoded in this way. Judging by comments, it sounds like it's actually a Word file - at which point extracting the text is a very separate problem.
I need help figuring out how to convert data that comes in from a SQL Server table column that is set as varBinary(max) into a string in order to display it in a label.
This is in C# and I'm using a DataReader.
I can pull the data in using:
var BinaryString = reader[1];
i know that this column holds text that was previously convert to binary.
It really depends on which encoding was used when you originally converted from string to binary:
byte[] binaryString = (byte[])reader[1];
// if the original encoding was ASCII
string x = Encoding.ASCII.GetString(binaryString);
// if the original encoding was UTF-8
string y = Encoding.UTF8.GetString(binaryString);
// if the original encoding was UTF-16
string z = Encoding.Unicode.GetString(binaryString);
// etc
The binary data must be encoded text - and you need to know which encoding was used in order to accurately convert it back to text. So for example, you might use:
byte[] binaryData = reader[1];
string text = Encoding.UTF8.GetString(binaryData);
or
byte[] binaryData = reader[1];
string text = Encoding.Unicode.GetString(binaryData);
or various other options... but you need to know the right encoding. Otherwise it's like trying to load a JPEG file into an image viewer which only reads PNG... but worse, because if you get the wrong encoding it may appear to work for some strings.
The next thing to work out is why it's being stored as binary in the first place... if it's meant to be text, why isn't it being stored that way.
You need to know what encoding was used to create the binary. Then you can use
System.Text.Encoding.UTF8.GetString(reader[1]);
And change UTF8 for whatever encoding was used.
I'm in the process of creating an application which will monitor specific registry key values for changes and write those changes to a text file.
At present I can monitor the changes and know when specific values have changed and collect the data held in those values. The problem I'm having at the moment is the return type of the data is Byte and I wish to convert this to String initially for display so I know it returns the right value and then can be saved to a text file.
The reason I'm asking is that later on the next time the user logs onto a system those keys will be created or changed to match the previous values. (Were doing this as a way to save user preferences as were currently using mandatory profiles).
If anyone has any advice it would be appreciated.
It depends what the bytes are.
You need to figure out what encoding the bytes were generated from, then write something like this:
string str = Encoding.UTF8.GetString(bytes);
Depending on how the bytes were made, you may need to use Encoding.ASCII or Encoding.GetEncoding.
You first need to decide what encoding the bytes are in before converting to a string..
Then :
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string myString = enc.GetString(myByteArray );
The System.Text.Encoding class has static methods to convert bytes into strings. In your case, you will want to use System.Text.Encoding.ASCII.GetString(bytes). Use other encodings (UTF8, UTF16) as appropriate.
Instead of using a specidief encoding, use the default one of your system :
public string ReadBytes(byte[] rawData)
{
//the encoding will prolly be the default of your system
return Encoding.Default.GetString(rawData);
}
Right I've now been able to resolve this by storing the registry value in a byte array, then adding each part of the array to a string, using the following code:
RegistryKey key = Registry.Users.OpenSubKey(e.RegistryValueChangeData.KeyPath);
Byte[] byteValue = (Byte[])key.GetValue(e.RegistryValueChangeData.ValueName);
string stringValue = "";
for (int i = 0; i < byteValue.Length; i++)
{
stringValue += string.Format("{0:X2} ", byteValue[i]);
}
Thanks for all the suggestions