Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Urgent easy question? Asc(a) to C#

Posted on 2004-10-16
12
Medium Priority
?
199 Views
Last Modified: 2008-02-01
I have to avoid the Asc(a)
I put the int (a) instead and it does not work. can someone fix this.

private string DecodeReceipt (string szBill)
{
      string szNewBill;
                 szNewBill = "";
      for (int i = 1; i <= szBill.Length-1;i++)
      {
              char a = szBill[i];
                  if ((Asc)(a) ==10 || (Asc)(a) ==15 || (Asc)(a)==18 || (Asc)(a) ==27)
              {}          
              else if ((Asc)(a) == 13)
            szNewBill = "";
              else
            szNewBill = szNewBill + a;
      }
      return szNewBill;
}


private string DecodeReceipt (string szBill)
{
      string szNewBill;
                 szNewBill = "";
      for (int i = 1; i <= szBill.Length-1;i++)
      {
              char a = szBill[i];
                  if ((int)(a) ==10 || (int)(a) ==15 || (int)(a)==18 || (int)(a) ==27)
              {}          
              else if ((int)(a) == 13)
            szNewBill = "";
              else
            szNewBill = szNewBill + a;
      }
      return szNewBill;
}

0
Comment
Question by:mathieu_cupryk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 6

Expert Comment

by:ryerras
ID: 12329233
Ok, I know you are trying to convert each char to ASCII value. If thats correct you need to use System.Text namespace and Encoding.ASCII. Take a look at the following microsoft example

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextencodingclassasciitopic.asp

Good luck
0
 

Author Comment

by:mathieu_cupryk
ID: 12329283
c:\inetpub\wwwroot\mspos\receipts.aspx.cs(82,10): error CS0246: The type or namespace name 'ascii' could not be found (are you missing a using directive or an assembly reference?)

I raised it to 150.
private string DecodeReceipt (string szBill)
            {
                  string szNewBill;
                  szNewBill = "";
                  // Create an ASCII encoding.
                  Encoding ascii = Encoding.ASCII;
                  //Byte[] encodedBytes = ascii.GetBytes(unicodeString);
                  //String decodedString = ascii.GetString(encodedBytes);
                        
                  //Byte[] encodedBytes = ascii.GetBytes(unicodeString);

                  for (int i = 1; i <= szBill.Length-1;i++)
                  {
                        char a = szBill[i];
                        if ((ascii.GetBytes)(a) ==10 || (ascii.GetBytes)(a) ==15 || (ascii.GetBytes)(a)==18 || (ascii.GetBytes)(a) ==27)
                        {}          
                        else if ((ascii.GetBytes)(a) == 13)
                              szNewBill = "";
                        else
                              szNewBill = szNewBill + a;
                  }
                  return szNewBill;
            }
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12329355
Ok, I didnt mean to say you should Encoding.ASCII as namespace. All you need to do is include System.Text at the top where all the using statements are, as follows

using System.Text;

I am going to modify your code as I understood wht you are doing


private string DecodeReceipt (string szBill)
          {
               string szNewBill;
               szNewBill = "";
               // Create an ASCII encoding.
               Encoding ascii = Encoding.ASCII;
               //Byte[] encodedBytes = ascii.GetBytes(unicodeString);
               //String decodedString = ascii.GetString(encodedBytes);
                   
               Byte[] encodedBytes = ascii.GetBytes(szNewBill );

              foreach (Byte a in encodedBytes)
              {
                   if ( a ==10 || a ==15 ||a==18 ||a ==27)
                    {}          
                    else if (a == 13)
                         szNewBill = "";
                    else
                         szNewBill = szNewBill +(char) a;

               }
       return szNewBill;
}

But onething I still do not understand, why are you making szNewBill=""; at the beginning of the for loop.

good luck
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mathieu_cupryk
ID: 12329364
BEcause I want to build a new string.
From one that has binary characters. Is this right?
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12329369
ok my mistake, I havent seen szBill and szNewBill as different, let me rewrite the above code again

private string DecodeReceipt (string szBill)
          {
               string szNewBill;
               szNewBill = "";
              // Create an ASCII encoding.
               Encoding ascii = Encoding.ASCII;

               Byte[] encodedBytes = ascii.GetBytes(szBill );

              foreach (Byte a in encodedBytes)
              {
                   if ( a ==10 || a ==15 ||a==18 ||a ==27)
                    {}          
                    else if (a == 13)
                         szNewBill = "";
                    else
                         szNewBill = szNewBill +(char) a;
               }
       return szNewBill;
}
0
 

Author Comment

by:mathieu_cupryk
ID: 12329401
c:\inetpub\wwwroot\mspos\receipts.aspx.cs(77,23): error CS0103: The name 'encodedBytes' does not exist in the class or namespace 'MSPOS.Receipts'


private string DecodeReceipt (string szBill)
            {
                  string szNewBill;
                  
                  // Create an ASCII encoding.
                  Encoding ascii = Encoding.ASCII;
                          
                  Byte[] encodedBytes = ascii.GetBytes(szNewBill);

                  foreach (Byte a in encodedBytes)
                  {
                        if ( a ==10 || a ==15 ||a==18 ||a ==27)
                        {}          
                        else if (a == 13)
                              szNewBill = "";
                        else
                              szNewBill = szNewBill +(char) a;

                  }
                  return szNewBill;
            }
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12329424
Sorry pal. I dont see why that error is coming. As you can see, we declared the encodedBytes in the method itself, with

Byte[] encodedBytes = ascii.GetBytes(szNewBill);

I hope you added using System.Text; on the top
And also I hope you are not trying to use it outside the method.
0
 

Author Comment

by:mathieu_cupryk
ID: 12329468
String reference not set to an instance of a String. Parameter name: s
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: String reference not set to an instance of a String. Parameter name: s

Source Error:


Line 72:                   // Create an ASCII encoding.
Line 73:                   Encoding ascii = Encoding.ASCII;
Line 74:                   Byte[] encodedBytes = ascii.GetBytes(szNewBill);
Line 75:                       
Line 76:
 
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12329500
Dude, you are using wrong code from what I gave you,

This is the following I gave, please use that and compile

private string DecodeReceipt (string szBill)
          {
               string szNewBill="";
                             
               // Create an ASCII encoding.
               Encoding ascii = Encoding.ASCII;
                       
               Byte[] encodedBytes = ascii.GetBytes(szBill);

               foreach (Byte a in encodedBytes)
               {
                    if ( a ==10 || a ==15 ||a==18 ||a ==27)
                    {}          
                    else if (a == 13)
                         szNewBill = "";
                    else
                         szNewBill = szNewBill +(char) a;

               }
               return szNewBill;
          }
0
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12336165

char a = 'a';
char.GetNumericValue(a);
0
 

Author Comment

by:mathieu_cupryk
ID: 12337436
Just want to understand the feed back here
this is the method getatoi?

char.GetNumericValue(a);
0
 
LVL 6

Accepted Solution

by:
ryerras earned 600 total points
ID: 12338765
Hey mathieu,
 So, my code didnt work for you?

By the way, GetNumericValue gives the value in integer, if the char is an integer. ok lets look at an example

if i='5';
then char.GetNumericValue(i) returns 5

but if i='a', char.GetNumericValue(a) does not return the ASCII value of 'a', it rather returns -1

I thought you want the ASCII stuff.. Let me know if not.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question