Win32 Programming Lesson 6: Everything you never wanted to know about strings.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Chapter 10.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
CENG 311 Machine Representation/Numbers
Homework Reading Programming Assignments
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
UNICODE Character Sets and Coding Standards Han Unification and ISO10646 Encoding Evolution and Unicode Programming Unicode.
Introduction to C programming
C Tokens Identifiers Keywords Constants Operators Special symbols.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
CPS120: Introduction to Computer Science
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
Other data types. Standard type sizes b Most machines store integers and reals in 4 bytes (32 bits) b Integers run from -2,147,483,648 to 2,147,483,647.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
C LANGUAGE Characteristics of C · Small size
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
CPS120: Introduction to Computer Science Variables and Constants.
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Characters and Strings
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Object Oriented Programming Lecture 2: BallWorld.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Computer Organization and Design Pointers, Arrays and Strings in C
A bit of C programming Lecture 3 Uli Raich.
Chapter 6: Data Types Lectures # 10.
Data Types, Arithmetic Operations
Programming Languages and Paradigms
Chapter 13 - The Preprocessor
C Short Overview Lembit Jürimägi.
Programming Paradigms
Chapter 3: Understanding C# Language Fundamentals
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Object Oriented Programming COP3330 / CGS5409
C Stuff CS 2308.
Introduction to Abstract Data Types
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
File Input and Output.
Homework Reading Programming Assignments Finish K&R Chapter 1
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Homework Continue with K&R Chapter 5 Skipping sections for now
Primitive Types and Expressions
Varying Character Lengths
Presentation transcript:

Win32 Programming Lesson 6: Everything you never wanted to know about strings

Before We Begin  Several of you probably had problems with character types in the last assignment, especially when reading the command line  Why? Because in Windows, strings aren’t always strings (if that makes sense)

Why?  Traditionally, a C string is a sequence of bytes, terminated by a NULL  Unfortunately, this only accommodates 256 different characters, and that’s too few for some languages (Kanji being the classic example)

DBCS  To fix this problem DBCS was created.  In a DBCS each character consists of 1 or 2 bytes  This means things like strlen don’t work correctly  Helper functions exist, but the solution is ugly  Enter UNICODE

WBCS  Wide Byte Character Set == Unicode  Consortium founded in 1988(!)  See for more information that you could ever wanthttp://  All characters are 16 bits in length

Why bother?  Enables easy data exchange between languages  Create a single binary that supports all languages  Improves execution efficiency

History  Unicode really is much more of a Windows 2000 thing…  Support in 98 was lacking  However, looking to the future, we’ll ignore the old 16-bit application space  Windows CE is Unicode only

Writing Unicode Code…  It’s possible to write pure Unicode applications using several new functions in the RTL  However, you can write code which is *both* very easily using macros

Unicode types  typedef unsigned short wchar_t;  Declared in string.h  wchar_t szBuffer[100] allocates 100 characters but not 100 bytes  Breaks strcat, strcpy etc.  Equivalent functions with wcs replacing str e.g. wcscat

A Better Way  tchar.h  Introduces a series of macros which allows the program to use Unicode or not, depending on compilation options  Creates a new TYPE TCHAR which is equivalent to a char if _UNICODE is not defined, and a wchar_t if it is

Problems  Imagine this: TCHAR *szError = “Error”; wchar_t *szError = “Error”; TCHAR *szError = L“Error”; TCHAR *szError = _TEXT(“Error”);

Windows Unicode data  WCHAR: Unicode character  PWSTR: Pointer to a Unicode string  PWCSTR: Pointer to a constant Unicode string

Windows API Revisited  CreateWindowEx doesn’t exist…  Really, is CreateWindowExA and CreateWindowExW  One is ASCII, the other is Unicode  Switched in WinUser.h depending on the definition of UNICODE

Unicode Gotchas  Use type BYTE and PBYTE to define bytes  Use generic type TCHAR etc.  Use the TEXT macro  Beware string arithmetic… don’t think about sizeof(szBuffer) as the number of characters you can hold! Similarly, think about malloc

Windows functions  Use lstrcat, lstrcmp, lstrcmpi, lstrcpy and lstrlen instead of wcs/str counterparts  Some use the Windows function CompareString  Useful for fancy language comparisons  There are a whole host of these functions (like CharLower and CharLowerBuff…)

Type Conversion  Of course, sometimes you have to convert from ASCII to Unicode in a program  Use MultiByteToWideChar to make Wide characters  Use WideCharToMultiByte to make regular characters

Your pwn DLLs  You can write your DLLs to provide both ASCII and Unicode support  For example, imagine a routine which reverses a string… BOOL StringReverseW(PWSTR pWideCharStr)  Instead of writing a completely separate function for StringReverseA… it should convert to WCS and then call StringReverseW and then convert back

Prototype  BOOL StringReverseW(PWSTR pWideCharStr); BOOL StringReverseA(PSTR pMultibyteStr); #ifdef UNICODE #define StringReverse StringReverseW #else #define StringReverse StringReverseA #endif

Not-too-difficult Assignment  Sort n words from the command line in ascending alphabetic order (unless the –d flag is set, in which case descending), and have your program compile and run easily with MBCS or UNICODE set

Next Class  Simple Kernel Objects…