Length in bytes of text

Hi,

is it possible to get the length in bytes of the data in a field of a form.

The length in characters can be found by using : .length

But since Servoy uses Utf8 the length in bytes can be more then he length in characters for special characters as à or €.

Regards,

You can do this in Java. You can get the length of a String provided an encoding. Truncating is tricky, though, because you could just hit the middle of multi byte character…

I am not realy a java programmer ( i did some "learn yourself Java books, but …)
I would like to solve it using Javasript.

I now use the follwoing trick :

var _tempFile = plugins.file.createTempFile('measure','.txt');
var _suc = plugins.file.writeTXTFile(_tempFile,offekrt_extra_info,'UTF8')
var _siz = _tempFile.size();


if (_suc && _siz >4000) 
{
	var thePressedButton = plugins.dialogs.showErrorDialog('Size', 'To many characters (' + _siz + ')','OK');
	return -1
}

The web searches I did were not helpful in doing it in Javascript.
I played around with getting the bytes length in Java. I think you would have to go an get each “character” individually from the original length in bytes. Then add them all up. Unfortunately the java.lang.String.getBytes() method seems not to be recognized by Servoy/javascript. I think you can do it properly if someone made a plugin in java and did it all within there.

I think you can do it properly if someone made a plugin in java and did it all within there.

Post a request for the Tools Plug-in and I will implement it this week. http://www.it2be.com/support/

Marcel,

I created the ticked.

Thanks !!

With the next release (later today or tomorrow) the following will be available in the Tools Plug-in:

getBytes(String, [encoding);

getByteLength(String, [encoding);

Great job Marcel !!

Thanks,

It is uploaded right now.

By default the encoding is that of the system.

When you want to check UTF-8 you need to explicitely enter it or something like ‘éncore’ will still give you an outcome of 6 instead of 7…

Now if someone has a bright idea how we can prevent truncating a string in the middle of a multi byte character, the solution is perfect…

that would involve testing the byte against the encoding value I guess. That would be an ugly job…