Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one EditControl where i need to print ASCII value to character which is come from the hardware .

I am new MFC in c++ if you help me its really thank full

What I have tried:

no idea how to do

i have tried normal program to converting ASCII value to character .
the project which i am doing its serial monitoring in mfc .


i hope you help me get me idea about this
Thanks in advance
Posted
Updated 3-Jun-20 6:09am

I think it is important to understand that what you are doing is NOT converting characters. None of this has anything to do with conversions. It is about display formatting. Data can be displayed in any format you want.

What you have is a byte. That byte can range in value from 0 to 255 in base 10. It can range from 0 to FF in base 16 or hexadecimal and 0 to 11111111 in base 2 or binary. Data is data. There is nothing that makes a byte an ASCII character or hexadecimal character or anything else. It is just a byte. What I just showed you are three different interpretations of that byte - it was displayed in three different formats.

Since you are using MFC, consider using the TRACE macro and looking at the output in the IDE. It will let you play with the formatting options. Here is code that will display the same byte in three different formats in the output window of the Visual Studio IDE :
C++
TRACE( "decimal format is : %d\n", myByte );
TRACE( "hexadec format is : %02X\n", myByte );
TRACE( "ASCII format is   : %c\n", myByte );
Note that this is the same piece of data displayed three different ways and it has the SAME value in each case.
 
Share this answer
 
Comments
Member 14837073 3-Jun-20 13:08pm    
Thanks , i will try this
Member 14837073 3-Jun-20 13:42pm    
i starting from the basic ,my requirement is my data is come from hardware based in ASCII format ,Above the code i want to print in static text in mfc ,how do i do that

m_result_ascii ///variable of static text
This has nothing to do with MFC, it is basic computing. If you define a variable as a char type and give it a numeric value and then try to display it, then it will show the actual character, if it is printable. For example:
C++
char inchar = 65;
cout << "Character code 65 is : " << inchar << endl;

will produce the output:
Character code 65 is : A

Similarly if you want the numeric value you just need to cast it to an integer thus:
C++
char inchar = 'Q';
int qvalue = (int)inchar;
 
Share this answer
 
v2
Comments
Member 14837073 3-Jun-20 10:06am    
Thanks ,lets try and i will update once when i complete
Member 14837073 3-Jun-20 11:19am    
for (int i = 0; i < 122; i++){
int asciiVal = rand()%97 + 122;
char asciiChar = asciiVal;
cout << asciiChar << " ";
}
Above the progrmas i getting error ,i want to print character value "a" to "z" and dumbed into static text in mfc

BOOL CAsciidemoDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

char nchar = 65;
m_ASCIIRESULT = nchar; /** here i want to dumbed the data which i will get the
output in cout.
// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}
Richard MacCutchan 3-Jun-20 11:31am    
I have no idea what all that is about. The printable ASCII characters start with the space character at value 0x20 and continue to 0x7E, the tilde '~'. The numbers you are using are somewhat random.

As for your dialog code, if you are collecting characters that you read from some external device then you just need to accumulate them as you read them and then pass the complete set to the text control.
Member 14837073 3-Jun-20 11:40am    
u help me to modified the code which i tried to print ASCII character ..
Richard MacCutchan 3-Jun-20 11:49am    
I have no idea what that means. None of the above code makes any sense in relation to your original question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900