Click here to Skip to main content
14,607,924 members
Rate this:
Please Sign up or sign in to vote.
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
Updated 3-Jun-20 6:09am
Rate this:
Please Sign up or sign in to vote.

Solution 2

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 :
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.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
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:
char inchar = 'Q';
int qvalue = (int)inchar;
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()

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

// IDM_ABOUTBOX must be in the system command range.

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
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.
jeron1 3-Jun-20 12:23pm
for (int i = 0; i < 122; i++){
int asciiVal = rand()%97 + 122;
char asciiChar = asciiVal;
cout << asciiChar << " ";

Is this how are trying to print 'a' through 'z'?
Member 14837073 3-Jun-20 13:06pm
Yeh something wrong is there ,no any idea
jeron1 3-Jun-20 13:16pm
how about something like;
    for (int i = 'a'; i <= 'z'; i++) 
         cout << i <<" "; 
Member 14837073 3-Jun-20 13:42pm
Above code is character to ASCII value ,(WHICH is did code based on that)
which i mentioned ASCII code convert to character (my expected).

TRACE( "ASCII format is : %c\n", myByte ); //solutions 2 it help me out in mfc to get character
jeron1 3-Jun-20 14:01pm
Sorry I am not following what you are trying to do. Are you trying to put characters in an Edit box that is in a dialog? If so, how about calling SetWindowText() method of your edit control, with whatever text you want.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100