Calculate byte size of string

Hi,

I have task to merge multiple XML's to one big XML.
In doing this i have to calculate the byte size of each XML which i will be recieving as a string from a code and append all the recieved XML's to single file.

The reason being the byte size and the offset will help me to extract only the XML i require from the large file.

How can i do this ?

Thanks,
Chetan

Hi,
Try this,

echo $str | wc -c

Cheers,
Ranga:-)

Better use double quotes or spaces get squeezed and use:

printf "%s" "$str" | wc -c 

or the newline introduced by echo gets counted too..

1 Like

Hi ,
Thanks for the reply.

The reason i want the byte size is to know how many bytes an XML can take.
In case the XML will have " i will have to escape it.

In that case will the byte size include the "\" which i'm using to escape "?

Thanks.

You don't need to escape " for the wc -c command. If the " is needed in the XML then the \ is part of the XML and it should be counted, no?

Hi,

I need the " inside the XML so \ will also be counted to give the byte size then right?
Currently in the XML we recieve \ is not a part of it so we have to \ explicitly.

Thanks,
Chetan.C

Why do you need to escape the double quote? If it is inside the string than that should not be necessary, I would think. Have you tried it without the \ . What error message did you get and could you show us what you were trying?

Hi,

My mistake i was not doing it right.
Thanks.