Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I identify between ASCII and Unicode, and combine bytes as a unicode?

Posted on 2004-10-24
10
Medium Priority
?
216 Views
Last Modified: 2010-04-05
Hi all!

I got some  bytes from device. some are ASCII and some are unicode. How do I identify between ASCII and Unicode, and combine bytes as a unicode. I am not familiar with unicode. I hope someone could tell me how to do it. Thanks all!

Regards,
abdate
0
Comment
Question by:abdate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 12398440
You can't tell. You can guess, perhaps, by assuming they are ASCII and checking if it looks like a sensble string being returned, but neither ASCII nor unicode have any additional information in them to indicate what they are - they are raw blocks of binary.

Perhaps you need to give a little more info on what you're doing?
0
 

Author Comment

by:abdate
ID: 12405385
Thanks andrewjb. I got a stream of Bytes from devices. There are may be ASCII and unicode combined together like this:
48 65 6C 6C 6F C1 C2 C1 C2
And I try to show these mesasge in String 'Hello謝謝'
My question is how do  I translate these bytes into String.
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 12408644
OK. Two problems:

1) How do you know what's ASCII and what's unicode? Why couldn't the C1 and C2 be ascii characters "A acute" and "A circumflex"? (if you're using Arial..)

2) Even if you do know somehow, the VCL components don't support unicode. Full stop. You need another component set if you want to be able to display them. Have a google. Perhaps http://tnt.ccci.org/delphi_unicode_controls/ , or there's another site which I can't find offhand...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:abdate
ID: 12410130
Thanks andrewjb!
I will download & study these unicode controls latter.
But now If I only concern that how to move these buffer bytes into string correctly. and  don't care which is ASCII or unicode.
I try move function like this:
Move(mBuffer(0),S1[1],20)      //S1:string,but I got exception code.

for i:=0 to 20 do
   S1:=S1+CHR(tmpBuf[i]);  //it  cannot move Bytes contain correctly to S1 except ASCII code

Any suggestion for move bytes buffer to string correctly?

abdate

 

 
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 12410300
maybe SetLength( string , len );
then a move.
0
 

Author Comment

by:abdate
ID: 12417176
Thanks andrewjb!
SetLength & Move function is working, but I got result S1 and which is not I want.
I try the following code, I think it is correct, if you could have some function for non ASCII code.

for i:=0 to 20 do
    begin
     if tmpBuf[i]<180 then      // if tmpbuf  byte <180 I suppose it is ASCII Byte
       S1:=S1+CHR(tmpBuf[i])
     else
       S1:=S1+.....               //do you have some function for not ASCII code
    end;

 
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 12419796
What's S1? If it's a 'string' then you CANNOT store unicode in it. It just isn't supported. At all. There's a WideString type which supports (only) unicode characters. Maybe you want that. Since all the ascii characters can be represented in unicode (with a leading byte of zero) they could be stored there. But none of the VCL components support WideString so you couldn't display anything..
0
 

Author Comment

by:abdate
ID: 12421518
Thanks andrewjb!
Yes,S1 is widestring. Bye the way, Japanese & Chinese characters are 2 bytes code and can be display in delphi string. I don't know how they do. but I will be appreciate if you can help me finish the following codeing.

procedure TForm1.Button1Click(Sender: TObject);
var S1:widestring;
      tmpBuf: array of byte
begin
SetLength(mBuffer,20);
for i:=0 to 20 do
    begin
     if tmpBuf[i]<180 then      // if tmpbuf  byte <180 I suppose it is ASCII Byte
       S1:=S1+CHR(tmpBuf[i])
     else
       S1:=S1+.....               //do you have some function for those not ASCII
    end;
end;

abdate
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 400 total points
ID: 12421810
"Japanese & Chinese characters are 2 bytes code...."

They're not unicode. They use the multi-byte character set (MBCS), which IS supported by the VCL components, I believe.


What are you trying to do? Just get the bytes into a WideString? In your original example of :
48 65 6C 6C 6F C1 C2 C1 C2

what should the end result be? A wide string containing the characters:

48 = H
65 = e
6C = l
6c = l
6F = o
C1 C2 = ???
C1 C2 = ???

If you just do
AnsiString s;
s := tmpBuf

where tmpbuf is array of char (not byte)

then assign this to, say, a label caption it works fine..... IF the label is set to the right character set.
e.g. set it to CHINESEBIG5_CHARSET, and the above string appears as Hello then two chinese characters..
0
 

Author Comment

by:abdate
ID: 12427841
Thanks andrewjb!

I appreciate for your help!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question