Solved

Chr, ChrB, Asc, AscB, Instr, InstrB

Posted on 2004-04-02
6
2,394 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
  • 3
6 Comments
 
LVL 6

Accepted Solution

by:
PhilAI earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hope that helps.


-Brian
0
 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
Thanks Bingie

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

-Brian
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now