char* to byte

how can i convert a char* to a byte? here is my code

void usbsend(char* str, int len){
  //code
  for(int num=0;num<len;++num){
    if(str[num] == '.'){
      UsbKeyboard.sendKeyStroke(55);
    }else{
      char node[255];
      strcpy(node, "KEY_");
      strcat(node, (const char *)str[num]);
      byte val = (byte)node;
      UsbKeyboard.sendKeyStroke(val);
    }
  }
}
...
usbsend("IEXPLORER.EXE", 13);

when i try to run it i get this error:

In function ‘void usbsend(char*, int)’:
error: cast from ‘char*’ to ‘byte’ loses precision

ideas?

What you're wanting to do is dereference the pointer - but before we get to that there is a problem

node is a pointer to a char array, and while you 'could' dereference it fine (assuming you want to get the first value in the array) it's not a good practice to do so.

What you will want to do is use an index to get the byte vale you are wanting.

Can you give an example of what you are expecting val to be when you send it? It looks as if you are really trying to send a string as a byte, which won't work. (for example, sending I looks to be trying to fit "KEY_I" into a byte)

SilkyPantsDan:
What you're wanting to do is dereference the pointer - but before we get to that there is a problem

node is a pointer to a char array, and while you 'could' dereference it fine (assuming you want to get the first value in the array) it's not a good practice to do so.

What you will want to do is use an index to get the byte vale you are wanting.

Can you give an example of what you are expecting val to be when you send it? It looks as if you are really trying to send a string as a byte, which won't work. (for example, sending I looks to be trying to fit "KEY_I" into a byte)

yes i expect val to be KEY_ "KEY_A, KEY_B, etc" that i can pass to UsbKeyboard.sendKeyStroke which is expecting a byte

Ahh - cool, I'm assuming you are using the USB libraries from here?

Going by that assumption, the KEY_ is a macro definition that gets replaced when the code complies, so you won't really be able to generate it on the fly like that.

The good news is you can still 'hack' your way to it, this example assumes you are using the above mentioned library

void usbsend(char* str, int len)
{
	for(int num=0;num<len;++num)
	{
		if(str[num] == '.')
		{
			UsbKeyboard.sendKeyStroke(55);
		}
		else
		{
			// We grab the current character, which is a byte/char
			byte val = str[num];
			
			// Now looking at the #define macros in UsbKeyboard.h we can see 
			// that the letters range from 4-29 and numbers are from 30-39.
			// 
			// The characters in the string are (most probably) in ASCII format, so we can do things like this
			// All we are doing is offsetting the ascii value so it matches up with the key stroke value
			
			// Convert capitals into keystroke
			// Could probably replace the numbers with the character literals ('A')
			if (val >= 65 && val <= 90)
				val -= 61;
			else if (val >= 97 && val <= 122) // lowercase
				val -= 93;
			else if (val >= 48 && val <= 57)
			{
				// Numbers, but they are 'tricky' since they're in a different order
				if(val == 48) // 0
					val = 39;
				else
					val -= 19;
			}

			UsbKeyboard.sendKeyStroke(val);
		}
	}
}

This should do the trick, can't really test it atm as I'm at work - but that's an idea to do what you're wanting to do :slight_smile:

Yep thats the lib, thank you very much for your help :smiley: I am now at work as well so i will test this out when i get home and let you know how it goes.

worked like a charm, again thank you very much!! XD

Many thanks SilkyPantsDan, i published my work on this here - Arduino Windows Attack Tool | dc414