Click to See Complete Forum and Search --> : Usual char-byte
kBytes
February 13th, 2005, 03:50 PM
Hello, I am sure this has been brought up a lot, and I found a lot of usefull info here, but nothing that could really fix my problem.
Basically I need to know how to convert a char to its hex value.
If I have like a
char c = 'd';
I need to have the hex value of that letter, which according to msdn's virtual key codes, it is 0x44. So how do I get the hex value into a byte like
BYTE pByte;
if I do
pByte = c;
then it has the value of 0x64, how can I fix this?
NigelQ
February 13th, 2005, 05:32 PM
Let's clear up a couple of things here.
0x44 is the hexadecimal code for 'D' in ASCII representation (upper case)
0x64 is the hexadecimal code for 'd' in ASCII representation (lower case)
Now, back to your concern:
char c = 'd';
Yes, I'm sure this has been mentioned many timed here (several by me).
Your variable c has been assigned the value of the lower case letter d.
How computers in general store characters is basically using a numeric representation for the character in question. There are a few basic types of code standards floating around, but a very common one is ASCII (I'll let you look up the acronym, which should explain it better than I can).
For every character, there is a numeric value equivalent in ASCII.
The way the computer stores your 'd' is not as a 'd' (because the brain of the computer doesn't know or care about our alphabet), so this is stored as a number in the 8-bit variable you call a char[acter].
In fact it is stored in binary format, not even as a number, which is represented as follows:
1100100
So, this binary number represents your 'd' that you assigned to the character variable. Being binary, you must convert it to something else to be useful to you. This can be one of the following:
Binary: 1100100
ASCII: d
Integer: 100
Hexadecimal: 0x64
Octal: 0144
You could also convert it into several other numeric formats depending on your needs.
So, your 'd' is equivalent to all of the above. The question remains, how to convert that value into something useful to you...
If you want to print the value out as a hexadecimal string, then the following should work:
printf("My Char is equivalent to 0x%02X in hexadecimal\n", c);
From your original post, I could not tell *why* you needed to convert the value into hexadecimal, so I am unable to suggest a solution for you.
Hope this helps,
- Nigel
kBytes
February 13th, 2005, 08:10 PM
I am trying to simulate keyboard strokes.
keybd_event(pByte,0,KEYEVENTF_EXTENDEDKEY | 0,0);
like that, I know I should use SendInput, but oh well. So I want to be able to take a string and be able to send it through the keyboard like so
for(int i = 0; i<strlen(BUFFER); i++)
{
BYTE pBytes = CharToHex(BUFFER[i]);
keybd_event(pByte,0,KEYEVENTF_EXTENDEDKEY | 0,0);
keybd_event(Bytes,0,KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP,0);
}
the only problem is that I can't make the CharToHex() function
And 1 more problem, for the KeyCode values 0x64 is the Numeric keypad 4 key here is a link to the msdn site.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
NigelQ
February 13th, 2005, 08:33 PM
Ah, you're talking about keyboard codes. This was not clear from your earlier post.
Obviously there are not two keys on the keyboard in front of you for upper case D and lower case d, there's only one key.
So to simulate the uppercase D, I suspect you need to press the shift key first.
// Pseudo code inside your for loop...
BYTE pBytes = BUFFER[i];
if(isalpha(pBytes)) // test for a-z and A-Z
{
// It's a letter
int ScanCode = pBytes; // Put byte in scancode variable
if(isupper(pBytes))
{
// It's uppercase - ScanCode is correct for the key we are sending
Send shift key code
Send ScanCode
Send ScanCode up-code
Send shift key up-code
}
else
{
// It's lower case - ScanCode must be converted to upper case
Send ScanCode - 0x20; // convert to upper case
Send ScanCode up-code
}
}
else
{
// Not alphabetic character, use other method for sending ScanCodes
}
Hope this helps,
- Nigel
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.