UCS2 (UTF-16BE) to text

For general Flowcode discussion that does not belong in the other sections.
Post Reply
jollybv
Posts: 150
http://meble-kuchenne.info.pl
Joined: Mon Mar 08, 2021 11:25 am
Location: Cape Town South Africa
Has thanked: 42 times
Been thanked: 13 times

UCS2 (UTF-16BE) to text

Post by jollybv »

Hi Guys im using the Simcom A7682E and there is a problem with the firmware. It only sends me back UCS2 format back when reading the airtime balance. I have spoken the Simcom and they busy working on the firmware but who knows how long it will take. What i need is to make a macro to convert the UCS2 to text. I have asked Ai to help and it came up with this C code but dos not work when i compile it dose not recognize the variables which i have created. Is there a way to create a macro that will convert it as i have no idear where to start with it.

Ai Generated C Code

// UCS2 to Text Conversion - Fixed Version
unsigned int i, j = 0;
unsigned char val;

for(i = 0; i < strlen(CollectNumber)-3; i += 4)
{
val = 0;

// High nibble
if(CollectNumber[i+2] >= '0' && CollectNumber[i+2] <= '9')
val += (CollectNumber[i+2] - '0') * 16;
else
val += (CollectNumber[i+2] - 'A' + 10) * 16;

// Low nibble
if(CollectNumber[i+3] >= '0' && CollectNumber[i+3] <= '9')
val += (CollectNumber[i+3] - '0');
else
val += (CollectNumber[i+3] - 'A' + 10);

CleanText[j++] = val;
}

CleanText[j] = 0; // Null terminate


Compiler Errors


E-P_U6_V-4-6-2_BaseV9_16-05-26_For-A7682E.c: FCM_Read_buffer()
8608: for(i = 0; i < strlen(CollectNumber)-3; i += 4)
^ (361) function declared implicit int (warning)
^ (192) undefined identifier "CollectNumber"
8613: if(CollectNumber[i+2] >= '0' && CollectNumber[i+2] <= '9')
^ (981) pointer required
^ (981) pointer required
(981) pointer required ^
(981) pointer required ^
8614: val += (CollectNumber[i+2] - '0') * 16;
(981) pointer required ^
(981) pointer required ^
8616: val += (CollectNumber[i+2] - 'A' + 10) * 16;
(981) pointer required ^
(981) pointer required ^
8619: if(CollectNumber[i+3] >= '0' && CollectNumber[i+3] <= '9')
^ (981) pointer required
^ (981) pointer required
(981) pointer required ^
(981) pointer required ^
8620: val += (CollectNumber[i+3] - '0');
(981) pointer required ^
(981) pointer required ^
8622: val += (CollectNumber[i+3] - 'A' + 10);
(981) pointer required ^
(981) pointer required ^
8624: CleanText[j++] = val;
^ (192) undefined identifier "CleanText"
^ (981) pointer required
^ (981) pointer required
E-P_U6_V-4-6-2_BaseV9_16-05-26_For-A7682E.c: 8627: too many errors (21)
(908) exit status = 1
(908) exit status = 1

Error returned from [xc8.exe]

C:\ProgramData\MatrixTSL\FlowcodeV9\FCD\PIC\batch\pic_xc8_comp.bat reported error code 0x1

Autoclose turned off


FINISHED

mnfisher
Valued Contributor
Posts: 1976
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 158 times
Been thanked: 936 times

Re: UCS2 (UTF-16BE) to text

Post by mnfisher »

It should be straightforward to display - at least for the standard character set. UCS2 stores data as 16bits so allows 65536 characters - ASCII is 8 bit (32..127) as a subset.

So one option is to just convert from 16bit to 8bit. This will depend on the byte order on the device but:

.char = .ucs2 // char is byte
Or
.char = .ucs2 >>8

For each character...

How do you have the ucs data stored - a macro UCS1ToASCII(UINT data[20], length n) returning a string:
loop .i .n times
.result[.i] = .data[.i] // Strip extra byte from data (0 is end of string char!)
return .result


Martin

mnfisher
Valued Contributor
Posts: 1976
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 158 times
Been thanked: 936 times

Re: UCS2 (UTF-16BE) to text

Post by mnfisher »

In code:

Assuming little-endian here.. If the device uses big-endian data then add the shift.

Martin
Attachments
ucs2.fcfx
(11.51 KiB) Downloaded 20 times

jollybv
Posts: 150
Joined: Mon Mar 08, 2021 11:25 am
Location: Cape Town South Africa
Has thanked: 42 times
Been thanked: 13 times

Re: UCS2 (UTF-16BE) to text

Post by jollybv »

Thanks Martin I will give it a go and see how it goes

Post Reply