Need some help on String to ASCII value 
Author Message
 Need some help on String to ASCII value

I read a string which is encrypted text, so it contains nothing but
the ascii characters.

When I get to the point of converting each character to the ascii
value for formula purposes, I have no problems unless the character is
the ascii equivalent of Chr$(128).

I've tried various ways to convert the character to an ascii value,
and all seem to yield the same result.

In my example below you will see Method 1 as it would generally be
received as is, but in Method 2 I forced the ascii value of 128 to see
if it would still {*filter*}but it works fine.  Just included Method 2 for
simplistic reference.

<-- Code Snip -->
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Method 1 ; use the ASCII character
char[] chrMan  = new char[1] { '€' };

// Method 2 ; use the ASCII value
chrMan[0] = (char)128;

Console.WriteLine("(int) " + (int)chrMan[0]);
// Method 1 yields: 8364
// Method 2 yields: 128

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

I have other values above 128 that convert just fine.  I think the
highest that I convert is an ascii value of 240.  It's only when I
encounter the ascii character for ascii value 128 that I encounter
this problem.

In an effort to attempt troubleshooting I tried to run the following
filter:

<-- Code Snip -->
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
for (int i=0;i<chrMan.Length;i++)
{
  Console.WriteLine("("+i+") " + chrMan[i]);

  for (int j=0; j<255; j++)
  {
    char tmpChar = (char)j;
    if (tmpChar == chrMan[i])
    {
      Console.WriteLine("<J> " + j);
      break;
    }
  }

Quote:
}

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

..and it never finds the Ascii value when I pass the Ascii character
in, but it will find the Ascii value of 128 if I manually force the
value per Method 2.

Any feedback for a solution is welcomed. ;)

--Riggs



Sat, 23 Oct 2004 07:01:59 GMT  
 Need some help on String to ASCII value

Quote:

> // Method 1 ; use the ASCII character
> char[] chrMan  = new char[1] { '€' };

There is one big misunderstanding...

- ASCII is only from 0 to 127
- char is not a 'byte' it is a 'short' (and not ASCII it is UNICODE)

So also

char c = 12334; is valid!

--
Greetings
  Jochen



Sat, 23 Oct 2004 14:10:24 GMT  
 Need some help on String to ASCII value
chars/strings in .NET are strictly Unicode.
You can see the first page (0..255) of unicode here:
  [identical iso]
    http://www.*-*-*.com/

  [Basic Latin & Latin-1 Supl. : PDF]
    http://www.*-*-*.com/

you see Unicode codes 128-160 [U+0080..U+00A0] are non-printable...

What you want to handle is a codepage, I guess Windows 1252:
  http://www.*-*-*.com/

codepage 1252 is (on Win OS) equal to .NET 'Encoding.Default',
convert like this:
 // ----------------------------------------------------------
 using System.Text;
 byte[] codes = new byte[] { 128, 129, 130, 131, 132 };

 // bytes => unicode string / char[]
 string txt = Encoding.Default.GetString( codes );
 char[] chars = Encoding.Default.GetChars( codes );

 // reverse: unicode string / char[]  =>  bytes
 byte[] revtxt = Encoding.Default.GetBytes( txt );
 byte[] revchr = Encoding.Default.GetBytes( chars );

 // ----------------------------------------------------------

--
  NETMaster  (Thomas Scheidegger)
  http://www.*-*-*.com/

Quote:

> I read a string which is encrypted text, so it contains nothing but
> the ascii characters.
> When I get to the point of converting each character to the ascii
> value for formula purposes, I have no problems unless the character is
> the ascii equivalent of Chr$(128).
> I've tried various ways to convert the character to an ascii value,
> and all seem to yield the same result.
> In my example below you will see Method 1 as it would generally be
> received as is, but in Method 2 I forced the ascii value of 128 to see
> if it would still {*filter*}but it works fine.  Just included Method 2 for
> simplistic reference.

> <-- Code Snip -->
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> // Method 1 ; use the ASCII character
> char[] chrMan  = new char[1] { '?' };

> // Method 2 ; use the ASCII value
> chrMan[0] = (char)128;

> Console.WriteLine("(int) " + (int)chrMan[0]);
> // Method 1 yields: 8364
> // Method 2 yields: 128

> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

> I have other values above 128 that convert just fine.  I think the
> highest that I convert is an ascii value of 240.  It's only when I
> encounter the ascii character for ascii value 128 that I encounter
> this problem.

> In an effort to attempt troubleshooting I tried to run the following
> filter:

> <-- Code Snip -->
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> for (int i=0;i<chrMan.Length;i++)
> {
>   Console.WriteLine("("+i+") " + chrMan[i]);

>   for (int j=0; j<255; j++)
>   {
>     char tmpChar = (char)j;
>     if (tmpChar == chrMan[i])
>     {
>       Console.WriteLine("<J> " + j);
>       break;
>     }
>   }
> }
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> ..and it never finds the Ascii value when I pass the Ascii character
> in, but it will find the Ascii value of 128 if I manually force the
> value per Method 2.



Sat, 23 Oct 2004 15:50:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Determination of value type in ascii string

2. Extracting Ascii Value from String?

3. Extracting Ascii Value from String?

4. Extracting Ascii Value from String?

5. newbie needs help to strings, strings, strings

6. Sort an ASCII file - need help!

7. HELP - need C function to convert EBCDIC to ASCII

8. Help? need function to convert EBCDIC to ASCII

9. Float to ASCII I need some help.

10. HELP: NEED SOURCE CODE FOR ASCII TEXT VIEWER

11. WideCharToMultiByte not converting UNICODE string to ASCII String

12. How to convert UNICODE string to ASCII string

 

 
Powered by phpBB® Forum Software