473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

String.ASC and (int)

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
Nov 16 '05 #1
2 10789
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]
- mv*@spam.guard.caspershouse.com

<sk****@hotmail.com> wrote in message
news:7a**************************@posting.google.c om...
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

Nov 16 '05 #2
Skeamy,
(char) & (int) in C# deal with Unicode characters which are 16-bit.

Strings.Asc & Strings.Chr deal with Ansi characters (Encoding.Default) which
are 8-bit.

Strings.AscW & Strings.ChrW deal with Unicode characters.

To convert packed fields I would not bother with converting to Char. I would
work with the bytes (System.Byte) themselves & using shifting & anding I
would extact each half of the packed values...

Hope this helps
Jay

<sk****@hotmail.com> wrote in message
news:7a**************************@posting.google.c om...
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vincent Cantin | last post by:
I have a class defined by a template which needs to "say" its type to the user via string. As an example, here is the class that I want to fix : template<class T> class Container : public...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
15
by: Fariba | last post by:
Hello , I am trying to call a mthod with the following signature: AddRole(string Group_Nam, string Description, int permissionmask); Accroding to msdn ,you can mask the permissions using...
1
by: fakeprogress | last post by:
I am attempting to write a function (I shall call it findcode()) that makes sure that a code read in from a file is an actual code, one found within the library of books. Here is what I have:...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.