views:

89

answers:

2

I have unicode string passed to vbscript procedure (not visual basic 6, but vbscript). I want to iterate unicode string char by char, get code for every symbol, truncate code to byte range [0..255] and create array of bytes.

This way new array of bytes should be twice smaller in memory compared to original unicode string. I am going save this array to file via ADODB.Stream object further

How can I convert unicode string to bytes array with symbol code truncated to byte range?

Thank you in advance!

+1  A: 

Firstly, translating unicode to ascii will only work if your string only contains ascii characters. Since unicode contains ascii, it is just a matter of removing every second character.

Look up unicode on the internet for details.

EDIT: In unicode, every ascii character is proceeded with a NULL (0) byte. Remove this byte to convert the string to ASCII.

Alexander Rafferty
Thank you for the response but there is no CharW function in vbs. I found walkaround how to create array of bytes in vbs I would describe in a few days
Andrew Florko
A: 

It seems there is no way to create array of bytes in vbs (though it's very straightforward in visual basic) -- all arrays are arrays of variants.

The task was to send binary stream from server to vbs script via string type. I have found the solution by creating Xml Document on the server with CDATA section that contains base64 coded array of bytes as string data.

Client (vbs) do the following:

set xmlDoc = CreateObject("Microsoft.XmlDom")
xmlDoc.loadXML(dataFromServer)
base64str = xmlDoc.DocumentElement.Text  ' it's base64 coded binary stream
arrayOfBytes = decodeBase64(base64str)

Function decodeBase64(base64)  
  set dm = CreateObject("Microsoft.XMLDOM")
  set el = dm.createElement("tmp")
  el.DataType = "bin.base64"
  el.Text = base64
  decodeBase64 = el.NodeTypedValue
  set dm = Nothing
End Function
Andrew Florko