349 views
in System Integration by

Hello.

There was a problem converting ansi character to string.

 


typedef struct
{

  char text[32];
  char id[32];

} MyStringStruct;

...

MyStringStruct test;

test.text = "01234567890123456789012345678901";   // length = 32
test.id = "ABCDEF";

The code is a structure that stores strings.

But if I convert this data to EwNewStringAnsi like below code...

 

// embedded wizard

var string text = "";
var string id = "";

native( text, id )
{
  text = EwNewStringAnsi( test.text );
  id = EewNewStringAnsi( test.id );
}


// trace
trace text;
trace id;

/*
 
  01234567890123456789012345678901ABCDEF
  ABCDEF

*/

Convert until NULL data is found.

Is there any way to limit the length of the conversion of EwNewStringAnsi()? 

1 Answer

0 votes
by
 
Best answer

Hello,

the function EwNewStringAnsi() expects the string to be zero-terminated as this is usual in C. It would be thus better to adapt the MyStringStruct definition to reserve one additional char. Concrete to change the size of a string from 32 to 33. For example:

typedef struct
{
  char text[33];
  char id[33];
} MyStringStruct;

If this is not possible, then you can't use the function EwNewStringAnsi(). Instead, you have to perform a temp. copy step into an array containing an additional zero terminator sign. For example:

var string text = "";
var string id = "";

native( text, id )
{
  // temp. buffers for the strings. Per default filled with zero char.
  char tmpText[ sizeof( test.text ) + 1 ] = { 0 };
  char tmpId  [ sizeof( test.id   ) + 1 ] = { 0 };

  // copy the strings from the 'test' data structure. Use memcpy() or
  // EmWi-own EwCopy() function:
  EwCopy( tmpText, test.text, sizeof( test.text ));
  EwCopy( tmpId,   test.id,   sizeof( test.id ));

  text = EwNewStringAnsi ( tmpText );
  id   = EewNewStringAnsi( tmpId   );
}

Alternative approach omits the local arrays. Instead you have to copy the string sign by sign into a previously created EmWi string. To create such string use the function EwNewStringChar():

var string text = "";
var string id = "";

native( text, id )
{
  int i;

  // First create new EmWi string with enough capacity.
  text = EwNewStringChar( 0, sizeof( test.text ));
  id   = EwNewStringChar( 0, sizeof( id.text   ));

  // Then copy the ANSI string into the EmWi string. Sign by sign.
  for ( i = 0; i < sizeof( test.text ); i++ )
    text[i] = test.text[i];

  // Repeat the operation for the second string.
  for ( i = 0; i < sizeof( test.id ); i++ )
    id[i] = test.id[i];
}

Does it help you further?

Best regards

Paul Banach

by
Thank you. your comment.

Unfortunately, I cannot adjust the length of the buffer.

So I copied it to an arbitrary buffer and convert it.

However, I wish I could limit the length in the EwNewStringAnsi() functions afterwards.
by

In such case you can implement your own NewStringAnsi() function and use it wherever you need to create an EmWi-String from a non-zero terminated ANSI string. This could be the implementation:

XString NewStringAnsi( const char* aSrcString, int aStringLength )
{
  int     i;
  XString s;

  if ( aStringLength <= 0 )
    return 0;

  // First create new EmWi string with enough capacity.
  s = EwNewStringChar( 0, aStringLength );

  if ( !s )
    return 0;

  // Then copy the ANSI string into the EmWi string. Sign by sign.
  for ( i = 0; i < aStringLength; i++ )
    s[i] = aSrcString[i];

  return s;
}

Best regards

Paul banach

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...