Okay small problem using SDL_TTF...unsigned char* cannot convert to char*

And here I thought I wouldn’t need help. Turns out I was wrong again.

I decided as a fun little project to make a CD Player. I know, lame,
but I am still a beginner. And I thought it would be a fun project.
Anywho, in order to see what was playing and how far along I was in
the song I needed to draw the id of the track. And I did this using a
small function I made.

In order to make the placing of text easier on my screen I put all of
the font rendering pieces in a small function I called PixelText()
here’s what it looks like

void PixelText(char* text, int textx, int texty)
{
textDraw = TTF_RenderText_Solid(mainFont, text, fontColor);

SDL_Rect rcSrc;
SDL_Rect rcDst;

//source rectangle
rcSrc.x=rcSrc.y=0;
rcSrc.w=textDraw->w;
rcSrc.h=textDraw->h;

//destination rectangle
rcDst=rcSrc;
rcDst.x=textx;
rcDst.y=texty;

//blit the surface
SDL_BlitSurface(textDraw,&rcSrc,mainScreen,&rcDst);

SDL_FreeSurface(textDraw);

}

Very simple, however as you can see it uses a char* just as is
required by TTF_RenderText_Solid(). And unfortunately
cdToPlay->track[0].id is an unsigned char*. So I can’t exactly just
use it like this.

PixelText(cdToPlay->track[0].id,10,20);

It comes up with this error when I try

PixelText’ : cannot convert parameter 1 from ‘unsigned char’ to 'char *'
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast

I’ve tried using reinterpret_cast but probably incorrectly I’ve even
done the simple (char*) cast and that doesn’t work. I really don’t
understand casting, could anyone help me here? What exactly would I
need to do to use the track id in my function?

Also how would I convert numbers to text? I’m assuming that uses the
same thing.

If it’s saying “cannot convert unsigned char to char *” the problem is that
you are passing a char where it is expecting a pointer to char so just toss
a & in front of what you are passing and it should be ok!

If you are looking to turn a number into a string here’s how…

char Buffer[256];
int I;

I=20000;

sprintf(Buffer,"%i",I);

then after that, buffer will be “20000”.

Hope that helps!
Alan> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Matthew Johnson
Sent: Thursday, August 23, 2007 9:27 PM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: [SDL] Okay small problem using SDL_TTF…unsigned char*
cannotconvert to char*…

And here I thought I wouldn’t need help. Turns out I was wrong again.

I decided as a fun little project to make a CD Player. I know, lame,
but I am still a beginner. And I thought it would be a fun project.
Anywho, in order to see what was playing and how far along I was in
the song I needed to draw the id of the track. And I did this using a
small function I made.

In order to make the placing of text easier on my screen I put all of
the font rendering pieces in a small function I called PixelText()
here’s what it looks like

void PixelText(char* text, int textx, int texty)
{
textDraw = TTF_RenderText_Solid(mainFont, text, fontColor);

SDL_Rect rcSrc;
SDL_Rect rcDst;

//source rectangle
rcSrc.x=rcSrc.y=0;
rcSrc.w=textDraw->w;
rcSrc.h=textDraw->h;

//destination rectangle
rcDst=rcSrc;
rcDst.x=textx;
rcDst.y=texty;

//blit the surface
SDL_BlitSurface(textDraw,&rcSrc,mainScreen,&rcDst);

SDL_FreeSurface(textDraw);
}

Very simple, however as you can see it uses a char* just as is
required by TTF_RenderText_Solid(). And unfortunately
cdToPlay->track[0].id is an unsigned char*. So I can’t exactly just
use it like this.

PixelText(cdToPlay->track[0].id,10,20);

It comes up with this error when I try

PixelText’ : cannot convert parameter 1 from ‘unsigned char’ to 'char *'
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast

I’ve tried using reinterpret_cast but probably incorrectly I’ve even
done the simple (char*) cast and that doesn’t work. I really don’t
understand casting, could anyone help me here? What exactly would I
need to do to use the track id in my function?

Also how would I convert numbers to text? I’m assuming that uses the
same thing.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well I tried using the & in front it didn’t work, the problem seems to
be that track[0].id is an unsigned char * and I need a regular char*
both of them say char* in them but only one says unsigned.

I also must be hopeless cause I tried doing something very similar to
your example for putting numbers into text.

int songTrack[256];
int CDIndex;

CDIndex = 4; //start playing track 5

sprintf(songTrack,"%i",CDIndex);

PixelText(&songTrack,20,20);

And received a cannot convert a char (*) [256] to a char * error.

I do appreciate the help though.On 8/23/07, Alan Wolfe wrote:

If it’s saying “cannot convert unsigned char to char *” the problem is that
you are passing a char where it is expecting a pointer to char so just toss
a & in front of what you are passing and it should be ok!

If you are looking to turn a number into a string here’s how…

char Buffer[256];
int I;

I=20000;

sprintf(Buffer,"%i",I);

then after that, buffer will be “20000”.

Hope that helps!
Alan

-----Original Message-----
From: sdl-bounces at lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Matthew Johnson
Sent: Thursday, August 23, 2007 9:27 PM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: [SDL] Okay small problem using SDL_TTF…unsigned char*
cannotconvert to char*…

And here I thought I wouldn’t need help. Turns out I was wrong again.

I decided as a fun little project to make a CD Player. I know, lame,
but I am still a beginner. And I thought it would be a fun project.
Anywho, in order to see what was playing and how far along I was in
the song I needed to draw the id of the track. And I did this using a
small function I made.

In order to make the placing of text easier on my screen I put all of
the font rendering pieces in a small function I called PixelText()
here’s what it looks like

void PixelText(char* text, int textx, int texty)
{
textDraw = TTF_RenderText_Solid(mainFont, text, fontColor);

    SDL_Rect rcSrc;
    SDL_Rect rcDst;

    //source rectangle
    rcSrc.x=rcSrc.y=0;
    rcSrc.w=textDraw->w;
    rcSrc.h=textDraw->h;

    //destination rectangle
    rcDst=rcSrc;
    rcDst.x=textx;
    rcDst.y=texty;

    //blit the surface
    SDL_BlitSurface(textDraw,&rcSrc,mainScreen,&rcDst);

    SDL_FreeSurface(textDraw);

}

Very simple, however as you can see it uses a char* just as is
required by TTF_RenderText_Solid(). And unfortunately
cdToPlay->track[0].id is an unsigned char*. So I can’t exactly just
use it like this.

PixelText(cdToPlay->track[0].id,10,20);

It comes up with this error when I try

PixelText’ : cannot convert parameter 1 from ‘unsigned char’ to 'char *'
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast

I’ve tried using reinterpret_cast but probably incorrectly I’ve even
done the simple (char*) cast and that doesn’t work. I really don’t
understand casting, could anyone help me here? What exactly would I
need to do to use the track id in my function?

Also how would I convert numbers to text? I’m assuming that uses the
same thing.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Behalf Of Matthew Johnson
charcannotconvert to char

Well I tried using the & in front it didn’t work, the problem seems to be
that track[0].id is an unsigned char * and I need a regular char* both of
them say char* in them but only one says unsigned.

I also must be hopeless cause I tried doing something very similar to your
example for putting numbers into text.

int songTrack[256];
int CDIndex;

CDIndex = 4; //start playing track 5

sprintf(songTrack,"%i",CDIndex);

PixelText(&songTrack,20,20);

And received a cannot convert a char (*) [256] to a char * error.

I do appreciate the help though.


It appears that you have songTrack declared as an integer array and not a
character array.

Try:

char songTrack[256];
int CDIndex;

CDIndex = 4; //start playing track 5

sprintf(songTrack,"%i",CDIndex);

PixelText(&songTrack,20,20);

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/

If it’s saying “cannot convert unsigned char to char *” the problem is
that you are passing a char where it is expecting a pointer to char so
just toss a & in front of what you are passing and it should be ok!

If you are looking to turn a number into a string here’s how…

char Buffer[256];
int I;

I=20000;

sprintf(Buffer,"%i",I);

then after that, buffer will be “20000”.

Hope that helps!
Alan

[mailto:sdl-bounces at lists.libsdl.org] On Behalf Of Matthew Johnson
SDL-announce)
cannotconvert to char*…

And here I thought I wouldn’t need help. Turns out I was wrong again.

I decided as a fun little project to make a CD Player. I know, lame,
but I am still a beginner. And I thought it would be a fun project.
Anywho, in order to see what was playing and how far along I was in
the song I needed to draw the id of the track. And I did this using a
small function I made.

In order to make the placing of text easier on my screen I put all of
the font rendering pieces in a small function I called PixelText()
here’s what it looks like

void PixelText(char* text, int textx, int texty) {
textDraw = TTF_RenderText_Solid(mainFont, text, fontColor);

    SDL_Rect rcSrc;
    SDL_Rect rcDst;

    //source rectangle
    rcSrc.x=rcSrc.y=0;
    rcSrc.w=textDraw->w;
    rcSrc.h=textDraw->h;

    //destination rectangle
    rcDst=rcSrc;
    rcDst.x=textx;
    rcDst.y=texty;

    //blit the surface
    SDL_BlitSurface(textDraw,&rcSrc,mainScreen,&rcDst);

    SDL_FreeSurface(textDraw);

}

Very simple, however as you can see it uses a char* just as is
required by TTF_RenderText_Solid(). And unfortunately
cdToPlay->track[0].id is an unsigned char*. So I can’t exactly just
use it like this.

PixelText(cdToPlay->track[0].id,10,20);

It comes up with this error when I try

PixelText’ : cannot convert parameter 1 from ‘unsigned char’ to 'char *'
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast

I’ve tried using reinterpret_cast but probably incorrectly I’ve even
done the simple (char*) cast and that doesn’t work. I really don’t
understand casting, could anyone help me here? What exactly would I
need to do to use the track id in my function?

Also how would I convert numbers to text? I’m assuming that uses the
same thing.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.12.4/969 - Release Date: 8/23/2007
4:04 PM

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.12.4/969 - Release Date: 8/23/2007
4:04 PM

----- Original Message -----
From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Sent: Friday, August 24, 2007 12:08 AM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] Okay small problem using SDL_TTF…unsigned
On 8/23/07, Alan Wolfe wrote:
-----Original Message-----
From: sdl-bounces at lists.libsdl.org
Sent: Thursday, August 23, 2007 9:27 PM
To: A list for developers using the SDL library. (includes
Subject: [SDL] Okay small problem using SDL_TTF…unsigned char*

Try:

char songTrack[256];
int CDIndex;

CDIndex = 4; //start playing track 5

sprintf(songTrack,"%i",CDIndex);

PixelText(&songTrack,20,20);

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/

not PixelText(&songTrack, 20, 20);

just PixelText(songTrack, 20, 20);

Awesome that last small fix of removing & from songTrack worked. Much
obliged all. Wonderful to see it working properly.

But that still leaves the initial problem of how do I convert from an
unsigned char* to a regular char*? My books, MSDN, and every help
file I’ve found online doesn’t seem to give a straight enough answer
for me to follow.

I don’t suppose it’s actually a big deal if I can’t get the
track[0].id to print to the surface but it would be a nice feature to
have.On 8/24/07, Will Langford wrote:

Try:

char songTrack[256];
int CDIndex;

CDIndex = 4; //start playing track 5

sprintf(songTrack,"%i",CDIndex);

PixelText(&songTrack,20,20);

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/

not PixelText(&songTrack, 20, 20);

just PixelText(songTrack, 20, 20);


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

As Alex tried to point out, your error was: PixelText’ : cannot convert
parameter 1 from ‘unsigned char’ to ‘char *’.

The first is just an unsigned char. the second is a pointer.

In reguards to converting from an unsigned char * to char *, you can
generally get away with just casting it… especially if you’re just playing
with 7bit ascii strings.

unsigned char foo[];

void function(char * bar, int x, int y);

function((char *)foo, 20, 20);

If you wanna be all kinds of proper and slow, you could create a converter
function similar to a string copy…

convert(char * dest, unsigned char * src, int len)
{
while(len–) *dest++ = (char) *src++;
}On 8/24/07, Matthew Johnson wrote:

Awesome that last small fix of removing & from songTrack worked. Much
obliged all. Wonderful to see it working properly.

But that still leaves the initial problem of how do I convert from an
unsigned char* to a regular char*? My books, MSDN, and every help
file I’ve found online doesn’t seem to give a straight enough answer
for me to follow.

Well my battery is dying so I can’t test any more. However when I did
(char*)cdToPlay->track[0].id it didn’t register an error while
compiling but did cause SDL to shut down the program. The only hint I
managed from it was a “Segmentation fault” error in stderr. But oh
well, it’s not important. At least I’ve got the ability to display
numbers. Thanks again guys. Much appreciated.On 8/24/07, Will Langford wrote:

On 8/24/07, Matthew Johnson <@Matthew_Johnson> wrote:

Awesome that last small fix of removing & from songTrack worked. Much
obliged all. Wonderful to see it working properly.

But that still leaves the initial problem of how do I convert from an
unsigned char* to a regular char*? My books, MSDN, and every help
file I’ve found online doesn’t seem to give a straight enough answer
for me to follow.

As Alex tried to point out, your error was: PixelText’ : cannot convert
parameter 1 from ‘unsigned char’ to ‘char *’.

The first is just an unsigned char. the second is a pointer.

In reguards to converting from an unsigned char * to char *, you can
generally get away with just casting it… especially if you’re just playing
with 7bit ascii strings.

unsigned char foo[];

void function(char * bar, int x, int y);

function((char *)foo, 20, 20);

If you wanna be all kinds of proper and slow, you could create a converter
function similar to a string copy…

convert(char * dest, unsigned char * src, int len)
{
while(len–) *dest++ = (char) *src++;
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

C is not C++ or any other language that supports operator overloading or
immediate type conversion etc etc etc

cdtoplay->track[0].id is a single byte. it’s not a string. its not an
array.

pixeltext (or whatever it was), needs a string / array to work from.

if you cast cdtoplay->track[0].id to a pointer, you’re just… generally
gonna fubar everything up.

track[0].id might be… 0x01.

so the string you’re trying to force / create out of this is going to have a
base address of 0x00000001. darn near close to NULL. which isn’t what you
want.

you have to use sprintf() or similar to convert mister track[0].id into a
third-party-variable string (buffer[], temp[], etc, whatever ya want).On 8/24/07, Matthew Johnson wrote:

Well my battery is dying so I can’t test any more. However when I did
(char*)cdToPlay->track[0].id it didn’t register an error while
compiling but did cause SDL to shut down the program. The only hint I
managed from it was a “Segmentation fault” error in stderr. But oh
well, it’s not important. At least I’ve got the ability to display
numbers. Thanks again guys. Much appreciated.

On 8/24/07, Will Langford <@William_Langford> wrote:

On 8/24/07, Matthew Johnson wrote:

Awesome that last small fix of removing & from songTrack worked. Much
obliged all. Wonderful to see it working properly.

But that still leaves the initial problem of how do I convert from an
unsigned char* to a regular char*? My books, MSDN, and every help
file I’ve found online doesn’t seem to give a straight enough answer
for me to follow.

As Alex tried to point out, your error was: PixelText’ : cannot convert
parameter 1 from ‘unsigned char’ to ‘char *’.

The first is just an unsigned char. the second is a pointer.

In reguards to converting from an unsigned char * to char *, you can
generally get away with just casting it… especially if you’re just
playing
with 7bit ascii strings.

unsigned char foo[];

void function(char * bar, int x, int y);

function((char *)foo, 20, 20);

If you wanna be all kinds of proper and slow, you could create a
converter
function similar to a string copy…

convert(char * dest, unsigned char * src, int len)
{
while(len–) *dest++ = (char) *src++;
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Awesome that last small fix of removing & from songTrack worked. Much
obliged all. Wonderful to see it working properly.

But that still leaves the initial problem of how do I convert from an
unsigned char* to a regular char*? My books, MSDN, and every help
file I’ve found online doesn’t seem to give a straight enough answer
for me to follow.

As Alex tried to point out, your error was: PixelText’ : cannot convert
parameter 1 from ‘unsigned char’ to ‘char *’.

The first is just an unsigned char. the second is a pointer.

Right.

In reguards to converting from an unsigned char * to char *, you can
generally get away with just casting it… especially if you’re just
playing with 7bit ascii strings.

unsigned char foo[];

void function(char * bar, int x, int y);

function((char *)foo, 20, 20);

I tend to disagree here:

  1. Proper code will compile without casts - before casting, always ask
    yourself what conversion takes place here and what do I need. In C++
    specifically, avoid C-style casts like the plague, they are dangerous and
    unnecessary.
  2. An unsigned char doesn’t necessarily represent any text, it is also often
    used when all you need is a simple byte. Now, in order to convert it to a
    string, you first need to know what is in there and only then you can convert
    accordingly.
  3. Ever heard of ‘const’? If a function isn’t modifying a string given to it,
    pass it as ‘char const*’, which will help getting code correct, too.

If you wanna be all kinds of proper and slow, you could create a converter
function similar to a string copy…

convert(char * dest, unsigned char * src, int len)
{
while(len–) *dest++ = (char) *src++;
}

This is what memcpy() does, though it uses a const source and a size_t instead
of an int.

UliOn Friday 24 August 2007 07:38:26 Will Langford wrote:

On 8/24/07, Matthew Johnson wrote:

Converting between char * and unsigned char * is free, you don’t need to
loop through and assign from one to the other, and memcpy doesn’t loop
through memory to convert, it just does so to copy.

If you check out his original message, he is having problem converting from
unsigned char to char * despite the misleading subject of the email. It
looks like he’s trying to pass a number (stored in an unsigned char) to a
function which wants a string (char *).

sprintf should be his friend there (or snprintf if you are worried about
security!) so once he prints the number into a buffer and passes that to his
pixel text function he should be good to go.

Can’t believe how complicated this discussion has gotten over this :stuck_out_tongue:

If anyone is looking for a good generic game programming list (since this
list is for SDL specifically) check out the list at game programmer.com.
Great place w/ lots of sdl users as well.> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Ulrich Eckhardt
Sent: Sunday, August 26, 2007 2:16 AM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Okay small problem using
SDL_TTF…unsignedcharcannotconvert to char

On Friday 24 August 2007 07:38:26 Will Langford wrote:

On 8/24/07, Matthew Johnson wrote:

Awesome that last small fix of removing & from songTrack worked. Much
obliged all. Wonderful to see it working properly.

But that still leaves the initial problem of how do I convert from an
unsigned char* to a regular char*? My books, MSDN, and every help
file I’ve found online doesn’t seem to give a straight enough answer
for me to follow.

As Alex tried to point out, your error was: PixelText’ : cannot convert
parameter 1 from ‘unsigned char’ to ‘char *’.

The first is just an unsigned char. the second is a pointer.

Right.

In reguards to converting from an unsigned char * to char *, you can
generally get away with just casting it… especially if you’re just
playing with 7bit ascii strings.

unsigned char foo[];

void function(char * bar, int x, int y);

function((char *)foo, 20, 20);

I tend to disagree here:

  1. Proper code will compile without casts - before casting, always ask
    yourself what conversion takes place here and what do I need. In C++
    specifically, avoid C-style casts like the plague, they are dangerous and
    unnecessary.
  2. An unsigned char doesn’t necessarily represent any text, it is also often

used when all you need is a simple byte. Now, in order to convert it to a
string, you first need to know what is in there and only then you can
convert
accordingly.
3. Ever heard of ‘const’? If a function isn’t modifying a string given to
it,
pass it as ‘char const*’, which will help getting code correct, too.

If you wanna be all kinds of proper and slow, you could create a converter
function similar to a string copy…

convert(char * dest, unsigned char * src, int len)
{
while(len–) *dest++ = (char) *src++;
}

This is what memcpy() does, though it uses a const source and a size_t
instead
of an int.

Uli


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org