Link to home
Start Free TrialLog in
Avatar of nphoenix
nphoenix

asked on

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

Avatar of josgood
josgood
Flag of United States of America image

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;
}
Avatar of nphoenix
nphoenix

ASKER

C please
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);
)
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.
I'm happy to answer any unanswered questions.
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);
}
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.

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.
I'm taking my wife to dinner...will be back in an hour or so.
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.
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);
}
(the difference is that we now have Display and Display2)
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
ASKER CERTIFIED SOLUTION
Avatar of josgood
josgood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)   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