Efficient converting from byte-string to word-string 
Author Message
 Efficient converting from byte-string to word-string

Hallo,

I have an ASP string which reads Request.Request.BinaryRead(size). Although
docs tell that it returns an array of bytes, that wrong. It returns a
byte-string.

All the rest of my ASP code is in JScript, and JScript doesn't have such
variable type as string of bytes. All the strings user characters of 32 bit
codes. Therefore, to user the Request.Request.BinaryRead(size) byte-string
in JScript, I need to convert it to word-string

This code works fine:

bin = Request.BinaryRead(size)
str = ""
For I = 1 To LenB(bin)
    str = str & Chr(AscB(MidB(bin, I, 1)))
Next

Alas, it is pretty slow and for larger request contents, unforgivably slow.
I would like to know if there is fome function, or perhaps a decent trick,
to do this conversion faster?

Thanks,

-- Pavils Jurjans



Mon, 18 Apr 2005 18:46:33 GMT  
 Efficient converting from byte-string to word-string
Hi
I think that the proper term for what you are getting is 'byte array'.  In other languages you can create and manipulate byte arrays
just like any other array.  Native VBScript has only seven or so 'B' versions of some string handling commands, such as lenB, leftB,
to extract info from byte arrays, but nothing for creating, modifying, or accessing individual elements of the array as in other
languages, like bytarr(n).  To verify that you are working with a byte array, use the typename function; it will return a value of
Byte() for byte array, or use the vartype function, which will return a value of 8209, which is 8192 (vbArray) + 17 (vbByte).

If you really are working with a byte array:
I had a long discussion with Michael Harris and others in this newsgroup on byte arrays recently.  See the thread titled: Problem
using CStr Function with byte array element, started on Oct 26, 2002 at 15:35.  Yesterday I added to that thread that you can use
the ADO stream object to convert from text to byte array formats quite easily, if you use the proper stream.charset property, and
correct sequence for adding text and then setting the stream.position to zero before changing the stream.type to binary.  I think in
this case, you just want to do the opposite -- Set the stream.charset to x-ansi, stream.write your byte array to the stream, set the
stream.pointer to 0, change the stream.type to character, then stream.readall the info into a string.

Hope this helps.  Post your code if it doesn't work and maybe we can help you.  Post your code if it works so we all can learn from
it!

-Paul Randall

Quote:

> Hallo,

> I have an ASP string which reads Request.Request.BinaryRead(size). Although
> docs tell that it returns an array of bytes, that wrong. It returns a
> byte-string.

> All the rest of my ASP code is in JScript, and JScript doesn't have such
> variable type as string of bytes. All the strings user characters of 32 bit
> codes. Therefore, to user the Request.Request.BinaryRead(size) byte-string
> in JScript, I need to convert it to word-string

> This code works fine:

> bin = Request.BinaryRead(size)
> str = ""
> For I = 1 To LenB(bin)
>     str = str & Chr(AscB(MidB(bin, I, 1)))
> Next

> Alas, it is pretty slow and for larger request contents, unforgivably slow.
> I would like to know if there is fome function, or perhaps a decent trick,
> to do this conversion faster?

> Thanks,

> -- Pavils Jurjans



Tue, 19 Apr 2005 00:11:24 GMT  
 Efficient converting from byte-string to word-string
Hi, Pavils
I had some time on my hands so I wrote a function to convert any byte array to a string.  The description of what to do, in my
previous post, was close, but not exactly correct.  The code below is a complete script that converts some character strings to byte
arrays and converts those byte arrays back to character strings and displays message boxes to show that it works for all ASCII
characters from chr(0) to chr(255).

You will probably only need the byte array to string routine.  Use it with code like:

bytArray = Request.Request.BinaryRead(size)
strYourString = ByteArrayToASCIIStringViaStreamOnly(bytArray, 1)

Let us know how it works.  I'm especially interested in how much it speeds things up for you.

<CODE>
'Here's two way to convert a GUID from formatted string format
' to a pGUID byte array. TypeName(pGUID) returns "Byte()"
' and Vartype(pGUID) returns 8209, or byte + array.
'The routines can also return a byte array from any character string.
'Also the inverse - byte array to any string or Hex nibble string.

'Typical Microsoft GUID string:
strGUIDOriginal = "{EEF00083-8597-4c2c-9ACB-FC860480FC5D}"

'Remove all characters not in the '0' to '9' and 'A' to 'F' range.
with new regexp
 .pattern = "[\{\}-]"
 .global = true
 strGUID = .replace(strGUIDOriginal,"")
end with

bytAry1 = AnyASCIIStringToByteArrayViaStreamOnly(strGUID, 2)
bytAry2 = AnyASCIIStringToByteArrayViaStreamAndFile(strGUID, 2)

'Test the opposite
strTemp = ByteArrayToASCIIStringViaStreamOnly(bytAry1, 2)

'Show that all methods return the correct byte array or string.
strMsg = "typename(bytAry1): " & typename(bytAry1) & ";    " & _
 "vartype(bytAry1): " & vartype(bytAry1) & vbcrlf & _
 "typename(bytAry2): " & typename(bytAry2) & ";    " & _
 "vartype(bytAry2): " & vartype(bytAry2) & vbcrlf & _
 "Original GUID:" & vbtab & vbtab & strGUIDOriginal & vbcrlf & _
 "Hex Nibble Only string:" & vbtab & strGUID & vbcrlf & _
 "Byte Array back to Strimg:" & vbtab & strTemp & vbcrlf & _
 "Hex of byte array method 1:" & vbtab

for i = 1 to Ubound(bytAry1) + 1
 strMsg = strMsg & right("00" & hex(ascB(midB(bytAry1, i, 1))), 2)
next

strMsg = strMsg & vbcrlf & "Hex of byte array method 2:" & vbtab
for i = 1 to Ubound(bytAry2) + 1
 strMsg = strMsg & right("00" & hex(ascB(midB(bytAry2, i, 1))), 2)
next
msgbox strMsg

'Demonstrate that any character in the range Chr(0) to Chr(255) can
' also be converted to a byte array.

for i = 0 to 255 step 32
 strTemp = ""
 for j = i to i + 31
  strTemp = strTemp & chr(j)
 next

 strMsg = "Chr(x)" & vbtab & _
  "Stream Only" & vbtab & "Stream/File" & vbcrlf

 bytAry1 = AnyASCIIStringToByteArrayViaStreamOnly(strTemp, 1)
 bytAry2 = AnyASCIIStringToByteArrayViaStreamAndFile(strTemp, 1)

'Check for invalid byte-array values, and display a message showing
' all are OK.
 for j = 1 to 32
  if (ascB(midB(bytAry2, j, 1)) <> ascB(midB(bytAry1, j, 1))) or _
   (ascB(midB(bytAry2, j, 1)) <> i + j - 1) then
   msgbox "ERROR: One or both methods has returned a bad value" _
    & vbcrlf & i + j - 1 & vbtab & _
    ascB(midB(bytAry1, j, 1)) & vbtab & _
    ascB(midB(bytAry2, j, 1)) & vbcrlf & "Quitting"
   wscript.quit
  end if
  strMsg = strMsg & i + j - 1 & vbtab & _
   vbtab & ascB(midB(bytAry1, j, 1)) & _
   vbtab & vbtab & ascB(midB(bytAry2, j, 1)) & vbcrlf
 next
 msgbox strMsg

'Test the inverse - byte array to string function.

 strTemp = ByteArrayToASCIIStringViaStreamOnly(bytAry1, 1)

 strMsg = "Chr(x)" & vbtab & "Stream Only" & vbcrlf
 for j = 1 to 32
  if asc(mid(strTemp, j, 1)) <> (i + j - 1) then
   msgbox "ERROR: The conversion has returned a bad value" _
    & vbcrlf & i + j - 1 & vbtab & _
    asc(mid(strTemp, j, 1)) & vbcrlf & "Quitting"
   wscript.quit
  end if
  strMsg = strMsg & i + j - 1 & vbtab & _
   vbtab & asc(mid(strTemp, j, 1)) & vbcrlf
 next
 msgbox strMsg

next

wscript.quit

'**********************************************
'Byte Array to String using ADO stream only.
'The routine can produce two types of output strings:
'iType=1
' Input is string contains any ASCII characters from Chr(0)
'  to Chr(255).
' Each element of the byte array becomes a character in the string.
'iType=2
' String contains only Hex ASCII characters from
' '0' to '9', and 'A' to 'F', which represent a 4-bit nibble
' in the range 0 to 15.  Each element of the byte array is split
' into a pairs of these nibble characers and the whole thing is
' returned as a string.

Function ByteArrayToASCIIStringViaStreamOnly(bytInput, iType)
' Paul Randall, October, 2002

Dim strHexNibbles() 'Used for iType=2

'Create an ADO stream object.
'Set the stream type to 1; this will allow writing the data to the
' stream in byte/binary mode by passing it a byte array.
'BUT - change the characer set to x-ansi, also known as windows-1252
' so that all 256 character codes are unchanged when read back in
' text mode.  Many other character sets do strange transformations.

set stream = createobject("adodb.stream")
stream.type = 1
stream.open

stream.write bytInput

'Change the stream type to character, so we can read a text string,
' and set the stream position to zero so we can read all the
' characters.
'Note that the stream position must be zero before the stream type
' OR charset is changed, or an error occurs.  Contents
' are not lost when changing stream.type.

stream.position = 0
stream.type = 2
stream.charset = "x-ansi"

'Note: with no number of bytes specified, stream.readtext reads all.
strTemp = stream.readtext

'Depending on the iType value,
' just return the string or
' convert each byte to two hex nibbles, and return the joined set.

if iType = 1 then
 ByteArrayToASCIIStringViaStreamOnly = strTemp
elseif iType = 2 then
 redim strHexNibbles(len(strTemp))
 for n = 1 to len(strTemp)
  strHexNibbles(n) = right("00" & hex(asc(mid(strTemp, n, 1))), 2)
 next
 ByteArrayToASCIIStringViaStreamOnly = join(strHexNibbles, "")
else
 msgbox "The second parameter to " & _
  "Function ByteArrayToASCIIStringViaStreamOnly(strInput, iType)" _
  & vbcrlf & "must be either 1 for any char, or 2 for Hex chars." _
  & vbcrlf & "You passed: " & iType & vbcrlf & "Quitting"
 wscript.quit
end if

stream.close
set stream = nothing

end function

'**********************************************
'String to Byte Array using ADO stream only.
'The routine can handle two types of input strings:
'iType=1
' Input is string contains any ASCII characters from Chr(0)
'  to Chr(255).
' Each character is returned in a separate element of the byte array.
'iType=2
' String contains only Hex ASCII characters from
' '0' to '9', and 'A' to 'F', which represent a 4-bit nibble
' in the range 0 to 15.  Pairs of these nibbles are converted
' to a single 8-bit byte in the range of 0 to 255, and these are
' returned in the elements of the byte array.

Function AnyASCIIStringToByteArrayViaStreamOnly(strInput, iType)
' Paul Randall, October, 2002

'Create an ADO stream object.
'Set the stream type to 2; this will allow writing the data to the
' stream in text mode by passing it plain ASCII strings.
'BUT - change the characer set to x-ansi, also known as windows-1252
' so that all 256 character codes are unchanged when read back in
' binary mode.  Many other character sets do strange transformations.

set stream = createobject("adodb.stream")
stream.type = 2
stream.charset = "x-ansi"
stream.open

'Depending on the iType value, write the string or byte-equivalents of
' the combined nibble-pairs to the stream, in text mode.
if iType = 1 then
 stream.writetext strInput, 0
elseif iType = 2 then
 if (len(strInput) mod 2) <> 0 then
  msgbox "The length of the hex nibble-string is not " & _
   "an even number." & vbcrlf & _
   "Don't know what to do with the last nibble." & vbcrlf & _
   "Quitting."
  wscript.quit
 end if
 for n = 1 to len(strInput) -1 step 2
  stream.writetext chr("&h" & mid(strInput,n,2)), 0
 next
else
 msgbox "The second parameter to " & _
  "Function AnyASCIIStringToByteArrayViaStreamOnly(strInput, iType)" _
  & vbcrlf & "must be either 1 for any char, or 2 for Hex chars." _
  & vbcrlf & "You passed: " & iType & vbcrlf & "Quitting"
 wscript.quit
end if

'Change the stream type to binary, so we can read a byte-array,
' and set the stream position to zero so we can read all the bytes.
'Note that the stream position must be zero before the stream type
' is changed, or an error occurs on changing stream type.  Contents
' are not lost when changing stream.type.
stream.position = 0
stream.type = 1

AnyASCIIStringToByteArrayViaStreamOnly = stream.read

stream.close
set stream = nothing

end function

'**********************************************
'String to Byte Array using ADO stream and text file.
'The routine can handle two types of input strings:
'iType=1
' Input is string contains any ASCII characters from Chr(0)
'  to Chr(255).
' Each character is returned in a separate element of the byte array.
'iType=2
' String contains only Hex ASCII characters from
' '0' to '9', and 'A' to 'F', which represent a 4-bit nibble
' in the range 0 to 15.  Pairs of these nibbles are converted
' to a single 8-bit byte in the range of 0 to 255, and these are
' returned in the elements of the byte array.

Function AnyASCIIStringToByteArrayViaStreamAndFile(strInput, iType)

'Create an ADO stream object and a temporary file in the
' current folder.
set fso = createobject("scripting.filesystemobject")
set stream = createobject("adodb.stream")
temp = fso.gettempname()
set ts = fso.createtextfile(temp)

'Depending on the iType value, write the string or byte-equivalents of
' the combined nibble-pairs to the temporary file, in text mode.
if iType = 1 then
 ts.write strInput
elseif iType = 2 then
 if (len(strInput) mod 2) <> 0 then
  msgbox "The length of the hex nibble-string is not " & _
   "an even number." & vbcrlf & _
   "Don't know what to do with the last nibble." & vbcrlf & _
   "Quitting."
  wscript.quit
 end if
 for n = 1 to len(strInput) -1 step 2
  ts.write chr("&h" & mid(strInput,n,2))
 next
else
...

read more »



Tue, 19 Apr 2005 02:35:38 GMT  
 Efficient converting from byte-string to word-string
Ok, Paul, here are my results:

First, until I received your reply, I was able to push down processing time
by splitting the string in smaller chunks and then joining the chunks
together.

Here are all the versions:

Version #1: Slow and inefficient. But works ;)

function getBinaryRead()
 'Returns a string of bytes from Request.BinaryRead
 'This is a workaround due to JScript not being able to access
Request.BinaryRead directly
 Dim size, bin, str, I
 size = Request.TotalBytes
 If size = 0 Then Exit Function
 bin = Request.BinaryRead(size)
 str = ""
 For I = 1 To size
  str = str & Chr(AscB(MidB(bin, I, 1)))
 Next
 getBinaryRead = str
end function

Version #2: Much better than version 1.

function getBinaryRead()
 'Returns a string of bytes from Request.BinaryRead
 'This is a workaround due to JScript not being able to access
Request.BinaryRead directly
 Dim size, bin, strPart, partSize, strArr(), I
 size = Request.TotalBytes
 If size = 0 Then Exit Function
 bin = Request.BinaryRead(size)
 partSize = 700
 redim strArr(int(size/partSize) + 1)
 strPart = -1
 For I = 1 To size
  If (I-1) mod partSize = 0 Then strPart = strPart + 1
  strArr(strPart) = strArr(strPart) & Chr(AscB(MidB(bin, I, 1)))
 Next
 getBinaryRead = join(strArr, "")
end function

Version #3: Using adodb.stream object

function getBinaryRead()
 'Returns a string of bytes from Request.BinaryRead
 'This is a workaround due to JScript not being able to access
Request.BinaryRead directly
 Dim size, bin

 size = Request.TotalBytes
 If size = 0 Then Exit Function
 bin = Request.BinaryRead(size)

 set stream = createobject("adodb.stream")
 stream.type = 1
 stream.open

 stream.write bin

 stream.position = 0
 stream.type = 2
 stream.charset = "x-ansi"

 getBinaryRead = stream.readtext

end function

My testing method is very machine-dependent. I have a form with single
<input type="file"> entry, where I submit a text file of size 100 Kb (102400
bytes). With this content, the size of binary read is equal to 102634 bytes.
I measure time what was taken to read the request iteslf [ bin =
Request.BinaryRead(size) ], and then time what was needed to convert it to
normal string. The first time appears to be insignifficantly small, and
currently I don't have opportunity to test if it is larger on slow
connections like modem.

Results are:
Version #1: 67 seconds
Version #2: 7 seconds (not bad huh)
Version #3: about 0 seconds (Tada!)

The first version stinks because the longer is the string, the slower are
maipulations with it. For version 2, I found that the optimal chunk size is
700 by simple trial-error testing. The approach suggested by you is very
innovative, and, no doubt, the best I currently have.

I am on my way to finish a pure code file upload solution in JScript.
Amazing, isn't it, I found no evidence on the net of someone trying to do
this. There are many components for this, some for free, and some for good
money, but once you have code, you can add your own features, adjust
compatibility, etc. Soon I'd put it online and welcome people to test on
different OS/Browser combinations. Mac is dark area for me, and I hope
someone to clear that up.

Regards,

--
-Pavils Jurjans
Information Technology consultations and solutions
Phone: (+371) 9459777
ICQ: 4047612



Tue, 19 Apr 2005 16:48:32 GMT  
 Efficient converting from byte-string to word-string
Oh boy another problem looms here:

Please see the code here:

http://www.jurjans.lv/dhtml/FileUpload.zip

Note that you have to change the path in line 250 of upload.asp, and set it
to some place where ASP is authorised to create a file.

In line 204 you can choose version of getBinaryRead() to go with.

Now, the problem is just between the version 2 and version 3:

All other things equal, it works in case 2, but I get this error in case 3:

Microsoft JScript runtime error '800a0005':Invalid procedure call or
argument

It's crazy. If you' ll check the code, you'll see that in both cases the
variable this.body looks to JScript equally the same -- of type " string",
equal length, equal first and last character codes. But one is allowed to be
written on text file, the other not.

If you'd have some ideas, I'd welcome them. I am now puzzled.

Regards,

--
-Pavils Jurjans
Information Technology consultations and solutions
Phone: (+371) 9459777
ICQ: 4047612



Tue, 19 Apr 2005 17:27:45 GMT  
 Efficient converting from byte-string to word-string
Ok,

I've discovered the root of problem... but solution is still pending:

in my test text file there are characters with codes beyond 0x7F, such as
0xe2 and 0xfe. How they are read depends on the line " stream.charset = ...
". Now, there is problem with that, if I use "x-ansi", it leaves the byte
codes as they are. Now what fails is the line "myFile.write(this.body);",
when it doesn't know what to do with these codes. Obviously is expects the
this.body to be in some code page.

Obviously I need some way how to write my string which may contain
characters ranging from code 0x00 to 0xFF in file without any conversions:

a = "\0x00\0x81\0xFF";

// How do I put this string in a file of size 2 bytes?

But I think I'll repost this question in JScript or ASP newsgroups, where it
would be more appropriate.

-- Pavils


Quote:
> Oh boy another problem looms here:

> Please see the code here:

> http://www.jurjans.lv/dhtml/FileUpload.zip

> Note that you have to change the path in line 250 of upload.asp, and set
it
> to some place where ASP is authorised to create a file.

> In line 204 you can choose version of getBinaryRead() to go with.

> Now, the problem is just between the version 2 and version 3:

> All other things equal, it works in case 2, but I get this error in case
3:

> Microsoft JScript runtime error '800a0005':Invalid procedure call or
> argument

> It's crazy. If you' ll check the code, you'll see that in both cases the
> variable this.body looks to JScript equally the same -- of type " string",
> equal length, equal first and last character codes. But one is allowed to
be
> written on text file, the other not.

> If you'd have some ideas, I'd welcome them. I am now puzzled.

> Regards,

> --
> -Pavils Jurjans
> Information Technology consultations and solutions
> Phone: (+371) 9459777
> ICQ: 4047612



Tue, 19 Apr 2005 18:36:33 GMT  
 Efficient converting from byte-string to word-string
Hi, Pavils
I'm glad to hear that the stream solution greatly increased the speed.  I thought it would, but haven't tried it in a 'real world'
situation.

My knowledge of ASP and JScript is too small to be of any help to you.  My knowledge of the ASO stream is kind of small too - I just
accidently figured out how to do the character/byte-array conversions.

But, looking at your code:
 var myFile = fs.createTextFile(physicalPath);
 myFile.write(this.body);

If physicalPath already exists, I'm not sure that you will be able to write to it.  Do you need to use the overwrite option when
creating myFile?  Does this.body contain the identical string for methods 2 & 3 of obtaining binTxt?  If myFile.write can't write a
binary string to a file (perhaps it quits when a chr(0) byte is written?), then perhaps you should also use the stream object for
writing the file.  The stream object has a lot of handy methods and properties.

Stream.pointer = 0
Stream.write this.body
...
Stream.SaveToFile FileName, SaveOptions

I hope this helps.

-Paul Randall

Quote:

> Ok,

> I've discovered the root of problem... but solution is still pending:

> in my test text file there are characters with codes beyond 0x7F, such as
> 0xe2 and 0xfe. How they are read depends on the line " stream.charset = ...
> ". Now, there is problem with that, if I use "x-ansi", it leaves the byte
> codes as they are. Now what fails is the line "myFile.write(this.body);",
> when it doesn't know what to do with these codes. Obviously is expects the
> this.body to be in some code page.

> Obviously I need some way how to write my string which may contain
> characters ranging from code 0x00 to 0xFF in file without any conversions:

> a = "\0x00\0x81\0xFF";

> // How do I put this string in a file of size 2 bytes?

> But I think I'll repost this question in JScript or ASP newsgroups, where it
> would be more appropriate.

> -- Pavils



> > Oh boy another problem looms here:

> > Please see the code here:

> > http://www.jurjans.lv/dhtml/FileUpload.zip

> > Note that you have to change the path in line 250 of upload.asp, and set
> it
> > to some place where ASP is authorised to create a file.

> > In line 204 you can choose version of getBinaryRead() to go with.

> > Now, the problem is just between the version 2 and version 3:

> > All other things equal, it works in case 2, but I get this error in case
> 3:

> > Microsoft JScript runtime error '800a0005':Invalid procedure call or
> > argument

> > It's crazy. If you' ll check the code, you'll see that in both cases the
> > variable this.body looks to JScript equally the same -- of type " string",
> > equal length, equal first and last character codes. But one is allowed to
> be
> > written on text file, the other not.

> > If you'd have some ideas, I'd welcome them. I am now puzzled.

> > Regards,

> > --
> > -Pavils Jurjans
> > Information Technology consultations and solutions
> > Phone: (+371) 9459777
> > ICQ: 4047612



Tue, 19 Apr 2005 23:55:13 GMT  
 Efficient converting from byte-string to word-string
Yap, I tried to user the stream object for this purpose, but it grives the
same error. There is some strnage behaviour that some character codes (ant
it's not 0x00 what gives trouble), all of them are abouve 0x7F cause the
same error. I could guess that file.write internally uses the ado stream
object, maybe...

You can check out the thread I posted in jscript group: Writing text file:
some characters are not allowed?

Meanwhile, I happened to find out that problem is solved, when I do

stream.charset = "windows-1257" instead of "x-ansi"

The flow goes like this:

1. Byte array is assigned to stream
2. Text is read from stream, *converting* some characters to unicode chars
3. Later, when saving the text to file, there are those unicode characters
4. file.write *internally* converts the unicode characters to corresponding
ASCII codes, which happen to be the same codes which were converted to
unicode beyond-255 codes in step 2.

Because of file.write behaviour in point 4 I decided that it uses server
charset to convert the text string, so I used that instead of "x-ansi", and
it seems to work Ok.

Regards,

-- Pavils


Quote:
> Hi, Pavils
> I'm glad to hear that the stream solution greatly increased the speed.  I

thought it would, but haven't tried it in a 'real world'
Quote:
> situation.

> My knowledge of ASP and JScript is too small to be of any help to you.  My

knowledge of the ASO stream is kind of small too - I just
Quote:
> accidently figured out how to do the character/byte-array conversions.

> But, looking at your code:
>  var myFile = fs.createTextFile(physicalPath);
>  myFile.write(this.body);

> If physicalPath already exists, I'm not sure that you will be able to

write to it.  Do you need to use the overwrite option when
Quote:
> creating myFile?  Does this.body contain the identical string for methods

2 & 3 of obtaining binTxt?  If myFile.write can't write a
Quote:
> binary string to a file (perhaps it quits when a chr(0) byte is written?),

then perhaps you should also use the stream object for
Quote:
> writing the file.  The stream object has a lot of handy methods and
properties.

> Stream.pointer = 0
> Stream.write this.body
> ...
> Stream.SaveToFile FileName, SaveOptions

> I hope this helps.

> -Paul Randall




Quote:
> > Ok,

> > I've discovered the root of problem... but solution is still pending:

> > in my test text file there are characters with codes beyond 0x7F, such
as
> > 0xe2 and 0xfe. How they are read depends on the line " stream.charset =
...
> > ". Now, there is problem with that, if I use "x-ansi", it leaves the
byte
> > codes as they are. Now what fails is the line

"myFile.write(this.body);",
Quote:
> > when it doesn't know what to do with these codes. Obviously is expects
the
> > this.body to be in some code page.

> > Obviously I need some way how to write my string which may contain
> > characters ranging from code 0x00 to 0xFF in file without any
conversions:

> > a = "\0x00\0x81\0xFF";

> > // How do I put this string in a file of size 2 bytes?

> > But I think I'll repost this question in JScript or ASP newsgroups,
where it
> > would be more appropriate.

> > -- Pavils



> > > Oh boy another problem looms here:

> > > Please see the code here:

> > > http://www.jurjans.lv/dhtml/FileUpload.zip

> > > Note that you have to change the path in line 250 of upload.asp, and
set
> > it
> > > to some place where ASP is authorised to create a file.

> > > In line 204 you can choose version of getBinaryRead() to go with.

> > > Now, the problem is just between the version 2 and version 3:

> > > All other things equal, it works in case 2, but I get this error in
case
> > 3:

> > > Microsoft JScript runtime error '800a0005':Invalid procedure call or
> > > argument

> > > It's crazy. If you' ll check the code, you'll see that in both cases
the
> > > variable this.body looks to JScript equally the same -- of type "
string",
> > > equal length, equal first and last character codes. But one is allowed
to
> > be
> > > written on text file, the other not.

> > > If you'd have some ideas, I'd welcome them. I am now puzzled.

> > > Regards,

> > > --
> > > -Pavils Jurjans
> > > Information Technology consultations and solutions
> > > Phone: (+371) 9459777
> > > ICQ: 4047612



Wed, 20 Apr 2005 00:08:51 GMT  
 Efficient converting from byte-string to word-string
Hi, Pavils.
    BinaryRead really returns binary data (VT_ARRAY | VT_UI1) - not a byte
string (BSTR containing bytes). But multibyte string functions (MidB, LenB,
InstrB, ...) accepts binary data also.

    To convert the data:
    1. Use MultiByteToWideChar Win32 function. Do you have C++/VBA to do a
simple object converting data?

    2. See ByteArray class (not free) at
http://pstruh.cz/help/scptutl/cl46.htm. The class was designed to work with
bytearray, mutibyte, unicode and hex strings (including UTF) in more than
120 code pages.
For example, to convert source data stream from windows-1250:

Dim ByteArray, String
Set ByteArray = CreateObject("ScriptUtils.ByteArray")
ByteArray.ByteArray = Request.BinaryRead(size)
ByteArray.CharSet = "windows-1250"
String = ByteArray.String

    3. See "Convert a binary data (BinaryRead) to a string by VBS" article
at http://www.pstruh.cz/tips/detpg_binarytostring.htm. There are several
optimalizations of your VBS code to convert data between binary -> string
and multibyte -> binary.

    Have a nice day
    Antonin Foller
    PSTRUH Software
    http://www.pstruh.cz


Quote:
> Hallo,

> I have an ASP string which reads Request.Request.BinaryRead(size).
Although
> docs tell that it returns an array of bytes, that wrong. It returns a
> byte-string.

> All the rest of my ASP code is in JScript, and JScript doesn't have such
> variable type as string of bytes. All the strings user characters of 32
bit
> codes. Therefore, to user the Request.Request.BinaryRead(size) byte-string
> in JScript, I need to convert it to word-string

> This code works fine:

> bin = Request.BinaryRead(size)
> str = ""
> For I = 1 To LenB(bin)
>     str = str & Chr(AscB(MidB(bin, I, 1)))
> Next

> Alas, it is pretty slow and for larger request contents, unforgivably
slow.
> I would like to know if there is fome function, or perhaps a decent trick,
> to do this conversion faster?

> Thanks,

> -- Pavils Jurjans



Thu, 21 Apr 2005 05:56:05 GMT  
 Efficient converting from byte-string to word-string
Hallo Antonin,

What you say is not entirely true. There are some workarounds, and, as Paul
showed in this thread, very handy ones. I believe ADODB.Recordset uses
ADODB.Stream in its guts (just my assumption), so using ADODB.Stream may be
more efficient. I tried all of your three methods but all of them do some
character encoding with bytecodes above 0x7F. The use of ADODB.Stream
enables to choose charset, so the character encoding effect is controllable.

I was able to make pure JScript file upload solution. You can see it here. I
is not very featured, of course, byt open code enables developers to add
whatever features they'd like:

http://www.jurjans.lv/asp/FileUpload.zip

Regards,

-Pavils Jurjans
Information Technology consultations and solutions
Phone: (+371) 9459777
ICQ: 4047612


Quote:
> Hi, Pavils.
>     BinaryRead really returns binary data (VT_ARRAY | VT_UI1) - not a byte
> string (BSTR containing bytes). But multibyte string functions (MidB,
LenB,
> InstrB, ...) accepts binary data also.

>     To convert the data:
>     1. Use MultiByteToWideChar Win32 function. Do you have C++/VBA to do a
> simple object converting data?

>     2. See ByteArray class (not free) at
> http://pstruh.cz/help/scptutl/cl46.htm. The class was designed to work
with
> bytearray, mutibyte, unicode and hex strings (including UTF) in more than
> 120 code pages.
> For example, to convert source data stream from windows-1250:

> Dim ByteArray, String
> Set ByteArray = CreateObject("ScriptUtils.ByteArray")
> ByteArray.ByteArray = Request.BinaryRead(size)
> ByteArray.CharSet = "windows-1250"
> String = ByteArray.String

>     3. See "Convert a binary data (BinaryRead) to a string by VBS" article
> at http://www.pstruh.cz/tips/detpg_binarytostring.htm. There are several
> optimalizations of your VBS code to convert data between binary -> string
> and multibyte -> binary.

>     Have a nice day
>     Antonin Foller
>     PSTRUH Software
>     http://www.pstruh.cz



> > Hallo,

> > I have an ASP string which reads Request.Request.BinaryRead(size).
> Although
> > docs tell that it returns an array of bytes, that wrong. It returns a
> > byte-string.

> > All the rest of my ASP code is in JScript, and JScript doesn't have such
> > variable type as string of bytes. All the strings user characters of 32
> bit
> > codes. Therefore, to user the Request.Request.BinaryRead(size)
byte-string
> > in JScript, I need to convert it to word-string

> > This code works fine:

> > bin = Request.BinaryRead(size)
> > str = ""
> > For I = 1 To LenB(bin)
> >     str = str & Chr(AscB(MidB(bin, I, 1)))
> > Next

> > Alas, it is pretty slow and for larger request contents, unforgivably
> slow.
> > I would like to know if there is fome function, or perhaps a decent
trick,
> > to do this conversion faster?

> > Thanks,

> > -- Pavils Jurjans



Fri, 22 Apr 2005 19:02:32 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. How to convert extra long strings into their equivalent Hex Strings in VBA (Word 2K)

2. convert byte array to string

3. convert string to array of bytes

4. Converting from string to array of bytes

5. Convert integers and strings to byte array

6. Convert Byte Array to a String

7. Converting String to Byte ...

8. Converting between String and Byte Array

9. convert Byte array to string and vice veras

10. Convert Strings to Byte arrays and back

11. Convert String to Byte

12. Convert byte() or Binary Stream to base64 string

 

 
Powered by phpBB® Forum Software