473,225 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

convertion problem? from char* to unsigned char*?

Hello,
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?

BYTE* bite;
char* ch="help me, please";
bite = (BYTE*)ch; ? is this not right? what shoul i do here?

2) Second Problem

I defined array of WORD in c++;

WORD ar[7];
when how i can give it default value?
default values must be NULL(or zero).

i mean the arrays addres must be clean, must be NULL,

how should i do for it?

thanks

Sep 6 '07 #1
4 2738
HackerisNewKnight wrote:
Hello,
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?

BYTE* bite;
char* ch="help me, please";
bite = (BYTE*)ch; ? is this not right? what shoul i do here?
right, but not good.

static_cast<BYTE*>(ch);

But don't cast it to unsigned char*, if it's only a string literal, it's
useless. Or that's only your demo.
2) Second Problem

I defined array of WORD in c++;

WORD ar[7];
when how i can give it default value?
no default value actually,
you may say, how to give initial value.

WORD ar[7] = {1,2,3,4,5,6,7};

or

WORD ar[] = {1,2,3,4,5,6,7};
default values must be NULL(or zero).
the value is random.
>
i mean the arrays addres must be clean, must be NULL,
no such way of expression.
>
how should i do for it?

thanks

--
Thanks
Barry
Sep 6 '07 #2
HackerisNewKnight wrote:
Hello,
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?

BYTE* bite;
char* ch="help me, please";
That's a C-ism. 'ch' should be actually declared

char const *ch = ...; // note the 'const'
bite = (BYTE*)ch; ? is this not right? what shoul i do here?
Depends on what you're going to do with it. A cast to a pointer to
non-const BYTE is in most cases wrong. You probably want to cast it
to a pointer to const BYTE.
>
2) Second Problem

I defined array of WORD in c++;

WORD ar[7];
when how i can give it default value?
default values must be NULL(or zero).

WORD ar[7] = {};
i mean the arrays addres must be clean, must be NULL,

how should i do for it?
You cannot control the address of the array. It's allocated when
you define it, and it gets its address there and then.

Again, what do you need 'ar' for? Do you really need it to be
an array of 7 'WORD's?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 6 '07 #3
On Sep 6, 5:07 pm, Barry <dhb2...@gmail.comwrote:
HackerisNewKnight wrote:
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?
BYTE* bite;
char* ch="help me, please";
bite = (BYTE*)ch; ? is this not right? what shoul i do here?
right, but not good.
static_cast<BYTE*>(ch);
That shouldn't compile; you need a reinterpret_cast here. Or
static_cast to void*, and then to BYTE*. And as Victor has
pointed out, ch should really be a char const*, so you'd also
need a const_cast.
But don't cast it to unsigned char*, if it's only a string
literal, it's useless. Or that's only your demo.
That would be my real question as well: why the conversion to
begin with? If you're dealing with text, char is the type, not
unsigned char, and while some legacy interfaces in the C library
do require converting an individual char to an unsigned char
(those in <ctype.h>), the need to convert char* to unsigned
char* should be exceedingly rare.
2) Second Problem
I defined array of WORD in c++;
WORD ar[7];
when how i can give it default value?
It depends, of course, on the type. He doesn't say what WORD
is.
no default value actually,
you may say, how to give initial value.
WORD ar[7] = {1,2,3,4,5,6,7};
or
WORD ar[] = {1,2,3,4,5,6,7};
default values must be NULL(or zero).
the value is random.
He can always write something like:

WORD ar[7] = {} ;

to get zero initialization for all of the elements.
i mean the arrays addres must be clean, must be NULL,
no such way of expression.
I'm not even sure what he means. The address of ar can't be
null, ever.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 7 '07 #4
On Sep 7, 10:26 am, James Kanze <james.ka...@gmail.comwrote:
On Sep 6, 5:07 pm, Barry <dhb2...@gmail.comwrote:
HackerisNewKnight wrote:
1) First problem.
In my project i need to convert from char* to BYTE*( BYTE is unsigned
char),
is there any way to make the convertion?
BYTE* bite;
char* ch="help me, please";
bite = (BYTE*)ch; ? is this not right? what shoul i do here?
right, but not good.
static_cast<BYTE*>(ch);

That shouldn't compile; you need a reinterpret_cast here. Or
static_cast to void*, and then to BYTE*. And as Victor has
pointed out, ch should really be a char const*, so you'd also
need a const_cast.
But don't cast it to unsigned char*, if it's only a string
literal, it's useless. Or that's only your demo.

That would be my real question as well: why the conversion to
begin with? If you're dealing with text, char is the type, not
unsigned char, and while some legacy interfaces in the C library
do require converting an individual char to an unsigned char
(those in <ctype.h>), the need to convert char* to unsigned
char* should be exceedingly rare.
2) Second Problem
I defined array of WORD in c++;
WORD ar[7];
when how i can give it default value?

It depends, of course, on the type. He doesn't say what WORD
is.
no default value actually,
you may say, how to give initial value.
WORD ar[7] = {1,2,3,4,5,6,7};
or
WORD ar[] = {1,2,3,4,5,6,7};
default values must be NULL(or zero).
the value is random.

He can always write something like:

WORD ar[7] = {} ;

to get zero initialization for all of the elements.
i mean the arrays addres must be clean, must be NULL,
no such way of expression.

I'm not even sure what he means. The address of ar can't be
null, ever.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


Sorry for my mistake,
i mean by "arrays addres must be clean, must be NULL" is to get zero
initialization for all elements of The array,

now it is clear for me.

thanks for your help
Sep 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chiller | last post by:
I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc. I wish to write...
5
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/...
1
by: tsunami | last post by:
I have written a bitarray class which has a constructor taking bit stream as a string parameter.but it gives me an error such that cannot convert 'char*' to 'unsigned char*'.where I have declared...
0
by: Yodai | last post by:
Hi all.... I am trying to reduce the code of this function and I presume there's a way to do it through a loop, buit I can't figure out how. This is what I got: an embedded processor with a mini...
0
by: Yodai | last post by:
Hi all.... I am trying to reduce the code of this function and I presume there's a way to do it through a loop, buit I can't figure out how. This is what I got: an embedded processor with a mini...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
27
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type;...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.