dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: BCD to ASCII conversion

  1. #1
    Join Date
    Aug 2003
    Location
    Japan
    Posts
    146

    BCD to ASCII conversion

    I want to convert from BCD to ASCII ?

    How to write a program for that?

    Code snippet or logic would be grateful!
    "They can because they think they can" - Voltaire.

  2. #2
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871
    Where is your BCD data? I mean what is the format of this source?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  3. #3
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    kumaru_san,

    Simply convert each individual BCD byte to a ASCII character by adding 0x30 to the BCD byte.

    Take a look at the code below in C++.

    Sincerely, Chris.

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    typedef unsigned char UINT8;
    
    bool BCD_to_ASCII(const UINT8* bcd, const int length, ::std::string& str_ascii)
    {
      if(length)
      {
        ::std::stringstream ss;
    
        for(int i = 0; i < length; i++)
        {
          ss << static_cast<char>(bcd[i] + 0x30);
        }
    
        ss << ::std::endl;
    
        ss >> str_ascii;
    
        return true;
      }
      else
      {
        return false;
      }
    }
    
    int main(int argc, char* argv[])
    {
      UINT8 bcd[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
      ::std::string str;
    
      BCD_to_ASCII(bcd, sizeof(bcd)/ sizeof(bcd[0]), str);
    
      ::std::cout << str << ::std::endl;
    
      return 0;
    }
    You're gonna go blind staring into that box all day.

  4. #4
    Join Date
    Aug 2003
    Location
    Japan
    Posts
    146

    c++

    hello again..

    for example if i input as "47" i need the ASCII value of 47.

    4 - 0100

    7- 0111

    so i write 47 in BCD as

    4 7
    0100 0111


    public static string BCDTOASCII(string sourcestring)
    {

    // convert the sourcestring (BCD string) to ASCII string
    // and return the value
    // here the BCD string 47 has to converted into ASCII string

    }


    How to do that?
    Could u help me out ?

    The above function is available in Java libraray.
    I want to write same one in C++.

    "They can because they think they can" - Voltaire.

  5. #5
    Join Date
    Aug 2003
    Location
    Japan
    Posts
    146
    hey chris,

    i read ur code snippet ..
    thanks lot. i will try it out now.
    "They can because they think they can" - Voltaire.

  6. #6
    Join Date
    Aug 2003
    Location
    Japan
    Posts
    146
    but why do we add 0x30 ?
    "They can because they think they can" - Voltaire.

  7. #7
    Join Date
    Feb 2003
    Location
    Bilbao
    Posts
    513
    Because the decimal value for the '0' Ascii is 0x30...

    That is:

    '4' = 4 + '0' = 4 + 0x30.

    However if you have a decimal value that ranges from 0 to 0xff you have first splitt it into three decimal digits (that goes from 0 to 9) or two hexadecimal digits (that goes from 0 to f)
    Caronte
    Si tiene solución... ¿por qué te preocupas?
    Si no tiene solución... ¿por qué te preocupas?

  8. #8
    Join Date
    Aug 2003
    Location
    Japan
    Posts
    146
    thanks lot.
    I understood now.
    "They can because they think they can" - Voltaire.

  9. #9
    Join Date
    May 2000
    Location
    Indore (MP) INDIA.
    Posts
    356

    Thumbs up Re: BCD to ASCII conversion

    Hey dude_1967,

    I was trying to interpret BCD byte array. I seen your solution and wow it worked.

    3 cheers to your solution :-)

    *(Vipul)() ;
    (*Vipul)() ;

  10. #10
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: BCD to ASCII conversion

    Hi,

    I just had a look at this old thread.

    It is not necessary to modify anything.

    However I might code this functionality slightly differently these days. I would probably try to compact the code a bit through a sensible use of the standard library function std::transform as shown below. The resulting code is more compact and probably better in this alternate form.

    Sincerely, Chris.

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    
    typedef unsigned char UINT8;
    
    namespace
    {
      static char BCD_to_ASCII(const UINT8& u)
      {
        return static_cast<char>(u + static_cast<UINT8>(0x30u));
      }
    }
    
    int main(int argc, char* argv[])
    {
      // Declare the data which should be converted BCD.
      static const UINT8 bcd_data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    
      // Put these data into a standard vector container.
      static const std::vector<UINT8> bcd(bcd_data, bcd_data + (sizeof(bcd_data) / sizeof(bcd_data[0])));
    
      // Declare an output string for the conversion and be sure that it is created with
      // enough storage space.
      std::string str(bcd.size(), static_cast<char>('\0'));
    
      // Use the standard function std::transform in combination with a unary function for
      // the conversion.
      std::transform(bcd.begin(), bcd.end(), str.begin(), ::BCD_to_ASCII);
    
      std::cout << str << std::endl;
    }
    You're gonna go blind staring into that box all day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)