?
Solved

c code to convert short integer to string

Posted on 2009-02-09
15
Medium Priority
?
1,261 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

0
Comment
Question by:nphoenix
  • 10
  • 5
15 Comments
 
LVL 13

Expert Comment

by:josgood
ID: 23596344
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;
}
0
 

Author Comment

by:nphoenix
ID: 23596378
C please
0
 
LVL 13

Expert Comment

by:josgood
ID: 23596401
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);
)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:josgood
ID: 23596415
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.
0
 
LVL 13

Expert Comment

by:josgood
ID: 23596421
I'm happy to answer any unanswered questions.
0
 
LVL 13

Expert Comment

by:josgood
ID: 23596472
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);
}
0
 

Author Comment

by:nphoenix
ID: 23596497
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.

0
 
LVL 13

Expert Comment

by:josgood
ID: 23596610
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.
0
 
LVL 13

Expert Comment

by:josgood
ID: 23596611
I'm taking my wife to dinner...will be back in an hour or so.
0
 

Author Comment

by:nphoenix
ID: 23596769
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.
0
 
LVL 13

Expert Comment

by:josgood
ID: 23596897
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);
}
0
 
LVL 13

Expert Comment

by:josgood
ID: 23596902
(the difference is that we now have Display and Display2)
0
 

Author Comment

by:nphoenix
ID: 23597319
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
0
 
LVL 13

Accepted Solution

by:
josgood earned 2000 total points
ID: 23597456
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
0
 

Author Comment

by:nphoenix
ID: 23601172
:)   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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question