0

I'm migrating a project from VB6 to C#.

In one part of the code it gets the int value in ANSI(I think!) and gets the char value to an string:

StringBuilder s_e = new StringBuilder();
s_e.Append(Microsoft.VisualBasic.Strings.Chr(inNCr).ToString());

Tried with

s_e.Append((char)inNCr);

But works only from a-z but not for another characters, so the result is not always correct.

I know I can use using Microsoft.VisualBasic; and be done with it, but is there any way to do it "natively" in c#?

Thanks in advance.

[edit] Clarification needed.

inNCr is the character code of the character as an int. then that int should be a char and show up as an string on the app. For example: Character code 130 should be '‚' but on the app is showing as blank and on debug as \u0082

vvic
  • 317
  • 1
  • 9
  • 1
    You may be looking for `Convert.ToChar`. MSDN: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tochar?view=netframework-4.8#System_Convert_ToChar_System_Int32_ – Collen Dec 24 '19 at 21:28
  • Already tried but unfortunately it's missing some chars too. Thanks – vvic Dec 24 '19 at 21:53
  • 2
    Any example of characters that are not converting properly ? – Jawad Dec 24 '19 at 21:54
  • 1
    Also, please note that not all characters can be printed .. unless you have the proper fonts installed: https://stackoverflow.com/questions/5750203/how-to-write-unicode-characters-to-the-console – Jawad Dec 24 '19 at 21:59
  • 1
    Neither VB6 nor .NET languages restrict themselves to "ANSI" characters. VB6 used Windows NT's Unicode capability. In .NET, character handling is even more flexible. Your code doesn't show what type `inNCr` is, or how it gets initialized. Can you show enough so that we can see what you are doing? – Flydog57 Dec 24 '19 at 22:13
  • See the edit @Flydog57. Thanks – vvic Dec 24 '19 at 22:19
  • Are you saying the character with integer value 130 should be a comma? That seems wrong to me. – itsme86 Dec 24 '19 at 23:33
  • isn't the character value of 130 and \u0082 a comma? http://ascii-table.com/ansi-codes.php – vvic Dec 25 '19 at 00:32
  • True ASCII characters (the primary "latin" characters) max out at 127 (0x7f). A comma is 44 (0x2c or \u002c) – Flydog57 Dec 25 '19 at 02:22
  • Sorry, but I don't get what you mean. Microsoft.VisualBasic.Strings.Chr as far as I understand, returns ANSI characters of a given int, and it does well. The problem I think is any char cast or char convert in c# is doing it in another econding and its not treating some symbols the same way, hence i'm not getting the result. For example Strings.Chr(140) is returning 'Œ' and (char)140) is returning '\u008c' . how would I transform that unicode to the symbol itself? – vvic Dec 25 '19 at 10:48
  • 1
    `ANSI` encoding doesn't exist (the Institution does). Above ASCII 127, you have Local Encoding or a default encoding (=> `CodePage 1252`, aka `iso-8859-1` (not the same encoding, though, in practice), aka `Windows-1252`, aka `Western European (Windows) => System.Text.SBCSCodePageEncoding`, aka `Encoding.Default`). So, you can write: `var bytes = new byte[] { 130, 140 }; var uniChars = Encoding.GetEncoding(1252).GetChars(bytes); [StringBuilder].Append(uniChars);` or using the Encoding.Default predefined set: `var uniChars = Encoding.Default.GetChars(bytes); [StringBuilder].Append(uniChars);` – Jimi Dec 27 '19 at 07:25

1 Answers1

1

Use Convert.ToChar. See here for more details: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tochar?view=netframework-4.8#System_Convert_ToChar_System_Int32_

Simcha
  • 99
  • 1
  • 12
  • Already tried, but this is what I'm getting: 140 '\u008c' , 33 '!' , 133 '\u0085'. Can I somehow print 140 and 133? If I do a simple Console.WriteLine this is what I get: '?!?' – vvic Dec 26 '19 at 12:36