login about faq

Hi

When I used SendBytes to send some information based on ASCII codes it sent two bytes per character but I expected only one byte. So I changed the command to SendString and set the Character set to Windows-1252 and then only one byte per character was used. Can SendBytes be used to send data using one byte per character?

Thanks, Simon

asked Feb 02 '15 at 10:04

swapna's gravatar image

swapna
1113


My guess is that you are using the ActiveX and passing a string variable to SendBytes. What happens is that in any language using ActiveX, strings are passed as utf-16 (in something called a BSTR, which is "COM" terminology). What really happens is that the COM layer/functionality of your programming language is passing a Variant containing a BSTR to SendBytes. SendBytes always sends exactly the bytes you pass to it, and therefore it is sending the exact bytes of the BSTR.

What you should do instead is call SendString. This method pays attention to the StringCharset property, which you may set to "windows-1252". This allows for any programming language to call SendString and with the StringCharset property set to "windows-1252", you are assured that the string is sent using the Windows-1252 byte representation (1-byte per char). Some programming languages might pass strings using utf-16, others utf-8, and still others ANSI, but you don't have to worry because if StringCharset is "windows-1252", then whatever conversion is required (such as utf-8 to Windows-1252, or utf-16 to Windows-1252), it will happen automatically.

The semantics of SendBytes is that you are sending raw bytes, which are not necessarily bytes representing characters. The bytes could be, for example, the bytes of a JPG image. It wouldn't make sense for Chilkat to try to interpret the bytes passed to SendBytes as text characters.

link

answered Feb 02 '15 at 10:59

chilkat's gravatar image

chilkat ♦♦
11.8k316358420

edited Feb 02 '15 at 11:00

Thank-you for your reply. I had forgotten about the BSTR in COM which I am indeed using so your explanation makes perfect sense. As I mentioned earlier I did switch to SendString and everything worked fine. I was just trying to understand the issue and once you mentioned BSTR I realized what I had happened.

(Feb 02 '15 at 11:19) swapna
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×5

Asked: Feb 02 '15 at 10:04

Seen: 775 times

Last updated: Feb 02 '15 at 11:20

powered by OSQA