Re: String.ASC and (int)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 01/25/05


Date: Tue, 25 Jan 2005 09:33:52 -0500


    When converting a character to an int, you shouldn't expect any change
to take place. An int can hold any value that a character can represent, so
the int will just be the same value as the character. When you see 337,
338, that is correct.

    When you run the values of 337 and 338 through ASC in VB6, you get 51,
not the values you are getting, so the logic is definitely different from
.NET, which produces the results you are getting (I get the same when
running in .NET).

    Basically, if you want to convert to a character and get those results,
you will have to reference Microsoft.VisualBasic.dll, and use the ASC
function. It has certain logic for endianness which the convert routines (I
used ASCIIEncoding, the Convert function) and a cast just don't do.

    I don't believe this is an error though, as the cast and convert
functions (as well as the Encoder) works as advertised.

    Hope this helps.

-- 
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com
<skeamy@hotmail.com> wrote in message 
news:7a132177.0501250616.3be57f9e@posting.google.com...
> Hi everybody,
> Im currently converting an application from VB6 to C# which contains
> logic to convert EBCDIC to ASCII and vice versa. I have a test app
> which reads some previously stored EBCDIC from a text file and
> converts it. I have finally got it all working (Encoding.DEFAULT was
> the turning point when reading the file)
>
> I am now trying to remove all references to the Microsoft.VisualBasic
> and Compatibility namespaces and use the C# System namespace
> equivilents (Purely for my own experience I am not starting a debate
> about this). I have everything changed except a packed date routine
> which uses Strings.ASC and uses the last char of the packed field to
> indicate whether its a positive or negative number - I have isolated
> the problem to a few lines of code and wonder if someone can explain
> what is happening
>
> char cTest = (char)(174);
> MessageBox.Show(((int) cTest ).ToString()); // Returns 174 Works OK
> MessageBox.Show(Strings.Asc(cTest).ToString()); // Returns 174 Works
> OK
> char cTest1 = (char)(338);
> MessageBox.Show(((int) cTest1 ).ToString()); // Returns 338 Doesn't
> work
> MessageBox.Show(Strings.Asc(cTest1).ToString()); // Returns 140 Works
> OK
> char cTest2 = (char)(337);
> MessageBox.Show(((int) cTest2 ).ToString()); // Returns 337 Doesn't
> work
> MessageBox.Show(Strings.Asc(cTest2).ToString());// Returns 111 Works
> OK
>
> I was led to believe that the VB6 function ASC() returned the
> equivilent of (int) char in C# but Im missing something. Does anyone
> know how String.ASC is achieving its (working) results?
>
> Thanks
> Please reply in this thread not to email 


Relevant Pages

  • Re: Insufficient guarantees for null pointers?
    ... will the compiler know what the bounds are after converting that char * ... to an int *, if it could point to either of two arrays which happen to ... compares equal to the original pointer. ...
    (comp.std.c)
  • Re: Write to file
    ... You can read the file one character at a time. ... compared to the current char. ... Once the (lgh - 1)th char ... void initnext(int *next, const char *id, int lgh) ...
    (comp.lang.c)
  • Re: Common misconceptions about C (C95)
    ... int main ... void f ... printf("\nThe character is lower case.\n"); ... plain char to them, but they take unsigned char values. ...
    (comp.lang.c)
  • Re: Write to file
    ... int main(int argc, char *argv) ... I intended you you use c here not read another character! ... You reuse the buffer. ...
    (comp.lang.c)
  • In a timeline pinch (Suspense: 25Jul05) pleading for assistance - Q1
    ... It will print out the computer's internal code for the character '7' ... It is never necessary to use an int for character processing. ... making an assignment to a pointer variable. ...
    (comp.lang.c)