Click to See Complete Forum and Search --> : javascript code to C++ or C#


ade11
05-31-2010, 05:25 AM
The code given below is in javascript form and it is based on speech processing,to extract the unicode from the character.So,anyone please explain me this code in a simplified form as i know very little about javascript.And if possible please write the code below in C-language.

Some tips:-
127 refers to the ASCII value of characters
document.form.unicode.value-----is the dictionary or database of unicodes
CODE:---

/*
convertToEntities()
This is to convert characters to Unicode numbers
*/

function convertToEntities() {
var astr = document.form.unicode.value;
var bstr = '';
for(i=0; i<astr.length; i++)
{
if(astr.charCodeAt(i)>127)
{
bstr += '&#' + astr.charCodeAt(i) + ';';
}
else
{
bstr += astr.charAt(i);
}
}
document.form.entity.value = bstr;
}

Kor
05-31-2010, 05:43 AM
That code replaces some characters in a string. If the numeric Unicode value is greater than 127 (that means if the character is not a direct match of the ASCII character set), that character is replaced with its Numeric Character Reference (NRC) - that means its Unicode numeric value preceded by '&#' and followed by ';'. Else, it is replaced by its Direct Character Unicode Entry (in fact let alone as it is).

Technically, it is not a replacement character by character. The code builds another string, step by step, based on the initial input string, and set him as a value of another form element
Ex:
a will be replaced by a
but
ä will be replaced by ä

ade11
05-31-2010, 06:20 AM
Hiii,can u help me write this code in C-language and how to fetch value from a database created in C-language. Actually the main task is to create a simple unicode converter in C.Unicode is the unique code given to a character,independent of its language.:confused::confused:


That code replaces some characters in a string. If the numeric Unicode value is greater than 127 (that means if the character is not a direct match of the ASCII character set), that character is replaced with its Numeric Character Reference (NRC) - that means its Unicode numeric value preceded by '&#' and followed by ';'. Else, it is replaced by its Direct Character Unicode Entry (in fact let alone as it is).

Technically, it is not a replacement character by character. The code builds another string, step by step, based on the initial input string, and set him as a value of another form element
Ex:
a will be replaced by a
but
ä will be replaced by ä

Kor
05-31-2010, 06:33 AM
Wait a minute! Do you know C (+, ++, #, whichever...)? It this case it will be a piece of cake to create your self this kind of code. It is not rocket science, you know... :)

Du you need C language by all means? I can move this thread to the proper Forum, if you want me to. For me, I could only write the equivalent in PHP(on MySQL if needed). This is the only server language I know, so far.

Sterling Isfine
05-31-2010, 08:02 AM
Hiii,can u help me write this code in C-language and how to fetch value from a database created in C-language. Actually the main task is to create a simple unicode converter in C
Why don't you just switch subjects to something less taxing like graphic design?

Kor
05-31-2010, 08:15 AM
ade11
Let's take it otherwise: What do you know about ASCII, Unicode notations and Numeric Character Reference? That is not JavaScript, nor C, nor any other language. That is about the character codes notation within documents. Mainly within Web related documents.

Why do I start to believe that this is your homework? :rolleyes:

ade11
05-31-2010, 09:03 AM
Hiiii Buddy,
thanks for ur help.Actuly i am an electronics engg student and this unicode part is a small part of my research project.Since i have a poor hand in coding,thats why i am here at forums for some help.

Kor
05-31-2010, 09:22 AM
You have not answered my question: Are you sure you want the code in C variant? I need that for, in affirmative case, I will move this Thread to the C language Forum, where you might get proper assistance.

ade11
06-01-2010, 04:53 AM
Actually since the whole work is being done in C-platform,i have to create the code in C only...i truly appreciate your help...Please redirect me to the right forum if you can do it...thanks..:confused:

sohguanh
06-02-2010, 04:31 AM
The code given below is in javascript form and it is based on speech processing,to extract the unicode from the character.So,anyone please explain me this code in a simplified form as i know very little about javascript.And if possible please write the code below in C-language.


Below is compiled in Linux using gcc version 3.4.6 I did not do the proper error checking cuz I want to focus on the problem. You did not provide a dictionary so I hardcode the values. You should be able to duplicate below and port it to C++ and it will be easier as you can use C++ string data-type instead of using the plain old char using pointer.


#include <stdio.h>
#include <string.h>

int main() {
int dict[] = {1,2,3,130,5,6,7,8,128,129}; //dictionary of Unicodes
char *str = malloc(sizeof(dict));
char tmp[3];
int i = 0;
for(i=0; i< sizeof(dict)/sizeof(*dict); i++) {
if (dict[i]>127){
strcat(str, "&#");
sprintf(tmp, "%d", dict[i]);
strcat(str, tmp);
strcat(str, ";");
} else {
sprintf(tmp, "%d", dict[i]);
strcat(str, tmp);
}
}
printf("%s\n", str);
free(str);

return 0;
}

ade11
06-03-2010, 12:13 AM
thanks for ur C-code which u have written.As of now,i don't have any idea of how to create a dictionary of Unicodes in C-language but i am trying to.It would be great if u could please guide me in this regard.Actually i am building a text to speech synthesizer,but all the C-coding part is new to me.I have the list of unicodes of hindi language with me but how to create a dictionary in C,is a problem.C if you can help me out somehow????

sohguanh
06-03-2010, 01:42 AM
thanks for ur C-code which u have written.As of now,i don't have any idea of how to create a dictionary of Unicodes in C-language but i am trying to.It would be great if u could please guide me in this regard.Actually i am building a text to speech synthesizer,but all the C-coding part is new to me.I have the list of unicodes of hindi language with me but how to create a dictionary in C,is a problem.C if you can help me out somehow????

Can you provide a sample of the set of Unicode for Hindi language which I presume it to be hexa-decimal numbers like 0xFF0xAB ?

The "correct" solution is to load all the Unicode information from the database into a C array. What I have provided is based on your sample and is just a Proof-of-Concept. Of course you can replace the database with a plain text file of course.

Next question you will ask is how do C "talk" to database. Now that really depend on the database. If it is Oracle, they provide C libraries in the form of OCI* function calls or using their Pro*C. If it is MySQL or other database, you got to explore what libraries or API are exposed to C program to call them.

Basically C work at a much lower level in comparison to nowadays Java with their JDBC, PHP with their in-built mysql* functions etc etc.