Friday, June 21, 2019

C++: convert an integer to a hex string


#include <iostream>
#include <sstream>
#include <iomanip>

long i = 127;

std::ostringstream oss;


oss << "0x" << std::hex << std::uppercase << std::setfill('0') << std::setw(8) << i;


std::string result = oss.str();


Monday, June 17, 2019

Perl: Count the appearance of a specific character in a string


For example, to count the appearance of character ':' in a string --

my $str = "a:b:c:d:e:f:g";
my $count = ($str =~ tr/://);
print $count, "\n";

The output will be 6.

Thursday, June 13, 2019

ASP.NET: Xml control loading a XML string in a safe way


In the .aspx file:

<asp:Xml id="xml1" runat="server" />


In the .aspx.cs file:

XmlSchema schema = new XmlSchema();
XmlSchemaElement elementRoot = new XmlSchemaElement();
schema.Items.Add(elementRoot);
elementRoot.Name = "root";

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(schema);
settings.ValidationType = ValidationType.Schema;
settings.DtdProcessing = DtdProcessing.Prohibit;   // to prevent XXE attack.
StringReader sr = new StringReader(xmlInString);
XmlReader reader = XmlReader.Create(sr, settings);

xml1.Document.XmlResolver = null;   // to prevent XXE attack.
xml1.Document.Load(reader);

Note: the schema generated has only the root element (as below). If xmlInString contains any type of children elements, it will be validated as good.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root"/>
</xs:schema>


Since Xml.Document is an obsolete property, for the above example, we should use another property DocumentContent. If Schema validation is not needed, we have a much simpler code:

xml1.DocumentContent = xmlInString;


Wednesday, June 12, 2019

ASP.NET: Cross site scripting attack and HtmlEncode


To prevent the Cross Site Scripting (XSS) attack, we should use System.Web.HttpUtility.HtmlEncode() to encode a string before sending it in a response if the string is from an untrusted source.

System.Web.HttpUtility.HtmlEncode will encode these characters:

   Character       Encoded
    <        &lt;
    >        &gt;
    "        &quot;
    &        &amp;
    '        &#39; (.Net 4.0 Only)

Saturday, June 8, 2019

Google Docs - spreadsheet - insert chart on another sheet (updated)


To insert a chart for your spreadsheet data, you can highlight the wanted cells and select menu Insert|Chart. The new chart will be put somewhere on the same sheet of the data.

If you click on the chart, a vertical ellipsis icon will be shown at the top right corner.
 

 Click on the ellipsis icon and choose the "Move to own sheet..." option:

 

By selecting its menu Move to own sheet..., you can move the chart to another sheet. The chart will have its own sheet and it will fill the whole new sheet.

This may not be what you want. Sometimes, you want a chart sheet which contains several charts representing data from other sheets. To do that, you can add a new sheet first by clicking the Add Sheet button at the bottom-left corner.

Select the new sheet. Insert a new chart by menu Insert|Chart. Double click on the new blank chart to show the Chart editor as the right sidebar. In the Chart editor sidebar, you need to manually input what data you want to use. If the data are from Sheet1, you need to add Sheet1! right before the cells range.



Now you have a chart for the data from Sheet1. You can add more charts for the data from different sheets in this way, so that you can have a single "chart sheet" for the whole document.
 
Get This <