Solved

integer represenation of ASCii to char for XML web service

Posted on 2004-08-26
18
310 Views
Last Modified: 2013-11-20
I have been stuck on this for a day. What I am trying to do used to be simple. I want to take an int and convert it to it's ASCii character value. This is to be returned from a web service in xml.

char RGeneratorClass::GetChar(int c)
{

      if(c < 10)
            return (c + 48);
      else
            return (c + 55);
            
}
0
Comment
Question by:MichaelSFuller
  • 9
  • 5
  • 4
18 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Are you thinking of remodelling the functionality of

int __toascii( int c );

?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Ooops, my fault, you're doing something different.

Shoudln't that be

char RGeneratorClass::GetChar(int c)
{

    if(c < 10)
         return (c + 48);
    else
         return 0;
}

You cannot represent a 2-digit number in a single char. Or are you thinking about

char* RGeneratorClass::GetChar(int c)
{
   static char ac [ 12];

    wsprintf ( ac, "%d", i);

   return ac;
}

?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
There are many situations where static buffer is not recommended. It would be better:

char* RGeneratorClass::GetChar(int c, char *buffer)
{
    sprintf ( buffer, "%d", i);

   return buffer;
}

Of course you have to reserve proper space for buffer before calling GetChar()
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>> There are many situations where static buffer is not recommended

There are as many situations where it is useful, and even more where passing a buffer without specifying it's length is even more dangerous.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Look at this recent discussion, it is very similar to yours. As you will see, there are many possible answers.
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21105715.html
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
jkr, you know that GetChar can't be called twice because you will overwrite the 'ac' buffer. At least you have to advice to Michael.
Using your code:

char *value1, *value2;
value1 = obj.GetChar(5);
value2 = obj.GetChar(7);
printf ("%s %s", value1, value2);

Will return 7 7, not 5 7 as expected.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
The whole discussion is *pointless* whithout knowing in what context it is used:

CString value1, value2;
value1 = obj.GetChar(5);
value2 = obj.GetChar(7);

0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
The fact is that this is not the best solution among all the possibilities. Just a capricious preference.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
About the "context", user can re-use the code in a different context without noticing the problem.
Also, other EE members can read the answer and assume it always will work.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Author Comment

by:MichaelSFuller
Comment Utility
Sorry I was at lunch. What I am doing is generating number on a 36 base. For this method I was converting the decimal into 36 base. The caller of the web service needs a key. Here is some of the code:

//Calling function
String* RGeneratorClass::LongToBase(long lPassed, int iBase,int iKeyLength )
    {
      //This just for testing
            long lBasePow[] = new long[iKeyLength];
            long lBase __gc[]=new long __gc[5];
            lBase[0] =1679616;
            lBase[1] =46656;
            lBase[2] =1296;
            lBase[3] =36;
            lBase[4] =1;
      
      return Counter(lPassed,5,0,lBase);
      
      
     }

      char RGeneratorClass::GetChar(int c)
{
      
      if(c < 10)
            return (c + 48);
      else
            return (c + 55);
            
}


      String* RGeneratorClass::Counter(long lAccum, int iSizeof,int iPos, long lBasePow __gc[])
      {

      String* s;
      StringBuilder* sb = new StringBuilder();
      
      
      if(iPos < iSizeof) //Make sure value is in bounds
            sb = sb->Append(GetChar((lAccum /(lBasePow[iPos]))))->Append(Counter((lAccum % lBasePow[iPos]), iSizeof, iPos++, lBasePow));
      
      s=sb->ToString();

      return s; //stop
      }
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
You mean a numeric base of 36? You can do that using

char *_itoa( int value, char *string, int radix );
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
E.g.

   char buffer[20];
   int  i = 5;

   _itoa( i, buffer, 36 );
   printf( "String of integer %d (radix 36): %s\n", i, buffer );
0
 
LVL 5

Author Comment

by:MichaelSFuller
Comment Utility
Yes numeric base 36, from 00001-ZZZZZ. I just want to return from the GetChar function which I pass the integer value of an ASCii character the character not the number. For some reason I keep getting the number back in XML. It used to be easy to do but in .Net 2003 nothing seems to be availible anymore. I'm not sure if the problem is because of unicode or what, but I've tried just about everything.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Well, if '_itoa()' is still available in .NET, it should be easy, just like the above lines.
0
 
LVL 5

Author Comment

by:MichaelSFuller
Comment Utility
Verifying right now.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
According to http://support.microsoft.com/default.aspx?scid=kb;en-us;313836 ("INFO: Map the C Run-Time (CRT) Library Functions to .NET Functions"), there's at least an equivalent:

_itoa, _i64toa, _itow, _i64tow   System.Convert.ToString
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Yup, here ist is: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemconvertclasstostringtopic27.asp ("Convert.ToString Method (Int32, Int32)"):

public: static String* ToString(
   int value,
   int toBase
);

// Example of the Convert::ToString( int, int ) method.
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void RunToStringDemo( )
{
    int values __gc [ ] = {
        Int32::MinValue,
        -100,
        0,
        99999,
        Int32::MaxValue };
    int  radices[ 4 ] = { 2, 8, 10, 16 };

    // Implement foreach( int value in values ).
    IEnumerator*    myEnum = values->GetEnumerator( );
    while( myEnum->MoveNext( ) )
    {
        int value = Convert::ToInt32( myEnum->Current );

        // Iterate through the radices.
        for( int i = 0; i < 4; i++ )
        {
            // Convert a value with a radix.
            int radix = radices[ i ];
            String* valueString =
                Convert::ToString( value, radix );

            Console::WriteLine( S"{0,12} {1,3}   {2}",
                __box( value ), __box( radix ), valueString );
        }
    }
}

void main()
{
    Console::WriteLine(
        S"This example of Convert::ToString( int, int ) "
        S"generates \nthe following output. It converts several "
        S"int values to \nstrings using the radixes supported "
        S"by the method." );
    Console::WriteLine(
        S"\n    Value    Radix   String"
        S"\n    -----    -----   ------" );

    RunToStringDemo( );
}
0
 
LVL 5

Author Comment

by:MichaelSFuller
Comment Utility
Thanks, appreciate it
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now