How to get the size in bytes of a string value in webMethods developer?

Hi

I need to know the size in bytes of a string value in a flow service.

What is the easiest way of doing this? I looked for it in WMPublic but in vain.

Kind regards Mikael

I have written a Java service to do this in the past but you can have a look at the pub.string:length service.

If that doesn’t work I can post the code here.

Hi Mahesh

thanks for a very quick answer :slight_smile:

As far as I understand the pub.string:length service returns the number of characters, and I have used that at the moment where I multiply that value with 2 to get a rough estimate on the number of bytes.

I would love to see your java code.

Kind regards Mikael

Sure, I will!

Meanwhile have a look at this.

https://www.pixelstech.net/article/1427944920-String-length()-vs-String-getBytes()-length-in-Java

Hi,

what about of stringToBytes and bytesLength?

If there is no Built-In variant there should be services in WmTransformations and PSUtilities, which are available in the community for download.

Regards,
Holger

Hi Mikael,

I checked the code base and the java service is in-line with the link that I posted earlier String.getBytes().length

// pipeline
		IDataCursor pipelineCursor = pipeline.getCursor();
			String	inString = IDataUtil.getString( pipelineCursor, "inString" );
			int size = 0;
								
			byte[] bytes;
			
			try {
				bytes = inString.getBytes("utf-8");
				//bytes = inString.getBytes("iso-8859-1");
				System.out.println("bytes = "+Arrays.toString(bytes));
				System.out.println("bytes.length = "+bytes.length);
				
				size=bytes.length;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
		IDataUtil.put( pipelineCursor, "size", size );
		pipelineCursor.destroy();

Let me know if you have more questions?

If you need to know the exact size in bytes then you should be aware that it depends on the encoding used. E.g. the letter ‘Ä’ is represented as one byte if ISO-8859 ist used, and as two bytes if UTF-8 is used.

1 Like