• C

Changing from BYTE[] to char *

Hello,
I have a byte array of length 248.. that holds a unicode string.. How can I cast it to char * ?
LVL 6
nehayaAsked:
Who is Participating?
 
AxterCommented:
>>if i have a  string of (e.g)  10 chars, where each char is represented in 2 bytes.. the total will be 20 bytes.. right?

Not if it's UTF-8.  With UTF-8, ANSI characters are represented with 1 byte characters, and non-ANSI characters are represented with two bytes.


>>How can I re-represent the byte array in a char *..? ( note the result char * length will be 10 chars long! not 20 )
You can not, unless the non-ANSI characters are local characters.

If the non-ANSI characters are local characters, then it's possible to convert them to single byte characters.
Otherwise, there is no way for you to represent the non-ANSI character in a single byte.
0
 
AxterCommented:
Use wcstombs
0
 
AxterCommented:
If you're using Windows, you can use WideCharToMultiByte instead, which is a Win32 API
0
IT Degree with Certifications Included

Aspire to become a network administrator, network security analyst, or computer and information systems manager? Make the most of your experience as an IT professional by earning your B.S. in Network Operations and Security.

 
AxterCommented:
Example

/* WCSTOMBS.C illustrates the behavior of the wcstombs function. */

#include <stdio.h>
#include <stdlib.h>

void main( void )
{
   int      i;
   char    *pmbbuf   = (char *)malloc( MB_CUR_MAX );
   wchar_t *pwchello = L"Hello, world.";

   printf( "Convert wide-character string:\n" );
   i = wcstombs( pmbbuf, pwchello, MB_CUR_MAX );
   printf( "\tCharacters converted: %u\n", i );
   printf( "\tMultibyte character: %s\n\n", pmbbuf );
}
0
 
nehayaAuthor Commented:
Sorry Sir, Where is the byte [] here?

Imagine this BYTE arrary: ( this one is small of the sake of the example )

BYTE bStr[7] = {0xD9,0x87,0XD9,0x84,0xD8,0xA7,0x00};
char *  cStr;

How can I use the functions you provided?
Thanks..
0
 
AxterCommented:
Example:
int      i;
BYTE bStr[7] = {0xD9,0x87,0XD9,0x84,0xD8,0xA7,0x00};
char *  cStr  = (char *)malloc( MB_CUR_MAX );

   printf( "Convert wide-character string:\n" );
   i = wcstombs( cStr,  (wchar_t *)bStr, MB_CUR_MAX );
   printf( "\tCharacters converted: %u\n", i );
   printf( "\tMultibyte character: %s\n\n", cStr);
0
 
nehayaAuthor Commented:
Sorry.. not working : (..  not the same output.. !

Notice:
if every second byte is 0x00, it works fine.. exmaple:
BYTE bStr[6] = {0x67,0x00,0X65,0x00,0x68,0x00};
if it's real unicoded ( utf-8 ), the second byte will vary.. HOWEVER, the output using your code will generate a long series of the same char..

can we try with MultiByte ?
0
 
AxterCommented:
>>if it's real unicoded ( utf-8 ), the second byte will vary.. HOWEVER, the output using your code will generate a long series of the same char..

That's not TRUE UNICODE, and it will not work with wcstombs.

If it's UTF-8, then you're going to have characters that can not be translated into ANSI char type.

What you should be converting it to, is UTF-16, and not ANSI.
0
 
nehayaAuthor Commented:
Thanks for your help, again..
Let me explain the question again:
if i have a  string of (e.g)  10 chars, where each char is represented in 2 bytes.. the total will be 20 bytes.. right?
How can I re-represent the byte array in a char *..? ( note the result char * length will be 10 chars long! not 20 )
I tried your way, it's not working unless that every second byte is 0x00.. not anything else..!

..
0
 
nehayaAuthor Commented:
Ok,,
Is it possible to convert this 20 byte array into LPCWSTR or whateve that hold a unicode string?
0
 
AxterCommented:
>>Is it possible to convert this 20 byte array into LPCWSTR or whateve that hold a unicode string?

It's possible to convert UTF-8 to UTF-16.

LPCWSTR is typically a pointer to UTF-16 string.

If you're byte array stores UTF-8, then you can convert it to UTF-16 using mbstowcs.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.