Solved

Chr, ChrB, Asc, AscB, Instr, InstrB

Posted on 2004-04-02
6
2,472 Views
Last Modified: 2007-12-19
Can anyone show me how to use "Chr, ChrB, Asc, AscB, Instr, InstrB"

I knew chrB is for Byte operation. however, i don't know how to get the below result

I am confusing.

chrB(65) & chrB(65)=?                Why get ?
Asc(chrB(65) & chrB(65))=63      Why get 63
AscB(chrB(65) & chrB(65))=65    Why get 65


what is the difference between chrB() and chr() and Asc() and AscB()
how they operate?
0
Comment
Question by:smallee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 6

Accepted Solution

by:
PhilAI earned 20 total points
ID: 10739518
Byte operation is used primarily for converting strings in DBCS applications.

Short for double-byte character set, a character set that uses two-byte (16-bit) characters rather than one-byte (8-bit) characters. Some languages, such as Chinese, Japanese and Korean, have writing schemes with many different characters that cannot be represented with single-byte codes such as ASCII and EBCDIC. In a single-byte character set, the possible number of binary combinations is 256; the number increases to 65,536 in a double byte character set.

To be honest, don't know why you are worrying about it! It is rarely used, and the reason you get:

chrB(65) & chrB(65)=?

is because only a single byte is returned and therefore combining the two produce an invalid character. If you do, ChrB(67) & ChrB(66) & ChrB(65) = ?A

Asc(chrB(65) & chrB(65))=63

63 is the ASCII code of the question mark returned above

AscB(chrB(65) & chrB(65))=65

is because it is actually only passing the first byte ChrB(65), if you do AscB(ChrB(66) & ChrB(65)) = 66

Understanding this?
0
 
LVL 3

Author Comment

by:smallee
ID: 10739777
Thanks for your quick response!

But i still wonder something

Chr(65) & Chr(66)=AB

But don't it return two single byte character? Why don;t they combine to one character?

0
 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 30 total points
ID: 10739810
Because all strings are now represented internally in Unicode format, it is not as simple as it used to be to represent binary data in a string. Using the Chr function to assign data to a string does not result in the same behavior as before. For example:

   stringvar = Chr(65)
                        

results in a two-byte long string, where byte 1 has a value of 65 and byte 2 has a value of 0 (this is the Unicode representation of the letter "A"). Be sure to keep in mind that converting from ANSI to Unicode does not always entail just adding a second byte with a value of zero as it does in this case. For example, most of the ANSI character codes in the range 130-159 have completely different Unicode values. Try executing a 'Debug.Print AscW(Chr(130))' and you a value of 8218 is displayed.

Currently, Microsoft Windows requires a little endian processor, which means that in a multiple byte entity the first byte is the least significant, and significance increases in successive bytes. This explains why the Unicode character "A" is represented internally as the following:

   -------------------
   |   65   |    0   |
   -------------------
     byte 0     byte 1
                        

The AscB and ChrB functions can be used to replicate what used to be accomplished by the Asc and Chr functions, because these functions allow the manipulation of single byte quantities. If you would like a four-byte string that has the binary values of 65, 66, 67, and 68 consecutively then using the Chr function will not work. You must instead use the ChrB function. For example:

   stringvar = ChrB(65) & ChrB(66) & ChrB(67) & ChrB(68)
                        

Alternatively, you can use the ability to create arrays of the new byte data type and manipulate your binary data that way.

Listed below is an explanation of the results of some simple uses of these functions to further clarify this information.

Print Asc(Chr(255)) --> "255"

Nothing new here, except that the Chr function is returning a Unicode character that occupies two bytes instead of a one-byte ANSI character.

Print Asc(ChrB(255)) --> 5 - Invalid procedure call.

This usage returns an error because the Asc function always expects at least a two-byte parameter and the ChrB function is only returning a single byte.

Print Asc(Chr(256)) --> 5 - Invalid procedure call.

Although the Chr function returns a two-byte Unicode character, it still only takes numbers between 0 and 255 for its argument (note that on a DBCS enabled system, Asc/Chr handle two-byte DBCS characters, converting them to and from Unicode). Using the ChrW function allows access to the full 65,536 Unicode character locations.

Print AscW(ChrW(256)) --> "256"

This is the new version of the first statement in this section. The ChrW function takes a value from 0 to 65,536 and returns that character (on 32-bit systems). The AscW function interprets this two-byte character as a Unicode character and returns the correct Unicode value for that character.

Print Asc(ChrW(256)) --> "65"
Print Asc(ChrW(5000)) --> "63"

What is happening here is that the ChrW function is being evaluated first. ChrW(256) is the character "A", and so the function reduces to Asc("A"), and the Unicode (and ANSI) number for "A" is 65. Because Visual Basic does not know how to display the character represented by Chr(5000) it just displays a "?", and as expected, the Unicode and ANSI value for "?" is 63.

Print AscB(Chr(65)) --> "65"
Print AscB(ChrW(256)) --> "0"
Print AscB(ChrW(257)) --> "1"
Print AscB(ChrW(555)) --> "43"
Print AscB(ChrW(65535)) --> "255"

All of these return values can be explained by understanding how each character is represented internally (see the little-endian reference above) and by the fact that the AscB function looks only at the first byte of the character it receives. Visually it looks like the following diagram:

             -------------------
   Chr(65)   |   65   |    0   |
             -------------------
   Chr(256)  |    0   |    1   |
            -------------------
   Chr(257)  |    1   |    1   |
             -------------------
   Chr(555)  |   43   |    1   |
             -------------------
   Chr(65535)|   255  |  255   |
             -------------------
               byte 0    byte 1
                        

The AscB function just returns whatever the first byte of the character is.

Print ChrB(65) --> "" 

Visual Basic prints nothing for this call to the ChrB function because the ChrB function is only returning a one-byte string. One byte strings like this mean nothing to Visual Basic because they do not constitute a valid Unicode character (or series of characters).

Print ChrB(65) & ChrB(0) --> "A"

In this case, we are concatenating two one-byte strings into a single two-byte string. Because the resulting bit pattern is the same as the bit pattern for the Unicode "A", that is what Visual Basic prints.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 10739815
Hope that helps.


-Brian
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 10756834
Thanks Bingie

All I can take credit for is the process of using google to find the answer :)

-Brian
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question