Char and graphical visibility - c#

running this code I see that not all values of char (from 0 to 65534) correspond to a symbol. It is rigth? Or I've to do something differernt to show symbols different from a 'square'?
Thank you all!
private void Form1_Load(object sender, EventArgs e)
{
char x = Char.MinValue;
do
{
listBox1.Items.Add(x + " - " + (int)x);
x++;
} while ((int)x < (int)Char.MaxValue);
}

That's normal. There are at least three possible explanations for a character being displayed as a square:
Some characters may not be supported by the font the control uses.
Not all Unicode codepoints are assigned.
Reserved Code Point. Any code point of the Unicode Standard that is reserved for future assignment. Also known as an unassigned code point. (See definition D15 in Section 3.4, Characters and Encoding, and Section 2.4, Code Points and Characters.)
One of the characters is actually supposed to be a square (□) when displayed correctly.

It's worth checking what fonts you have available to your application, and cross-referencing what you wan to be displayed (or expect) with the Unicode code charts.

Related

GS1 barcode parsing - It seems that there is no separating character

I have a program for parsing GS1 Barcodes (with Zebra scanner), which worked just fine, atleast I thought it was OK...
Until I came across one box with 2 GS1 barcodes.. one "linear" and one data matrix (UDI). For linear, it worked just fine, I successfully got out the GTIN and Serial. But datamatrix is different. For some reason, its content is a bit longer than linear code, it has some production date and something else at the end.
This is the Linear code: (01)00380652555852(17)260221(21)25146965079(30)1
This is data matrix: (01)00380652555852(17)260221(21)2514696507911210222240SA60AT225
I have problems with parsing out the Serial number - 25146965079.
Serial number in GS1 has a length of 1-20 characters. This one has 11, but How can I make it stop after the 9 characters? How can I know that the serial ends there?
I tried transforming each character to UDI, but it seems that there is no special separating character or anything.. so I honestly donjt know what to do. Does anyone have any idea?
This the code, if anyone wanna try anything https://prnt.sc/1x2sw8l
Those codes/products came right from the manufacturer, so there shouldnt be anything wrong with the code, I guess...
If you verify the barcode with a scanner that is designed to interpret a GS1 structure, you will see that the generated barcode is in fact incorrect.
You are missing a GS after the serial number, these codes MUST end a variable-length field if it's not the last one. This is specified in GS1 general specifications section 7.8.5.2
Without this separator you can't know where the serial ends - or, a machine interpreting the code can't know.
Tell the manufacturer that they need to study the GS1 specs.
Edit: the "correct" version would be:
(01)00380652555852(17)260221(21)25146965079<GS>(11)210222(240)SA60AT225
The parentheses and group separator <GS> are not included literally in the code.
Since you have two variable-length identifiers (21) and (240) you need a GS no matter what you do. Only alternative would be to have some padding for serial number, then you could do without separator.
According to the GS1 documentation (page 156 and forwards)
All the fields are correct
(01)00380652555852 --> GTIN
(17)260221 --> Expiration date
(21)25146965079 --> Serial Number
(11)210222 --> Production Date
(240)SA60AT225 --> Additional Product Identification
I tried scanning the image but the result was the same as yours.
So the problem is that the separators are not there. Which is a problem for you, and there is no way to know where the serial number ends without the separator.
I am sorry my English is not good
The reason of this problem is group separetors are unreadable character for example if you focus on text box and press capslock button or shift button nothing appear in text box the same in gs
To solve this problem
Public l as integer
And put the following code in keyup event
If textbox1.textlenght = l then
My.combuter.keybord.sendkeys({enter})
L= textbox1.textlenght
End if
This code will give space after each litter (because each litter combined with cabslock button) and five spaces in groub space
store raw input in KeyPress event and then read the character for Letter Or Digit.
if (e.KeyChar != 13)
{
int asci = Convert.ToInt32(e.KeyChar);
if (asci > 31 && asci < 128) // numeric and chars only
rawbcode += Convert.ToChar((int)(e.KeyChar & 0xffff));
else
{
if (asci == 29)
{
rawbcode += "<GS>"; // GS1 Seperator
}
}
}

Making Raylib-cs work with extended ascii / unicode above 128 in windows?

I'm teaching C# and game dev to high school students, and we're using Raylib-cs as a simple introduction to graphics APIs and libraries.
We've hit a small snag: We're swedish, and we'd like to use some special non-ascii letters – namely å, ä and ö (lots of swedish words use them). However, I can't get Raylib-cs to display anything above codepoint 127 – at least not using DrawText.
Instead, all I get is ?.
This is on Windows 10, 64-bit, 20H2. Using dotnet 5 (latest) and primarily the Raylib-cs available as a nuget package.
What I've tried so far:
DrawText and DrawTextEx. Same result.
Loading different fonts, with or without explicit inclusion of codepoints up to 255. Same result.
Getting the latest Raylib-cs from the github page. Same result.
Running the same code but in a virtual Debian machine. THIS WORKS, so issue seems to be in Windows.
Asking a friend who's proficient in C/C++ to try using åäö using Raylib in C++. THIS WORKS, so the issue seems to be specific to Raylib-cs, even though it's just a wrapper?
DrawTextCodepoint. THIS WORKS, which means that for some reason the issue is specific to the DrawText methods (and InitWindow). Raylib is supposed to be Unicode-capable, and this proves that at least in theory, it is.
Here's my simple test code (just writes out characters 0-255):
static void Main(string[] args)
{
int font_size = 10;
Raylib.InitWindow(800, font_size * 64, "åäö");
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.BEIGE);
for (int i = 0; i < 255; i++)
{
int col = i / 64;
int x = col * 200;
int y = (i % 64) * font_size;
string text = i.ToString() + " | " + ((char)i).ToString();
Raylib.DrawText(text, x, y, font_size, Color.BLACK);
}
Raylib.EndDrawing();
}
}
Result in windows (at least for me): Window title bar is "???", as are all characters beyond the second column.
Result in debian: Window title bar is "åäö", and all characters are drawn as they should.
Has anyone come across this problem? Anyone got (tested) solutions?
Is there some known quirk in how C# specifically on windows handles strings or something?
The way the library imports DrawText is faulty; its default behavior "marshals" string values to fit a legacy character encoding, not necessarily UTF-8: https://github.com/ChrisDill/Raylib-cs/blob/b1f46d33071387800559523950aa131448251461/Raylib-cs/Raylib.cs#L2116 (Specifying CharSet=Unicode probably won't help because the DrawText function doesn't use wide-character strings, but rather ordinary char* pointers interpreted as UTF-8.)
I see you have posted an issue to the GitHub repository. And fortunately, the library's author suggested two workarounds. As they said, one of them is: "Using [MarshalAs(UnmanagedType.LPUTF8Str)] to marshal strings as UTF8."

Word-wrap line of string overflow in C# and also give out the number of lines that have been wordwrapped

from these two questions in SO | 1 | and | 2 |(this one's my
own) I've tried to solve but I'm running into some problems.
Take a look at where I am stuck!!
I have successfully word-wrapped this string "Damodarmarg, Kusunti, Inside Ringroad, Lalitpur, Bagmati, Nepal" using a code (see my SECOND CODE)
But I DO NOT KNOW how to get the number of lines that have been wrapped. (because it wraps automatically).
This is the screenshot of my PRINT PREVIEW:
I want to put some space between Residential Address and Permanent Address. To do that I need to know how mny lines are being word-wrapped.
Thats my problem. The word-wrap is happening but NOT the line spacing! I just want to know how I can calculate the number of lines that have been word-wrapped so that I can run a function to do appropriate line spacing between two fields!
I want to know the number of lines that have been word-wrapped (which you can observe, in the screenshot above, is clearly = 2 here).
Why do I need this number? ==> If x is the number of lines word wrapped, I want to execute the function newline() x number of times. That's why. Its my own function whose purpose is to correct line spacing among different fields.
Example:
For a string "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
IF the page margin only allows 10 characters per line then
The output should beABCDEFGHIJKLMNOPQRSTUVWXYZ
and the number of lines used, stored by variable linesFilled(say), should be = 3
Obviously,this is just a run-down example. In real-practice, I would like to have no character limit per line. Instead I want MeasureString to automatically know how many words fit in a line and then word-wrap the rest that are not fitting to next consecutive line(s). For your information: I have already done this much . You see, I seek your help only to know how to get the number of lines that have word-wrapped. That has been really tricky to work around.
What I have tried so far:
FIRST CODE:
My code looks like this(for the line counting; which you need to help me with):
int charactersFitted;
int linesFilled;
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString("Residential Address: " +
RAddressTextBox.Text, stringFont, layoutSize, newStringFormat,
out charactersFitted, out linesFilled);
textBox1.Text = Convert.ToString(stringSize);
textBox2.Text = Convert.ToString(stringSize.Width);
So, this first code is supposed to give me the number of lines that were wrapped around the print margin. Currently just gives the width of the part of string that occupied the whole line as opposed to the number of lines the string has occupied (can I get a method to know the number of lines?)
SECOND CODE:
Graphics RAddress = e.Graphics;
SizeF RAddressLength = RAddress.MeasureString("Residential Address: "+
RAddressTextBox.Text, stringFont,700);
RAddress.DrawString("Residential Address: " + RAddressTextBox.Text,
stringFont, Brushes.Black, new RectangleF(new Point(pagemarginX,newline()),
RAddressLength), StringFormat.GenericTypographic);
and this second code helps we actually wrap the string when it does not fit in page margin(this second code here works perfectly at the moment. It automatically word-wraps text NOT fitting in a line to next consecutive line(s) but it DOES NOT tell me how many lines have been word-wrapped. THATS my problem)
Note: newline() is my own function which leaves one line when called. And pagemarginX sets approapirate margin. Thats all. Do not be confused. As for why I havent used DrawString in my FIRST CODE; I have been using both codes. This one to display the string and the FIRST CODE to count the lines in string. I haven't been able to count the number of lines with this one. Sorry for the confusion.
SAMPLE OUTPUT(s) FOR YOUR INFORMATION: Currently, output of stringSize.Width is 114.226.As suggested in some of the comments, I tried outputting linesFilled instead of stringSize.Width and the output was 5 . Another suggestion was to try int numLines = Convert.ToInt32(Math.Ceiling(layoutSize.Width / stringSize.Width)); which gave me the output 7 . As shown in the screenshot of my PRINT PREVIEW over there^^^, I obviously need the output=2 for my string. Please somebody help me!
I welcome:
Solutions that augment and enhance my FIRST CODE to produce correct lines of word-wrap as output for the string: "Damodarmarg, Kusunti, Inside Ringroad, Lalitpur, Bagmati, Nepal" (which should be 2)
Solutions that can modify my SECOND CODE to also produce the number of lines that have been wordwrapped (This type of solution would be awesome!!)
Creative solution: A solution that takes input as string and wordwraps that string to a margin and also produces the number of lines word-wrapped. (OR, whatever you think that solves this problem also works!)
My specifications are NOT rigid. You can solve this problem any way you are comfortable with!
I have edited my question to include as much detail as possible. If you'd like the whole module, you can simply ask me too. I am hoping for a solution. Thanks!
Not sure if this is what you're looking for but when using a print dialog you can do something like this that will give you the number of characters on the page and how many lines per page the string takes up:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int charactersOnPage = 0;
int linesPerPage = 0;
// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);
}
e.MarginBounds.Size is what will do the trick for you I believe. Then you can just takes the "charactersOnPage" value and divide it by "linesPerPage" to get the number of characters that fit on one line:
var charactersPerLine = charactersOnPage / linesPerPage;
Once you have "charactersPerLine" you can accomplish the rest of what you're trying to do.
I think you meant
textBox2.Text = Convert.ToString(linesFilled);
instead of
textBox2.Text = Convert.ToString(stringSize.Width);
Edit: Try this:
int numLines = Convert.ToInt32(Math.Ceiling(layoutSize.Width / stringSize.Width));

Windows Phone textbox limits to only accepts 8 characters

I'm having a problem with a Windows Phone 7 application that I'm coding (Using Visual Studio 2010). My code looks like this:
private void Key1Input_TextChanged(object sender, EventArgs e)
{
string hexOnly= Key1Input.Text;
int n = 0;
if (!int.TryParse(hexOnly, System.Globalization.NumberStyles.HexNumber, System.Globalization.NumberFormatInfo.CurrentInfo, out n) &&
hexOnly!= String.Empty)
{
Key1Input.Text = hexOnly.Remove(hexOnly.Length - 1, 1);
Key1Input.SelectionStart = Key1Input.Text.Length;
}
}
which is a same code that can also be found from this web site: http://social.msdn.microsoft.com/Forums/windows/en-US/ec7b777d-deb1-45e1-b66e-e25daddf6497/text-box-which-accepts-only-hexadecimal-values
The code works fine, but when I tried to insert 9th character to the textbox, it didn't show up. My textbox's Maxlength value is 12. After inserting some breakpoints to the code and debugging the app, I noticed it goes inside the if statement when there's 8 hex numbers and trying to type more. Am I doing something wrong, or are there some kinds of limitations with WP7.
I also tried the other 2 codes from the same site. The second one works, while the third one does not.
The smallest 8-digit hex number is F0000000, which is greater than Int32.MaxValue. Try long instead of int.

HtmlElementEventArgs KeyPressedCode Confusion

I'm using the following code to decide if a '.' (full stop) has been entered into a webbrowser control:
private void body_KeyUp(object sender, HtmlElementEventArgs e)
{
if (e.KeyPressedCode == '.')
{
// Do something
}
}
According to msdn KeyPressedCode returns an ASCII value. What I get by breakpointing is '190' if I enter a '.' however. This is not even listed in the standard ASCII table.
Obviously I could simply test for 190 but I fear that KeyPressedCode might return different values on different systems with different code pages, languages and so on.
So can you please explain me why KeyPressedCode returns '190' instead of '46' and how I can manage this problem 'safely'?
Interestingly enough the return value for ' ' (space) is always correct ('32').
Playing with System.Text.Encoding.GetEncoding and different code pages didn't solve the problem, I don't have much experience with code pages however.
You were likely using a wired keyboard, because keycode 190 is an OEM number keycode of .. If you were using a laptop it would behave as you expected.
Just a wild guess, but have you checked the values of e.AltKeyPressed , e.CtrlKeyPressed and e.ShiftKeyPressed ? Hope you see what I'm getting at...

Categories

Resources