Simple UTF-8 C Decoder

December 23, 2008

This is a simple C or C++ code for UTF-8 decoder

// given that this is first byte of the character,
// how many bytes is the character occupy?
int NumberOfUTF8Chars(unsigned char ch)
{
if (ch < 0x80u) return 1; else if (ch < 0xE0u) return 2; else if (ch < 0xF0u) return 3; else if (ch < 0xF8u) return 4; else if (ch < 0xFCu) return 5; else return 6; } // given that this is first byte of the character, // what is the code value of that character? unsigned int ValueOfUTF8Code(const char* ch) { unsigned int Value; int Size = NumberOfUTF8Chars( *ch ); switch( Size ) { case 6: Value = ch[0] & 0x01; break; case 5: Value = ch[0] & 0x03; break; case 4: Value = ch[0] & 0x07; break; case 3: Value = ch[0] & 0x0F; break; case 2: Value = ch[0] & 0x1F; break; case 1: Value = ch[0]; } for ( int i= 1; i < Size; i++ ) { Value = Value << 6 | ch[i] & 0x3F; } return Value; } [/sourcecode] This code was never actually tested. Use it on your own risk.


LM058 communication problem

December 5, 2008

It should not escalated to this level and since it does it a bad evidence on the ‘LM Technologies’ brand.

I am working on a system the include communication from a smartphone to a controller.
The controller support RS232, the smartphone support Bluetooth and to bridge this gap I purchased a ‘Bluetooth Serial adaptor’ from ‘LM Technologies’.

The connection works well until the device sends a string longer than 20-40 characters. Then the LM058 freezes and never recovers. (power cycle require)

I tried all known configurations, I sent question to LM support (there website support is terrible), I posted a question in related forums (and in an unrelated forums :).

At this stage it become apparent that either my device is faulty or just badly designed.

A week after my initial question to LM support I got response that suggested disabling the ‘escape character check’ using the AT command ‘ATX0‘. That indeed solve the problem.

BUT!!!
Why is this feature not documented in the LM058 user manual?? or any other related LM058 documents!!??

In the purpose of this blog I am adding a link to another adaptor manual that actually describe this feature. It seems that LM updated the LM058 adaptor functionality but not the manuals.

here it is: a little piece of information that was not part of the ‘world accumulated knowledge base’ (Internet) and now it is.
In the future I’ll try to be more informative and ‘to the point’ and less ‘blah blah’.