String to HEX conversion in UNIX

i have this below string which i need to convert it to HEX. i have already tried it but it showing extra few things on it.. let me show what i have done and what is the output i am getting and what is the desired output
the input string is

 
"!\"\"\"\"\"\"\"!\"\"\"\"\"\"\""

which is technically nothing but below

!"""""""!""""""""

so as for now consider that string is already present in variable myString

myString=$( echo $myString | sed 's/\\//g' )
#above command to strip \ in between '"'
 
len=${#myString}
cutString=${myString:1:$len-1}
#above command to strip additional '"' at first and last character.
 
echo $cutString
echo $cutString | xxd  -c 256 -ps
#above command to convert it to HEX

output obtained as of now is as below

21222222222222222122222222222222220a

BUT the actual output for

!"""""""!""""""""

should be

2122222222222222212222222222222222

32 characters.

as according to

http://www.string-functions.com/string-hex.aspx

any help on this is deeply appreciated. thanks

So the problem is the 0a? That's a ^J (newline).

echo $cutString | xxd  -c 256 -ps | sed "s/..$//"
1 Like

so everytime that 0a will be a newline characters? so everytime i can just directly cut last 2 characters is it?

Yes, I guess so. I didn't see an xxd option to remove it.

It would not be there if you used printf instead of echo, actually.

printf $cutString | xxd  -c 256 -ps
1 Like

oh okay.. got it.. its working as i wanted it... thanks a lot for the fix :-).