Defined behavior of cast from unsigned char * to char * in Objective-C

I understand the difference between unsigned char * and char * types. I also understand how to use reinterpret_cast to cast an unsigned char * to a char * in C++.

I'm using sqlite3 in Objective-C and am trying to get an NSString from a call to


To do this, I'm basically doing:

char *cParam = (char *)sqlite3_column_text(compiledStatementPtr, 0); NSString *aParam = nil; if (cParam) { aParam = [NSString stringWithUTF8String:cParam]; }

sqlite3_column_text(), however, returns an unsigned char * and I'm worried about the behavior of the cast to char *. I've read that the cast is implemenation specific and I was wondering if this is a safe cast in Objective-C or if I'm totally barking up the wrong tree?

-------------Problems Reply------------

You don't have much to worry about due to the cast. Actually, Apple sample code uses the exact same code pattern you are using:

self.title =
[NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 0)];

(From SQLite Book List Book.m class).

I think you should be more concerned with the database actually having UTF-8 strings.

Category:objective c Views:0 Time:2009-08-05

Related post

  • reinterpret casting to and from unsigned char* and char* 2009-08-04

    I'm wondering if it is necessary to reinterpret_cast in the function below. ITER_T might be a char*, unsigned char*, std::vector<unsigned char> iterator, or something else like that. It doesn't seem to hurt so far, but does the casting ever aff

  • Casting an unsigned int + a string to an unsigned char vector 2011-10-07

    I'm working with the NetLink socket library ( ), and I want to send some binary data over the network in a format that I specify. The format I have planned is pretty simple and is as follows: Byt

  • C++ style cast from unsigned char * to const char * 2009-03-18

    I have: unsigned char *foo(); std::string str; str.append(static_cast<const char*>(foo())); The error: invalid static_cast from type ‘unsigned char*’ to type ‘const char*’ What's the correct way to cast here in C++ style? --------------Solution

  • C/C++ casting problems, unsigned char to char 2011-02-25

    i have an unsigned char which i need to convert to char before printf. So the example code goes like this: unsigned char y = sample.result; char x = (char)y; printf("%c \n", x); However, printf does not print x but if i use cout, x prints correctly.I

  • Is there a good way to convert from unsigned char* to char*? 2011-01-16

    I've been reading a lot those days about reinterpret_cast<> and how on should use it (and avoid it on most cases). While I understand that using reinterpret_cast<> to cast from, say unsigned char* to char* is implementation defined (and t

  • only allow defined behavior in C++? 2011-04-06

    Is it possible in either gcc/g++ or ms c++ to set a flag which only allows defined behavior? so something like the below gives me a warning or preferably an error func(a++, a, ++a) --------------Solutions------------- Undefined and unspecified behavi

  • casting char * to char[] 2011-12-08

    Example: char str[10]; gets(str); str = (char[10]) strtok(str, " "); // type error here Since strtok() returns a char *, I get an type error without that casting. With it I get the following: error: cast specifies array type What is the best to fix t

  • How to write binary data into a file without invoking implementation-defined behavior? 2012-09-17

    I am writing a program like this to write some bytes into a file. #include <fstream> int main() { char buffer[4] = {0, 0, 255, 255}; std::ofstream f("foo.txt", std::ios_base::binary); f.write(buffer, sizeof buffer); f.close(); return 0; } This

  • Undefined, unspecified and implementation-defined behavior 2010-03-07

    What is the difference between undefined, unspecified, and implementation-defined behavior in C and C++? --------------Solutions------------- Undefined behavior is one of those aspects of the C and C++ language that can be surprising to programmers c

  • Indirectly destroying object from it's own virtual method. Is it a defined behavior? 2010-08-12

    Am I allowed to indirectly destroy object from within object's own virtual method? Is it a "defined behavior" (as long as I'm not trying to access anything after destroying the object)? Example: #include <memory> #include <stdio.h> using

  • Cast char to char array or char array to char? 2011-01-14

    So let's say I have a char and I want to strcat() it to a char array in a single line of code. For [a non-practical] example: strcat("ljsdflusdfg",getchar()); Or I wanted to do the reverse, what would be the appropriate function to concat or typecast

  • C - casting int to char and append char to char 2011-02-15

    I am making my first parallel application, but I am stuck with basics of C. I need to know, how to cast int to char and then how to append one char to another. It you could help me please, i would be glad. Thank you. --------------Solutions----------

  • There are possible way of dividing two float numbers which was casted from unsigned int make negative float result? 2011-05-17

    unsigned int deltaTime = now() - last(); unsigned int freq = getFreq(); float timeOfGap = float(deltaTime) / float(freq); ASSERT(0.0f <= timeOfGap, "WHAT?"); I got a really weired result from my program. I didn't catch a scene which is happened. B

  • How to cast from unsigned long to void*? 2011-08-01

    I am trying to pwrite some data at some offset of a file with a given file descriptor. My data is a stored in two vectors. One contains unsigned longs and the other chars. I thought of building a void * that points to the bit sequence representing my

  • Difference between unsigned char and char pointers 2012-02-10

    I'm a bit confused with differences between unsigned char (which is also BYTE in WinAPI) and char pointers. Currently I'm working with some ATL-based legacy code and I see a lot of expressions like the following: CAtlArray<BYTE> rawContent; CAL

  • Why does Microsoft define a type CHAR as char and a macro CONST as const? 2015-01-14

    This question already has an answer here: Windows Data Types… why so redundant/undescriptive? 3 answers As a C beginner, looking through MSDN's Windows Data Types, I see there are a lot of types that are defined that seem redundant. For example using

  • Can't Cast Integer to Char in Char Array 2009-07-01

    Hey guys, I am trying to practice C++ and while doing so I ran into a problem in my code. I dynamically create a character array and then for each array index, I want to fill that element with an integer. I tried casting the integer to a character bu

  • Does passing an empty range (identical iterators) to an STL algorithm result in defined behavior? 2010-02-16

    Consider the following: std::vector<int> vec(1); // vector has one element std::fill(vec.begin(), vec.begin(), 42); std::fill(vec.begin()+1, vec.end(), 43); std::fill(vec.end(), vec.end(), 44); Will all of the std::fill usages above result in d

  • Const unsigned char* to char* 2010-04-05

    So, I have two types at the moment: const unsigned char* unencrypted_data_char; string unencrypted_data; I'm attempting to perform a simple conversion of data from one to the other (string -> const unsigned char*) As a result, I have the following

Copyright (C), All Rights Reserved.

processed in 0.175 (s). 11 q(s)