Howto convert Ascii -> UTF-8 & back C++


 
Thread Tools Search this Thread
Top Forums Programming Howto convert Ascii -> UTF-8 & back C++
# 1  
Old 07-02-2003
Error Howto convert Ascii -> UTF-8 & back C++

While working with russian text under FreeBSD&MySQL I need to convert a string from MySQL to the Unicode format.
I've just started my way in C++ under FreeBSD , so please explain me how can I get ascii code of Char variable and also how can i get a character into variable with the specified ascii code.

I know that in Basic there is a commands for that : Asc() and Chr(), is there variant of this on C++?
# 2  
Old 07-02-2003
I'm not sure if this is what you're asking, but you can cast a char to an int.

char c;
c='A';
printf("%d \n", (int) c);
# 3  
Old 07-02-2003
How about trying out the examples here?
# 4  
Old 07-03-2003
thank u guys ... just in case somebody need a solution :
Code:
#include <stdio.h>                                                                                           
#include <stdlib.h>                                                                                          
unsigned char* ASCIItoUNICODE (unsigned char ch);                                                            
unsigned int* ConvertString (unsigned char *string);                                                         
void UnicodePrint(unsigned int* Message);                                                                    
                                                                                                             
                                                                                                             
int main()                                                                                                   
{                                                                                                            
        unsigned char text[] = "Привет,world" ;                                                              
        unsigned int *UnMess;                                                                                
        printf("Content-type: text/plain\n\n\n");                                                            
                                                                                                             
                                                                                                             
        UnMess = ConvertString(text);                                                                        
        UnicodePrint(UnMess);                                                                                
                                                                                                             
}                                                                                                            
                                                                                                             
unsigned char* ASCIItoUNICODE (unsigned char ch)                                                             
{                                                                                                            
unsigned char Val[2];                                                                                        
if ((ch < 192)&&(ch != 168)&&(ch != 184))  {Val[0] = 0; Val[1] = ch;    return Val;}                         
if (ch == 168) {Val[0] = 208;   Val[1] = 129;   return Val;}                                                 
if (ch == 184) {Val[0] = 209;   Val[1] = 145;   return Val;}                                                 
if (ch < 240)  {Val[0] = 208;   Val[1] = ch-48; return Val;}                                                 
if (ch < 249)  {Val[0] = 209;   Val[1] = ch-112;        return Val;}                                         
}                                                                                                            
unsigned int* ConvertString (unsigned char *string)                                                          
{                                                                                                            
    unsigned int size=0, *NewString;                                                                         
        unsigned char* Uni;                                                                                  
    while (string[size++]!=0);                                                                               
        NewString = (unsigned int*)malloc(sizeof(unsigned int)*2*size-1);                                    
        NewString[0]=2*size-1;                                                                               
        size=0;                                                                                              
        while (string[size]!=0)                                                                              
        {                                                                                                    
            Uni = ASCIItoUNICODE(string[size]);                                                              
            NewString[2*size+1]=Uni[0];                                                                      
            NewString[2*size+2]=Uni[1];                                                                      
            size++;                                                                                          
        }                                                                                                    
        return NewString;                                                                                    
}                                                                                                            
                                                                                                             
void UnicodePrint(unsigned int* Message)     
{                                                                                                            
        int size, i;                                                                                         
                                                                                                             
        size=Message[0];                                                                                     
        if (Message[1] == 0) i=2;                                                                            
                else i=1;                                                                                    
                                                                                                             
                while (i<size)                                                                               
                {                                                                                            
                printf("%C",Message[i]);                                                                     
                        i++;                                                                                 
                }                                                                                            
}


Last edited by vino; 02-13-2009 at 03:00 AM.. Reason: add code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert files to UTF-8 on AIX 7.1

Dears, I have a shell script - working perfectly on Oracle Linux - that detects the encoding (the charset to be exact) of the files in a specified directory using the "file" command (The file command outputs the charset in Linux, but doesn't do that in AIX), then if the file isn't a UTF-8 text... (4 Replies)
Discussion started by: JeanM-1
4 Replies

2. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

3. Shell Programming and Scripting

Convert UTF-8 file to ASCII/ISO8859-1 OR replace characters

I am trying to develop a script which will work on a source UTF-8 file and perform one or more of the following It will accept the target encoding as an argument e.g. US-ASCII or ISO-8859-1, etc 1. It should replace all occurrences of characters outside target character set by " " (space) or... (3 Replies)
Discussion started by: hemkiran.s
3 Replies

4. Shell Programming and Scripting

Trying to convert utf-8 to WINDOWS-1251

Hello all i have utf-8 file that i try to convert to WINDOWS-1251 on linux without any success the file name is utf-8 when i try to do : file -bi test.txt it gives me : text/plain; charset=utf-8 when i try to convert the file i do : /usr/bin/iconv -f UTF-8 -t WINDOWS-1251 test.txt >... (1 Reply)
Discussion started by: umen
1 Replies

5. Linux

Help to Convert file from UNIX UTF-8 to Windows UTF-16

Hi, I have tried to convert a UTF-8 file to windows UTF-16 format file as below from unix machine unix2dos < testing.txt | iconv -f UTF-8 -t UTF-16 > out.txt and i am getting some chinese characters as below which l opened the converted file on windows machine. LANG=en_US.UTF-8... (3 Replies)
Discussion started by: phanidhar6039
3 Replies

6. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

7. Shell Programming and Scripting

ASCII to UTF-8 conversion

I Am trying to change the file encoding from ASCII to UTF-8 using below command iconv -f ASCII -t UTF-8 <input_file> > <output_file> But the output_file is not actually in UTF-8 format. If I use the file command to check the file encoding it still says ASCII. While converting am not... (5 Replies)
Discussion started by: Sriranga
5 Replies

8. Red Hat

Can't convert 7bit ASCII to UTF-8

Hello, I am trying to convert a 7bit ASCII file to UTF-8. I have used iconv before though it can't recognize it for some reason and says unknown file encoding. When I used ascii2uni package with different package, ./ascii2uni -a K -a I -a J -a X test_file > new_test_file It still... (2 Replies)
Discussion started by: rockf1bull
2 Replies

9. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

10. UNIX for Advanced & Expert Users

Convert UTF-8 encoded hex value to a character

Hi, I have a non-ascii character (Ŵ), which can be represented in UTF-8 encoding as equivalent hex value (\xC5B4). Is there a function in unix to convert this hex value back to display the charcter ? (10 Replies)
Discussion started by: sumirmehta
10 Replies
Login or Register to Ask a Question