Convert UTF8 apostrophe and quotes to ASCII for VB6 app - c#

I have a VB6 desktop app and iOS and Android smartphone apps. When someone enters a name such as O'Hara in the iOS app it comes through to the VB6 desktop app as unreadable characters. I assume this is a UTF8 to ASCII conversion issue? I would like to intercept this data server side and change the ' and " to the proper character that will read properly in VB6. How should I do this in C# .NET?

You can replace the problematic characters with a version that fits in the character set used by VB 6:
string value = "The problem string";
value = value.Replace("\u2022", "-");
value = value.Replace("\u2013", "-");
value = value.Replace("\u2014", "-");
value = value.Replace("\u2015", "-");
value = value.Replace("\u2017", "_");
value = value.Replace("\u2018", "'");
value = value.Replace("\u2019", "'");
value = value.Replace("\u201a", ",");
value = value.Replace("\u201b", "'");
value = value.Replace("\u201c", "\"");
value = value.Replace("\u201d", "\"");
value = value.Replace("\u201e", "\"");
value = value.Replace("\u2026", "...");
value = value.Replace("\u2032", "'");
value = value.Replace("\u2033", "\"");
This is not an exhaustive list of all Unicode characters, of course. You may end up finding you need to replace more, or alarm on them in some way.

Related

Add double quotes to a list to display in a label

Morning folks,
I have an ASP.Net C# page that pulls in a list of servers from a SQL box and displays the list of servers in a label. ("srv1,srv2,srv3"). I need to add double quotes around each of the servers names. ("srv1","srv2","srv3",)
Any help would be greatly appreached.
If you have string
string str = "srv1,srv2,srv3";
Then you can simply do
str = "\"" + str.Replace(",", "\",\"") + "\"";
Now str contains "srv1","srv2","srv3"
As far as I can understand, you are trying to use double quotes in a string.
If you want to use such,
you can use escape character:
("\"srv1\",\"srv2\",\"srv3\"",)
for the sake of simplicity, you can even convert it to a function:
private string quoteString(string serverName){
return "\"" + serverName + "\"";
}
Also, if you have already "srv1,srv2,srv3" format, find ',' characters in the string and add " before and after comma. Also, notice that you should add to first index and last index ".

incorrect right to left concatenation english and Arabic

I'm trying to concatenate an English string with Arabic string
string followUpFormula = "FIF";
string renewAbbreviation = "ع.ت" ;
string abbreviation = followUpFormula +"-"+ renewAbbreviation;
var result = 10 + "/" + abbreviation + "/" + 2016;
the result is 10/FIF-ع.ت/2016
but i want to display them like this: 10/FIF-ع.ت/
2016
how can I do that?
thanks
Couple of additions to your code
string followUpFormula = "FIF";
string renewAbbreviation = "ع.ت" ;
string abbreviation = followUpFormula +"-"+ renewAbbreviation;
var lefttoright = ((Char)0x200E).ToString();
var result = 10 + "/" + abbreviation + lefttoright + "/" + 2016;
Char 0x200E is a special character that tells the following text to read left to right see here for more information on the character.
Char 0x200F switches to a right to left format.
This has to do with the way that unicode process rules about mixing LTR and RTL text. You can override the default behaviour by explicitly using special characters that indicate an intention to directly embed RTL or LTR text :
private const char LTR_EMBED = '\u202A';
private const char POP_DIRECTIONAL = '\u202C';
private string ForceLTR(string inputStr)
{
return LTR_EMBED + inputStr + POP_DIRECTIONAL;
}
private void Form1_Load(object sender, EventArgs e)
{
string followUpFormula = "FIF";
string renewAbbreviation = "ع.ت";
string abbreviation = ForceLTR(followUpFormula + "-" + renewAbbreviation);
textBox1.Text = 10 + "/" + abbreviation + "/" + 2016;
}
This places an embedded Left-To-Right character (U+202A) before the string and follows it with a Pop-Directional-Formatting (U+202C) character. The latter removes the embedded directional formatting cue and returns the text direction to whatever it was in the previous context. The returned string, therefore, is safe to use in either an RTL or LTR context.
The rules for parsing LTR and RTL text in various contexts are extensive and complex. For reference you can find the bidirectional algorithm specification here. Certain characters are classified as "weak" or "strong" in terms of their affinity for LTR or RTL contexts. Things like / and - are weak so you have to be explicit when mixing them about which text direction and layout you wish these characters to respect.

UTF-8 Encoding converts Control Characters >127 to?

I have binary file with alphabets, numbers and Special Characters and Control characters as follows.
ESC!€STANDARD + UNDERLINE
ESC!COMPRESSED + UNDERLINE
ESC!ˆSTANDARD + EMPHASIZED + UNDERLINE
i am performing the following.
string file = FileOpenDlg.FileName;
System.IO.StreamReader myFile = new System.IO.StreamReader(file);
string data = myFile.ReadToEnd();
byte[] sendCmd = Encoding.UTF8.GetBytes(data);
This replaces €,ˆ etc whose hex value is >80 to ?. When i send this to printer it gives wrong answer.
How handle the these characters? Timely help is appreciated.

Decode Url with special & or + characters in query parameters value

I have met this difficulty while decoding a Base64 encoded URL with parameters
eg: http://www.example.com/Movements.aspx?fno=hello&vol=Bits & Pieces
My expected results should be:
fno = hello
vol = Bits & Pieces
#Encoding:
//JAVASCRIPT
var base64 = $.base64.encode("&fno=hello&vol=Bits & Pieces");
window.location.replace("Movements.aspx?" + base64);
#Decoding c#
string decodedUrl = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(Request.Url.Query.Replace("?", ""))); // Replace is used to remove the ? part from the query string.
string fileno = HttpUtility.ParseQueryString(decodedUrl).Get("fno");
string vol = HttpUtility.ParseQueryString(decodedUrl).Get("vol");
Actual Result:
fno = hello
vol = Bits
I have searched stackoverlow and seems I need to add a custom algorithm to parse the decoded string. But as the actual URL is more complicated than shown in this example I taought better asks experts for an alternative solution!
Tks for reading!
Your querystring needs to be properly encoded. Base64 is not the correct way. Use encodeURIComponent instead. you should encode each value separately (although not needed in most parts in the example):
var qs = "&" + encodeURIComponent("fno") + "=" + encodeURIComponent("hello") + "&" + encodeURIComponent("vol") + "=" + encodeURIComponent("Bits & Pieces");
// Result: "&fno=hello&vol=Bits%20%26%20Pieces"
Then you don't need to Base64 decode in C#.
var qs = HttpUtility.ParseQueryString(Request.Url.Query.Replace("?", ""));
var fileno = qs.Get("fno");
var vol = sq.Get("vol");
If the URL were correctly encoded, you would have :
http://www.example.com/Movements.aspx?fno=hello&vol=Bits+%26+Pieces
%26 is the url encoded caract for &
and spaces will be replaced by +
In JS, use escape to encode correctly your url!
[EDIT]
Use encodeURIComponent instead of escape because like Sani Huttunen says, 'escape' is deprecated. Sorry!

CHAR comm port commands

I am trying to use the following to send over an RS232 connected projector to turn it on:
commProj.Parity = "None";
commProj.StopBits = "One";
commProj.DataBits = "8";
commProj.BaudRate = "19200";
commProj.PortName = "COM6";
commProj.CurrentTransmissionType = PCComm.CommunicationManager.TransmissionType.Text; //.Hex
commProj.OpenPort();
commProj.WriteData((char)33 + (char)137 + (char)1 + (char)80 + (char)87 + (char)49 + "\n"); //turn on proj
Problem being is that it doesnt work.
I have done this with a VB6 port and it works just fine:
public static PCComm.CommunicationManager commProj = new PCComm.CommunicationManager();
MSCommProj.CommPort = 6
MSCommProj.Settings = "19200,N,8,1"
MSCommProj.PortOpen = True
MSCommProj.Output = Chr(33) & Chr(137) & Chr(1) & Chr(80) & Chr(87) & Chr(49) & Chr(10)
What am i missing?
David
CommunicationManager.cs: http://snipt.org/xmklh
Okay, the manual helps a lot. Try changing the CurrentTransmissionType to TransmissionType.Hex and sending the string 21890100000a
commProj.CurrentTransmissionType = TransmissionType.Hex;
commProj.WriteData("21890100000a");
EDIT
Sorry, that was "connection check". Use 2189015057310a for on and 2189015057300a for off.
The plus(+) operator for char's doesn't concatenate the values it adds them. So you end up passing "387\n" to write data.
You need to create a char array and then convert that to a string instead:
commProj.WriteData(new string(new char[] { (char)33, (char)37, (char)1, (char)80, (char)87, (char)49, '\n' }));
I don't know what kind of object commProj is (specifically) but my guess is that the problem comes from casting each numeric value to a char. A char is 2 bytes in size. I recommend either trying to write a Byte array with your data in it, or concatenating a string with these chars and then converting the string to ascii text.

Categories