ascii to string conversion.

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
jndlogic
Posts: 6
Joined: Tue Feb 26, 2013 6:47 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

ascii to string conversion.

Post by jndlogic »

Hi all,

I have a friend who isnt able to use his arms, hands. To get him to get around on his own i have made several electronic aids for him. Now i am building a HID with an ecio module.
The project:
On a display most commonly used chars scroll by and when he wants to use a char, he pushes a footswitch.
The ecio sends this char to the pc via usb hid.

I have this all working except the part where i read the value of the variabele (eg dec 65 to display the letter 'A' via print ascii on the display) needs to be converted to the string value 'A' to send via the USB HID.

Is there an algorithm to do such a thing easely, or do i really need to make a string array for all the letters, numbers and symbols.

thanks in advance

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: ascii to string conversion.

Post by JonnyW »

Hello.

Are you asking how to convert the number 65 (say) to the ASCII equivalent 'A' character?

The two forms are equivalent - decimal 65 is ASCII character 'A', or hexadecimal 41 - all are the same thing, just a different representation. If you send byte 65 to a console it will print an 'A'.

If you are asking how to produce a system to scroll through selected letters in order, this is more involved, as it depends what order you want.

To create a string with a single character in it, create an array of 2 characters (the first is your character, the second is the null character to denote the end of the string). Then in a calculation (assuming you are using Flowcode):

Code: Select all

text[0] = 65 // Or any other character
text[1] = '\0' // Null terminator
This is then a string containing the single character.

Cheers,

Jonny

jndlogic
Posts: 6
Joined: Tue Feb 26, 2013 6:47 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: ascii to string conversion.

Post by jndlogic »

Thanks Jonny,

Im still struggling with the hid component.

Is there anyone who can get me started with a working usb hid keyboard "hello world" example.
Lets say, i use an ecio28.

When i plug it in or push the reset button, it behaves as an usb keyboard typing "hello world"... Cant get it to work...

Thanks in advance... Davy

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: ascii to string conversion.

Post by Enamul »

Hi
Have you tried to use this example?
Attachments
Keyboard Demo.fcf
(24.14 KiB) Downloaded 346 times
Enamul
University of Nottingham
enamul4mm@gmail.com

jndlogic
Posts: 6
Joined: Tue Feb 26, 2013 6:47 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: ascii to string conversion.

Post by jndlogic »

I did, but only usable as à worst case scenario. If i start from this example i Will be having 100 or so decisions (most commonly used computerkeyboard symbols)

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: ascii to string conversion.

Post by kersing »

In the HID component you need key codes, not strings or ascii values. There are two ways to translate the ascii values to key codes, one is to create a table with 128 elements (assuming you do not require special characters). Use the ascii value of the character as index. An example:
keycode[48] = 39 (code for 0 is 39)
keycode[49] = 30 (code for 1 is 30)
...
keycode[67] = 38 (code for 9 is 38)
keycode[65] = 4 (code for A is 4)
keycode[66] = 5 (code for B is 5)
...
keycode[90] = 29 (code for Z is 29)
When you need to send the key you simply use the ascii value to index to array and send the result:
xfercode = keycode[index]
(There is no need to fill the table by using an assignment for each element. there are smart ways to initialize the table in one go if you want to go this route.)

The other solution is to do some calculations. The key codes for the characters are consecutive starting at 4 for 'a'/'A'. So if the ascii value of the character to send is between 65 (A) and 90 (Z) subtract 61.

Take a look at this document for the key codes.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

jndlogic
Posts: 6
Joined: Tue Feb 26, 2013 6:47 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: ascii to string conversion.

Post by jndlogic »

Ahaaa... Thanks!!! i was trying to send the ascii value of the key pressed, like in the old days. Guess i'm getting old :) I'll try, and if i get it to work i'll post the code here...

cheers Davy

Post Reply