We help IT Professionals succeed at work.

c code to convert short integer to string

nphoenix
nphoenix asked
on
Medium Priority
1,326 Views
Last Modified: 2012-05-06
I am not a c coder but need to fix some keymapping code. In my debugging i want to display the integer value of the key pressed:

MessageBox(0,key,"Key Pressed",1);

but key is a Short Integer

my compiler will not take key.ToString()


short	key;
	short scancode;
 
 
	// if we are not accepting keys, pass it on
	if (!gInterceptKeys)
		{
	   	CallNextHookEx(NextProc, nCode, wParam, lParam);
   		return 0;
	   	}
   
	if (!(lParam & 0x80000000L) && nCode != HC_NOREMOVE)
	{
		/* what key is it ? */
		scancode	= HIWORD((lParam & 0x00FF0000L));
 
		if (GetAsyncKeyState( VK_SHIFT ) != 0) 
		{
			if (screen.keyboard == 1)
			{
				key		= keysFRLS[scancode];
			}
			else
			{
				key		= keysLS[scancode];
			}
		}

Open in new window

Comment
Watch Question

Top Expert 2007

Commented:
Are you looking for a C language solution or a C++ language solution?

One C++ solution is
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
 
int main () {
      short scancode;

   scancode = 0x1234;
   // Example if you just want to display the value
   cout << dec << scancode << endl;

   // Example if you want to convert it to a string
   stringstream ss;
   string toString;
   // send the scancode to the stringstream
   ss << scancode;
   // stringstream converts to output type, which is string
   ss >>toString;
   cout << toString << endl;
}

Author

Commented:
C please
Top Expert 2007

Commented:
In C language, you can

void Display(unsigned char value) {
   unsigned char msb = (unsigned char)(value & 0xf0) >> 4;
   unsigned char lsb = (unsigned char)(value & 0x0f);
   printf("%d%d",msb,lsb);
}
 
void Display(short value) {
   unsigned char msb = (unsigned char)((value & 0xff00) >> 8);
   unsigned char lsb = (unsigned char) (value & 0x00ff);
   Display(msb);
   Display(lsb);
}

int main () {
   short scancode;
   scancode = 0x1234;
   Display(scancode);
)
Top Expert 2007

Commented:
Display(short value) takes the scancode and breaks it up into the high and low bytes -- the most significant and least significant.

Display(unsigned char value) takes a byte and breaks it up into the high and low nibbles and then prints each nibble.

(value & 0xff00) isolates the most significant byte
(value & 0x00ff) isolates the least significant byte.
(similar logic for isolating the nibbles.

The shifts (>> 8 and >>4) move the bits we're interested in.
Top Expert 2007

Commented:
I'm happy to answer any unanswered questions.
Top Expert 2007

Commented:
Rereading your question, I realized that you want the output in a string.  You're not looking to print the string.

Here's the same code reworked to output to a string.  sprintf is like printf in that it prints, but it prints to a memory location, not to the display.


char resultString[5];
 
void Display(unsigned char value, int stringOffset) {
   unsigned char msb = (unsigned char)(value & 0xf0) >> 4;
   unsigned char lsb = (unsigned char)(value & 0x0f);
   sprintf(resultString+stringOffset,"%d%d",msb,lsb);
}
 
void Display(short value) {
   unsigned char msb = (unsigned char)((value & 0xff00) >> 8);
   unsigned char lsb = (unsigned char) (value & 0x00ff);
   int stringOffset = 0;
   Display(msb,stringOffset);
   stringOffset += 2;
   Display(lsb,stringOffset);
}

int main () {
   short scancode;
      scancode = 0x1234;
      memset(resultString,0,5) ;
      Display(scancode);
      printf(resultString);
}

Author

Commented:
I added :


void Display(short value) {
   unsigned char msb = (unsigned char)((value & 0xff00) >> 8);
   unsigned char lsb = (unsigned char) (value & 0x00ff);
   int stringOffset = 0;
   Display(msb,stringOffset);
   stringOffset += 2;
   Display(lsb,stringOffset);
}

And i then tried:

MessageBox(0,Display(key),"Key Pressed",1);

But i get

C:\Projects\Screen.c(576) : warning C4013: 'Display' undefined; assuming extern returning int
C:\Projects\Screen.c(576) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
C:\Projects\Screen.c(576) : warning C4024: 'MessageBoxA' : different types for formal and actual parameter 2
C:\Projects\Screen.c(1478) : error C2371: 'Display' : redefinition; different basic types
Error executing cl.exe.

Top Expert 2007

Commented:
You're missing the first function
void Display(unsigned char value, int stringOffset) {
   unsigned char msb = (unsigned char)(value & 0xf0) >> 4;
   unsigned char lsb = (unsigned char)(value & 0x0f);
   sprintf(resultString+stringOffset,"%d%d",msb,lsb);
}

There are two functions named Display here, differentiated by their argument types.
Top Expert 2007

Commented:
I'm taking my wife to dinner...will be back in an hour or so.

Author

Commented:
Please enjoy the evening.

I see how one function calls the other now.

But after adding my compile offers:

C:\Projects\Screen.c(576) : warning C4013: 'Display' undefined; assuming extern returning int
C:\Projects\Screen.c(576) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int '
C:\Projects\Screen.c(576) : warning C4024: 'MessageBoxA' : different types for formal and actual parameter 2
C:\Projects\Screen.c(1481) : error C2371: 'Display' : redefinition; different basic types
C:\Projects\Screen.c(1487) : error C2371: 'Display' : redefinition; different basic types
Error executing cl.exe.
Top Expert 2007

Commented:
Whack (slap to the forehead)

My apologies...I was compiling with a C++ compiler and forgot that C doesn't allow two functions of the same name.  I've made a small change to my original code, which now compiles with a C compiler.

void Display2(unsigned char value, int stringOffset) {
   unsigned char msb = (unsigned char)(value & 0xf0) >> 4;
   unsigned char lsb = (unsigned char)(value & 0x0f);
   sprintf(resultString+stringOffset,"%d%d",msb,lsb);
}
 
void Display(short value) {
   unsigned char msb = (unsigned char)((value & 0xff00) >> 8);
   unsigned char lsb = (unsigned char) (value & 0x00ff);
   int stringOffset = 0;
   Display2(msb,stringOffset);
   stringOffset += 2;
   Display2(lsb,stringOffset);
}
Top Expert 2007

Commented:
(the difference is that we now have Display and Display2)

Author

Commented:
I am truly a C novice.

MessageBox(0,Display(key),"Key Pressed",1);

And the new code produces:

C:\Projects\Screen.c(595) : error C2095: 'MessageBoxA' : actual parameter has type 'void' : parameter 2
C:\Projects\Screen.c(595) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'const int '
C:\Projects\Screen.c(595) : warning C4024: 'MessageBoxA' : different types for formal and actual parameter 4
C:\Projects\Screen.c(595) : error C2198: 'MessageBoxA' : too few actual parameters
Top Expert 2007
Commented:
The second parameter is Display(key)
which is typed void Display(short value).
void means it returns no value.

If you're using the name resultString for the string that you're printing to, then
   MessageBox(0,resultString,"Key Pressed",1);
should do the job

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
:)   THANK YOU!

char resultString[5];
 
 
 
void Display2(unsigned char value, int stringOffset) {
   unsigned char msb = (unsigned char)(value & 0xf0) >> 4;
   unsigned char lsb = (unsigned char)(value & 0x0f);
   sprintf(resultString+stringOffset,"%d%d",msb,lsb);
}
 
void Display(short value) {
   unsigned char msb = (unsigned char)((value & 0xff00) >> 8);
   unsigned char lsb = (unsigned char) (value & 0x00ff);
   int stringOffset = 0;
   Display2(msb,stringOffset);
   stringOffset += 2;
   Display2(lsb,stringOffset);
}
 
memset(resultString,0,5) ;
Display(key);
 
MessageBox(0,resultString,"Key Pressed",1);

Open in new window

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.