|
-
May 25th, 2004, 12:57 AM
#1
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.
-
May 25th, 2004, 01:13 AM
#2
Where is your BCD data? I mean what is the format of this source?
-
May 25th, 2004, 01:27 AM
#3
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.
-
May 25th, 2004, 01:34 AM
#4
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.
-
May 25th, 2004, 01:39 AM
#5
hey chris,
i read ur code snippet ..
thanks lot. i will try it out now.
"They can because they think they can" - Voltaire.
-
May 25th, 2004, 02:38 AM
#6
"They can because they think they can" - Voltaire.
-
May 25th, 2004, 03:04 AM
#7
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?
-
May 25th, 2004, 03:16 AM
#8
-
September 27th, 2008, 02:38 PM
#9
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)() ; 
-
October 27th, 2008, 03:47 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|