473,382 Members | 1,420 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,382 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 2747
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
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.