joshua siew said:
Thanks a lot Ben.
Btw,
Any idea how to convert a given ASCII(1digits) code to HEX(2digit)...e.g
'A'=41H, 'B'=42H........If there's a function then it will surely be more
better....thanks in advanced.
For numeric literals, you already seem to have it mostly right:
'A', 0x41, and 65 are all equivalent.... mostly.
'A', char(0x41) and char(65) are exactly equivalent.
int('A'), 0x41 and 65 are exactly equivalent.
Most compilers do not flag a warning for a narrowing conversion of a
compile-time constant which fits in the smaller variable. Most calling
conventions also pass character arguments and integer arguments the same
way, one machine word on the stack or one CPU register. So the only time
the distinction really matters is when calling overloaded functions.
They are all stored identically inside the program, the compiler just
recognizes multiple forms for your convenience.
If you're asking for display purposes:
char a = 'a';
printf("%c", a); // a
printf("%d", a); // 97
printf("%x", a); // 61
printf("%xh", a); // 61h
printf("0x%02x", a); // 0x61
Pick your poison.
With the C++ standard library (using namespace std
:
cout << a; // a
cout << int(a); // 97
cout << hex << int(a); // 61
cout << hex << int(a) << "h"; // 61h
cout << "0x" << setw(2) << hex << int(a); // 0x61
-Joshua Siew
Ben Voigt said:
chris,
thanks for the reply but according to BASIC sample program it looks
like
this....
...
...
ENQ$ = CHR$(5)
...
...
OPEN "COM1":"AS#1"
SENDATA$="00FFTT204ABCD34"
PRINT #1, ENQ$;SENDATA$
...
...
I just want to translate this to MS VC++. Any idea ?
#include <stdio.h>
int main(int argc, char** argv)
{
FILE* serial = fopen("COM1:", "wb");
char alldata[] = { 5 /* ENQ */, '0', '0', 'F', 'F', 'T', 'T', '2', '0',
'4',
'A', 'B', 'C', 'D', '3', '4' };
fwrite(alldata, 1, sizeof alldata, serial);
....
....
fclose(serial);
}
Rgds,
Joshua from PG, Malaysia
:
joshua siew wrote:
heeeeeeeeeeeeeeeeeelpp !!!
Can anyone tell me how the heck does VC++ create the ASCII 'ENQ'
????
I'm on
a project of programming a PLC devices. I need to send the ENQ ascii
value
together with others data in a string into the plc. I'm also new in
VC++ as
well as plc...
Thanks for the advice in advanced.
Rgds,
Joshua Siew
mystring[somewhere] = 5;
or perhaps clearer:
#define ENQ 0x5
....
mystring[somewhere] = ENQ;
Chris