• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1290
  • Last Modified:

c code to convert short integer to string

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
nphoenix
Asked:
nphoenix
  • 10
  • 5
1 Solution
 
josgoodCommented:
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
 
nphoenixAuthor Commented:
C please
0
 
josgoodCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
josgoodCommented:
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
 
josgoodCommented:
I'm happy to answer any unanswered questions.
0
 
josgoodCommented:
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
 
nphoenixAuthor 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.

0
 
josgoodCommented:
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
 
josgoodCommented:
I'm taking my wife to dinner...will be back in an hour or so.
0
 
nphoenixAuthor 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.
0
 
josgoodCommented:
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
 
josgoodCommented:
(the difference is that we now have Display and Display2)
0
 
nphoenixAuthor 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
0
 
josgoodCommented:
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
 
nphoenixAuthor 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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now