## Resolved CharA() PowerScript function Logic

0
Undo
1. Vipin Dwivedi
2. PowerBuilder
3. Wednesday, 17 April 2024 20:06 PM UTC

Hello Appeon Team,

I need one help!

There is one function in PB PowerScript function CharA() which Extracts the first ASCII character of a string or converts an integer to a char as per the document. So CharA(65) will return Corressponding value 'A'. The same conversion we can do in C# by casting number to Char like (Char)65 which will give you value of Ascii of 65 i.e. A. My problem is when I am using CharA(306) it is giving me value 2 in PowerBuilder but when I use the same in C# (Char)306, it is giving me value as 'IJ'. The Ascii table is also showing me same 'IJ' then how Powerscript CharA(306) is showing me 2? What is the logic behind CharA() and how can I achieve the same in C#?

Thanks,

Vipin

Responses (4)
John Fauss Accepted Answer Pending Moderation
1. Wednesday, 17 April 2024 21:07 PM UTC
2. PowerBuilder
3. # 1

Hi, Vipin -

ASCII codes are single byte values, which is what PB expects... I'm unsure of the rationalization of what C# does, as that makes no sense to me.

Since 306 decimal in binary is larger than 255 (the largest possible integer value for a single byte), the bit pattern gets truncated. In essence, you get Mod(306,256) = 50, and 50 decimal is the ASCII value of the character "2".

Best regards, John

Comment
1. John Fauss
2. Wednesday, 17 April 2024 21:19 PM UTC
Instead of CharA(306), Use Char(306) to obtain the Unicode character.
1. 1
Vipin Dwivedi Accepted Answer Pending Moderation
1. Thursday, 25 April 2024 13:36 PM UTC
2. PowerBuilder
3. # 2

What about AscA() as this is also working differently in PB vs DOTNET. For example AscA('"') [Special Double quote] gives me 147 but (int)('"') in c# gives me 8220. Asc('"') in Powerbuiler is also giving me 8220. What's the logic behind AscA(). Basically I am trying to convert from PowerBuilder code to C# and looking for help.

Comment
1. John Fauss
2. Thursday, 25 April 2024 14:57 PM UTC
Why do you keep using ASCII-related PB functions, then wondering why their output does not agree with Unicode values from c#?

If you want Unicode characters and Unicode code points, use Unicode-related functions!

PB internally has used Unicode 16LE encoding (16 bits, 2 bytes per character) to represent string and character data since version 10, which was released in 2004. As I stated before, I don't know .NET, but it appears from the information you have supplied that it also uses Unicode.

If you want the numeric code point of a single Unicode (2-byte) character in PB, use the Asc() function, since this matches what c# gives you. The CharA() and AscA() functions in PB should ONLY be used if you are working with ASCII-encoded data.
1. 1
1. Vipin Dwivedi
2. Thursday, 25 April 2024 15:04 PM UTC
I am converting existing old PB code to C# and I can't change the existing code as it has some complex logic(algorithm) to encrypt and decrypt our business files.
Vipin Dwivedi Accepted Answer Pending Moderation
1. Thursday, 25 April 2024 13:46 PM UTC
2. PowerBuilder
3. # 3

PB is showing 147 based on below table chart. https://everythingfonts.com/ascii/codes/147

Comment
David Peace (Powersoft) Accepted Answer Pending Moderation
1. Thursday, 25 April 2024 15:15 PM UTC
2. PowerBuilder
3. # 4

I think in C# you need to be using something like this to get the Ascii values:

``System.Text.Encoding.ASCII.GetChars(new byte[]{2});``
Comment