Continue to Site

Counting non-ASCII charcaters in string

Status
Not open for further replies.

glenjoy

Banned
Advanced Member level 3
Joined
Jan 1, 2004
Messages
962
Helped
72
Reputation
146
Reaction score
20
Trophy points
1,298
Location
Philippines
Activity points
0
arabic counting in ascii

I am having a problem in knowing if how many ASCII or non-ASCII character there is in a certain string, as I've noticed that strlen() only counts ASCII characters and stops on a NULL, so if there is a NULL in between my array, it will stop counting and will leave the sequence.

Is there a command that I will know the lenght or number of ASCII or non-ASCII chracter inside an array?

Thanks.

Added after 13 minutes:

Code:
{
   unsigned char x;
   unsigned char data[] = {'A','B','C','D','E','F','G','H','I',};
   unsigned char data_1[] = {"ABCDEFGHI"};


                                           // results
   printF(" %d ",strlen(data));    // ---- > 18
   printf(" %d ",strlen(data_1));  //----->  9
   
   printF("sf%d",sizeof(data));    //----->  9
   printf(" sf%d",sizeof(data_1)); //---->10
}

Added after 44 seconds:

Your help will be much appreciated.
 

A string and a character array are two different things.

By definition, a C string terminates with NULL. There is no built-in length value. If you poke a NULL character into the middle of a string, then you have effectively shortened the string.

strlen() requires a pointer to a string. It counts chars until it finds a NULL terminator. If you give it a pointer to something else, you get undefined behavior (the value 18 you reported).

You defined those unsigned char arrays, so you and the compiler know their sizes. The sizeof operator gives you the array size in bytes.

More info from the excellent comp.lang.c FAQ:
https://www.eskimo.com/~scs/C-faq/s8.html
 

I had a similar problem. Just now i had to insert a stream of bytes from a fingerprint scanner to a database (field type=text) in my PC. If i just insert it using string datatype, it won't work well because there are many 0x00's in the data stream. The table in the database will store the string incompletely.

My solution was to convert them all to ASCII using the TLP protocol (i got inspired from GemPlus). Basically, a 0x00 would be translated into "00" (two characters), and a 0x1a will be translated to "1A", for example.

But luckily the length(s:string):integer function in Delphi works well despite the null characters inside :p ! (how come, i dunno :D)
 

That's because Delphi doesn't use NULL-terminated strings. Its strings have two parts, the string data and the length (number of characters in the string).
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top