2

i am having string with some hexadecimal values. Example:

<font color=**"&#x26;amp&#x3B;****amp&#x3B**;**amp&#x3B;&#x23;x23&#x3B;**336699">Hi How ARE YOU</font>

i want to store above string to database. in this string i found some Hexadecimal value . while storing i want convert hexadecimal to string. is there any solution in C#.net.

tgranjith
  • 493
  • 1
  • 4
  • 11
  • [what have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – pankar Sep 14 '12 at 08:54
  • Please post the _actual_ markup. Adding all those `**` and `amp` is not helping with understanding. – Oded Sep 14 '12 at 08:54
  • 1
    What you are calling "hexadecimal values" are HTML character entities. I suggest you don't mess with them as that _could_ change the meaning of the HTML. – Oded Sep 14 '12 at 08:56

1 Answers1

0

To parse a Hex you would do something like this:

var hex = "0xFFFFFF";
uint color;

if(uint.TryParse(hex, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out color))
{
    //Parsing ok
}else{
    color = 0; //Parsing failed
}

I dont know your string above, so i am unsure how to extract the hex from it.


EDIT #1:

As i understand you want to parse this string

<font color=**"&#x26;amp&#x3B;****amp&#x3B**;**amp&#x3B;&#x23;x23&#x3B;**336699">Hi How ARE YOU</font>

and produce thie string

<font color=0x0987AF>Hi How ARE YOU</font>

I dont know how your colorcode is created, so i can not parse it.

It it is embeded into a regular language you can use regular expressions to fit the important peaces together and parse those results. If it is HTML or something simmilar you can not use regex and should instead iterate through it character by character. In that case utilize the StringBuilder to keep your code fast.

once you have all stringpeaces together you can parse them individually, calculate the color and then use String.Format(""{0:X06}"", color) to output it as a hex again.


EDIT #2:

I do not know the encoding of that color string, so i can only give a rough estimate of how i would parse that.

I assume that the last digits in that string are the color and everything else is just noise. So this

<font color=**"&#x26;amp&#x3B;****amp&#x3B**;**amp&#x3B;&#x23;x23&#x3B;**336699">Hi How ARE YOU</font>

should transform into this

<font color="&336699">Hi How ARE YOU</font>

I dont show the first part of isolating the color string, but i want to note that you should not try to use regular expressions on html. Rather go through it character by character like you would with a mathematical expression.

 String examplestring = @"color=**""&#x26;amp&#x3B;****amp&#x3B**;**amp&#x3B;&#x23;x23&#x3B;**336699""";
 Console.WriteLine(examplestring);
 String lastsixnumbers = examplestring.Substring(examplestring.Length - 7, 6);
 Console.WriteLine(lastsixnumbers);
 String final = String.Format("color=\"#{0}\"", lastsixnumbers);
 Console.WriteLine(final);
 Console.ReadKey();

Is this helpful? If not then my understanding of the colorcode could be wrong - could you then provide more actually matching results using the color codes or even better provide me with the knowledge how the colors are encoded.

Community
  • 1
  • 1
Johannes
  • 6,490
  • 10
  • 59
  • 108
  • 1
    The OP is asking to convert the character entities to their characters. Not to convert a HEX color string to a `Color`. – Oded Sep 14 '12 at 09:06
  • i am using ajax HtmlExtendeditor. through the the editor if i am formatting string or image means it generating hexadecimal string. – tgranjith Sep 14 '12 at 09:12
  • while saving the string in to the database. the string is stored with some hexadecimal value so that i want to convert entire string as a normal string. – tgranjith Sep 14 '12 at 09:15
  • i want store the string without having any hexadecimal value – tgranjith Sep 14 '12 at 09:45
  • Could you add an example string how it would look like without the hexadecimals? Do you just want to delete all portions that look like hex... for examplete '"&amp;****amp;' to '"amp****amp'? – Johannes Sep 14 '12 at 11:11
  • example Avoid excess sugar in TEA and COFFEE – tgranjith Sep 14 '12 at 12:15