CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    May 2002
    Posts
    1,798

    How to set low and high bytes of a wchar_t ?

    I have a method of converting a wide char array to an unsigned char array. Now I need to be able to convert back (from unsigned char array to wide char array) but cannot figure out how to do that.

    Here's my wc2uc method which uses built in macros (BTW, where do these come from? Are they unique to Windows API or are they part of C standard ?):
    Code:
    int wcstoucs(wchar_t wcs[], int nsz, unsigned char uc[] )
    {
    	//uc = new unsigned char [ 2 * nsz + 1 ];
    	//memset(uc, 0x00, 2 * nsz + 1);
    	wchar_t wch = ' ';
    	int wdx = 0;
    	for(size_t i = 0; i < 2 * nsz; i+=2)
    	{
    		wch = wcs[wdx];
    		//pb[i] = LOBYTE(wch);      // bigEndian
    		//pb[i+1] = HIBYTE(wch);
    		uc[i] = HIBYTE(wch);		// littleEndian (x86)
    		uc[i+1] = LOBYTE(wch);
    		wdx++;
    	}
    	return 2 * nsz;
    
    }// wcstoucs(wchar_t wcs[], int nsz, unsigned char uc[] )
    And here's a method that only depends upon what I am certain is native 'C':
    Code:
    	// given a wchar_t get the low and the hi order bytes
    	wchar_t wch;
    	byte lobyte, hibyte;
    
    	wch = 0xABCD;
    	lobyte  = wch &0xff;
    	hibyte = wch >> 8;
    
    	printf("wch = %0.4X\n", wch);     // ABCD
    	printf("lobyte =: %0.2X\n", lobyte);   // CD
    	printf("hibyte =: %0.2X\n", hibyte);   // AB
    But we run into an lvalue problem if we try to inverse the operation:
    Code:
    	wchar_t wch = 0x0000;
    	unsigned char ucb = 0x94;
    	// set the low byte
    	LOBYTE(wch) = ucb;  // Error: Expression must be a modifiable lvalue
    So how to set the hi and lo bytes of a wchar_t ?
    Last edited by Mike Pliam; June 24th, 2013 at 01:28 AM.
    mpliam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured