How to call function with arguments( char *, int *) in below code using node-ffi? - ffi

C++ third party library function
int getUCode( char* sample, int* size);
Usage
char Code[256] = {0};
length = sizeof(Code);
ret = getUCode(Code, &length);
Trying to Call above getUCode function using ffi-napi. But getting empty buffer.
const charPointer = ref.refType('char')
"getUCode": [
"int32", [charPointer ,"int32"]
var str = Buffer.alloc(256)
var Len = 256
var val = lib.getUCode(str,Len);

We can use ref.refType to tell integer pointer in function signature
var int = ref.types.int;
var intPtr = ref.refType(int);
var cstring = ref.types.CString;
"getUCode": [
"int32", [ cstring ,intPtr]
],
Use Buffer to pass actual value in function calls
var str = Buffer.alloc(256)
const Len = ref.alloc(int, 256)
var val = lib.getUCode(str, len)

Related

C - unable to pass int by pointer to nested function call

in Main function, calling a function to loop through a text and check if a column already exists. If not add it into an array of string.
Main:
char** departmentList = NULL;
int sizeOfDepartmentList = 0;
departmentList = collectAllDepartments(departmentList, &sizeOfDepartmentList,recordsPtr);
Function loop for a text and check for new elements:
char** collectAllDepartments(char** departmentList, int *sizeOfDepartmentList, char* records){
char* copyOfRecords = malloc(strlen(records)+1);
strcpy(copyOfRecords,records);
char* sav1 = NULL;
char* token = strtok_s(copyOfRecords,"\n",&sav1);
while(token != NULL){
//char *token_orig = _strdup(token);
char* sav2 = NULL;
char* innerToken = strtok_s(token,",",&sav2);
int counter = 1;
while(innerToken != NULL){
if(counter == 4){
if(!departmentExists(departmentList,*sizeOfDepartmentList,innerToken)){
departmentList = addDepartment(departmentList,&sizeOfDepartmentList,innerToken);
}
// allDepartments = (char*) realloc(allDepartments,strlen(innerToken) + strlen(allDepartments));
// strcat(allDepartments,innerToken);
// strcat(allDepartments,",");
}
++counter;
innerToken = strtok_s(NULL, ",", &sav2);
}
token = strtok_s(NULL, "\n", &sav1);
//free(token_orig);
}
free(copyOfRecords);
return departmentList;
}
The function that pushback new elements into the array of string:
char** addDepartment(char** deparmentList, int* sizeOfDepartmentList, char* department){
*sizeOfDepartmentList += 1;
deparmentList = realloc(deparmentList, *sizeOfDepartmentList * sizeof(char *));
deparmentList[*sizeOfDepartmentList - 1] = department;
return deparmentList;
}
The int need to be passed by pointer twice in order to be used in the pushback function. But the compiler complaint receiving pointer of pointer instead of pointer:

How to create a list of word with specific length

Im currently writing a C programming to create a list of word with specific length.
For example, I have a string of ten letter
ABCDEFGHIJ
and wanted to create a list of word where the length is 2 that is
AB,BC,CD,DE,EF,FG,GH,HI,IJ.
For now I can only create
AB,CD,EF,GH,IJ
with my coding
#include<stdio.h>
#include<string.h>
int main()
{
/*q=length of query sequence, W=length of substring, t=(q-w)+1*/
char array[5][3],length[3];
int i=1;
FILE *input;
input=fopen("string.txt", "r");
/*stores and prints the data from the string*/
while(fgets(length,3,input))
{
strcpy(array[i],length);
printf(">%d\n%s\n\n",i,array[i]);
i++;
}
return 0;
}
How can I point the array to starts the next word at position from the second letter not the third letter?
Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char wmers[115];
int i = 1, j = 2;
FILE *input = NULL;
FILE *output;
if (!(input = fopen("exon11.txt", "r"))) {
// Error
return (EXIT_FAILURE);
}
if (!fgets(wmers,115,input)) {
// Error ?
}
printf(">%d\n%s\n\n",i,wmers);
i++;
output=fopen("2.txt", "w");
fputs(wmers,output);
for(int c = fgetc(input); c != EOF; c = fgetc(input))
{
wmers[0] = wmers[1];
wmers[1] = wmers[2];
wmers[2] = wmers[3];
wmers[3] = wmers[4];
wmers[4] = wmers[5];
wmers[5] = wmers[6];
wmers[6] = wmers[7];
wmers[7] = wmers[8];
wmers[8] = wmers[9];
wmers[9] = wmers[10];
wmers[10] = wmers[11];
wmers[11] = wmers[12];
wmers[12] = wmers[13];
wmers[13] = wmers[14];
wmers[14] = wmers[15];
wmers[15] = wmers[16];
wmers[16] = wmers[17];
wmers[17] = wmers[18];
wmers[18] = wmers[19];
wmers[19] = wmers[20];
wmers[20] = wmers[21];
wmers[21] = wmers[22];
wmers[22] = wmers[23];
wmers[23] = wmers[24];
wmers[24] = wmers[25];
wmers[25] = wmers[26];
wmers[26] = wmers[27];
wmers[27] = wmers[28];
wmers[28] = wmers[29];
wmers[29] = wmers[30];
wmers[30] = wmers[31];
wmers[31] = wmers[32];
wmers[32] = wmers[33];
wmers[33] = wmers[34];
wmers[34] = wmers[35];
wmers[35] = wmers[36];
wmers[36] = wmers[37];
wmers[37] = wmers[38];
wmers[38] = wmers[39];
wmers[39] = wmers[40];
wmers[40] = wmers[41];
wmers[41] = wmers[42];
wmers[42] = wmers[43];
wmers[43] = wmers[44];
wmers[44] = wmers[45];
wmers[45] = wmers[46];
wmers[46] = wmers[47];
wmers[47] = wmers[48];
wmers[48] = wmers[49];
wmers[49] = wmers[50];
wmers[50] = wmers[51];
wmers[51] = wmers[52];
wmers[52] = wmers[53];
wmers[53] = wmers[54];
wmers[54] = wmers[55];
wmers[55] = wmers[56];
wmers[56] = wmers[57];
wmers[57] = wmers[58];
wmers[58] = wmers[59];
wmers[59] = wmers[60];
wmers[60] = wmers[61];
wmers[61] = wmers[62];
wmers[62] = wmers[63];
wmers[63] = wmers[64];
wmers[64] = wmers[65];
wmers[65] = wmers[66];
wmers[66] = wmers[67];
wmers[67] = wmers[68];
wmers[68] = wmers[69];
wmers[69] = wmers[70];
wmers[70] = wmers[71];
wmers[71] = wmers[72];
wmers[72] = wmers[73];
wmers[73] = wmers[74];
wmers[74] = wmers[75];
wmers[75] = wmers[76];
wmers[76] = wmers[77];
wmers[77] = wmers[78];
wmers[78] = wmers[79];
wmers[79] = wmers[80];
wmers[80] = wmers[81];
wmers[81] = wmers[82];
wmers[82] = wmers[83];
wmers[83] = wmers[84];
wmers[84] = wmers[84];
wmers[85] = wmers[86];
wmers[86] = wmers[87];
wmers[87] = wmers[88];
wmers[88] = wmers[89];
wmers[89] = wmers[90];
wmers[90] = wmers[91];
wmers[91] = wmers[92];
wmers[92] = wmers[93];
wmers[93] = wmers[94];
wmers[94] = wmers[95];
wmers[95] = wmers[96];
wmers[96] = wmers[97];
wmers[97] = wmers[98];
wmers[98] = wmers[99];
wmers[99] = wmers[100];
wmers[100] = wmers[101];
wmers[101] = wmers[102];
wmers[102] = wmers[103];
wmers[103] = wmers[104];
wmers[104] = wmers[105];
wmers[105] = wmers[106];
wmers[106] = wmers[107];
wmers[107] = wmers[108];
wmers[108] = wmers[109];
wmers[109] = wmers[110];
wmers[110] = wmers[111];
wmers[111] = wmers[112];
wmers[112] = wmers[113];
wmers[113] = wmers[114];
wmers[114] = c;
printf(">%d\n%s\n\n",j,wmers);
j++;
fputs(wmers,output);
}
return (EXIT_SUCCESS);
}
this is the content of exon11.txt.
CTGCTTGTGAATTTTCTGAGACGGATGTAACAAATACTGAACATCATCAACCCAGTAATAATGATTTGAACACCACTGAGAAGCGTGCAGCTGAGAGGCATCCAGAAAAGTATCAGGGTAGTTCTGTTTCAAACTTGCATGTGGAGCCATGTGGCACAAATACTCATGCCAGCTCATTACAGCATGAGAACAGCAGTTTATTACTCACTAAAGACAGAATGAATGTAGAAAAGGCTGAATTCTGTAATAAAAGCAAACAGCCTGGCTTAGCAAGGAGCCAACATAACAGATGGGCTGGAAGTAAGGAAACATGTAATGATAGGCGGACTCCCAGCACAGAAAAAAAGGTAGATCTGAATGCTGATCCCCTGTGTGAGAGAAAAGAATGGAATAAGCAGAAACTGCCATGCTCAGAGAATCCTAGAGATACTGAAGATGTTCCTTGGATAACACTAAATAGCAGCATTCAGAAAGTTAATGAGTGGTTTTCCAGAAGTGATGAACTGTTAGGTTCTGATGACTCACATGATGGGGAGTCTGAATCAAATGCCAAAGTAGCTGATGTATTGGACGTTCTAAATGAGGTAGATGAATATTCTGGTTCTTCAGAGAAAATAGACTTACTGGCCAGTGATCCTCATGAGGCTTTAATATGTAAAAGTGAAAGAGTTCACTCCAAATCAGTAGAGAGTAATATTGAAGACAAAATATTTGGGAAAACCTATCGGAAGAAGGCAAGCCTCCCCAACTTAAGCCATGTAACTGAAAATCTAATTATAGGAGCATTTGTTACTGAGCCACAGATAATACAAGAGCGTCCCCTCACAAATAAATTAAAGCGTAAAAGGAGACCTACATCAGGCCTTCATCCTGAGGATTTTATCAAGAAAGCAGATTTGGCAGTTCAAAAGACTCCTGAAATGATAAATCAGGGAACTAACCAAACGGAGCAGAATGGTCAAGTGATGAATATTACTAATAGTGGTCATGAGAATAAAACAAAAGGTGATTCTATTCAGAATGAGAAAAATCCTAACCCAATAGAATCACTCGAAAAAGAATCTGCTTTCAAAACGAAAGCTGAACCTATAAGCAGCAGTATAAGCAATATGGAACTCGAATTAAATATCCACAATTCAAAAGCACCTAAAAAGAATAGGCTGAGGAGGAAGTCTTCTACCAGGCATATTCATGCGCTTGAACTAGTAGTCAGTAGAAATCTAAGCCCACCTAATTGTACTGAATTGCAAATTGATAGTTGTTCTAGCAGTGAAGAGATAAAGAAAAAAAAGTACAACCAAATGCCAGTCAGGCACAGCAGAAACCTACAACTCATGGAAGGTAAAGAACCTGCAACTGGAGCCAAGAAGAGTAACAAGCCAAATGAACAGACAAGTAAAAGACATGACAGCGATACTTTCCCAGAGCTGAAGTTAACAAATGCACCTGGTTCTTTTACTAAGTGTTCAAATACCAGTGAACTTAAAGAATTTGTCAATCCTAGCCTTCCAAGAGAAGAAAAAGAAGAGAAACTAGAAACAGTTAAAGTGTCTAATAATGCTGAAGACCCCAAAGATCTCATGTTAAGTGGAGAAAGGGTTTTGCAAACTGAAAGATCTGTAGAGAGTAGCAGTATTTCATTGGTACCTGGTACTGATTATGGCACTCAGGAAAGTATCTCGTTACTGGAAGTTAGCACTCTAGGGAAGGCAAAAACAGAACCAAATAAATGTGTGAGTCAGTGTGCAGCATTTGAAAACCCCAAGGGACTAATTCATGGTTGTTCCAAAGATAATAGAAATGACACAGAAGGCTTTAAGTATCCATTGGGACATGAAGTTAACCACAGTCGGGAAACAAGCATAGAAATGGAAGAAAGTGAACTTGATGCTCAGTATTTGCAGAATACATTCAAGGTTTCAAAGCGCCAGTCATTTGCTCCGTTTTCAAATCCAGGAAATGCAGAAGAGGAATGTGCAACATTCTCTGCCCACTCTGGGTCCTTAAAGAAACAAAGTCCAAAAGTCACTTTTGAATGTGAACAAAAGGAAGAAAATCAAGGAAAGAATGAGTCTAATATCAAGCCTGTACAGACAGTTAATATCACTGCAGGCTTTCCTGTGGTTGGTCAGAAAGATAAGCCAGTTGATAATGCCAAATGTAGTATCAAAGGAGGCTCTAGGTTTTGTCTATCATCTCAGTTCAGAGGCAACGAAACTGGACTCATTACTCCAAATAAACATGGACTTTTACAAAACCCATATCGTATACCACCACTTTTTCCCATCAAGTCATTTGTTAAAACTAAATGTAAGAAAAATCTGCTAGAGGAAAACTTTGAGGAACATTCAATGTCACCTGAAAGAGAAATGGGAAATGAGAACATTCCAAGTACAGTGAGCACAATTAGCCGTAATAACATTAGAGAAAATGTTTTTAAAGAAGCCAGCTCAAGCAATATTAATGAAGTAGGTTCCAGTACTAATGAAGTGGGCTCCAGTATTAATGAAATAGGTTCCAGTGATGAAAACATTCAAGCAGAACTAGGTAGAAACAGAGGGCCAAAATTGAATGCTATGCTTAGATTAGGGGTTTTGCAACCTGAGGTCTATAAACAAAGTCTTCCTGGAAGTAATTGTAAGCATCCTGAAATAAAAAAGCAAGAATATGAAGAAGTAGTTCAGACTGTTAATACAGATTTCTCTCCATATCTGATTTCAGATAACTTAGAACAGCCTATGGGAAGTAGTCATGCATCTCAGGTTTGTTCTGAGACACCTGATGACCTGTTAGATGATGGTGAAATAAAGGAAGATACTAGTTTTGCTGAAAATGACATTAAGGAAAGTTCTGCTGTTTTTAGCAAAAGCGTCCAGAAAGGAGAGCTTAGCAGGAGTCCTAGCCCTTTCACCCATACACATTTGGCTCAGGGTTACCGAAGAGGGGCCAAGAAATTAGAGTCCTCAGAAGAGAACTTATCTAGTGAGGATGAAGAGCTTCCCTGCTTCCAACACTTGTTATTTGGTAAAGTAAACAATATACCTTCTCAGTCTACTAGGCATAGCACCGTTGCTACCGAGTGTCTGTCTAAGAACACAGAGGAGAATTTATTATCATTGAAGAATAGCTTAAATGACTGCAGTAACCAGGTAATATTGGCAAAGGCATCTCAGGAACATCACCTTAGTGAGGAAACAAAATGTTCTGCTAGCTTGTTTTCTTCACAGTGCAGTGAATTGGAAGACTTGACTGCAAATACAAACACCCAGGATCCTTTCTTGATTGGTTCTTCCAAACAAATGAGGCATCAGTCTGAAAGCCAGGGAGTTGGTCTGAGTGACAAGGAATTGGTTTCAGATGATGAAGAAAGAGGAACGGGCTTGGAAGAAAATAATCAAGAAGAGCAAAGCATGGATTCAAACTTAG
Given your question, you have tor ead your file char by char, not 2 char by 2 char :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char str[3] = "";
FILE *input = NULL;
if (!(input = fopen("string.txt", "r"))) {
// Error
return (EXIT_FAILURE);
}
if (!fgets(str,3,input)) {
// Error ?
}
printf("%s\n", str);
for(int c = fgetc(input); c != EOF; c = fgetc(input)) {
str[0] = str[1];
str[1] = c;
printf("%s\n", str);
}
return (EXIT_SUCCESS);
}
But a better way is to read the whole line (like with getline if you are on Linux) and simply display 2 char by 2 char ...
----------
Okay, so this is not really how SO work, but I'm gonna give you a piece of code.
But before that, some general advice :
Begin by testing your code with little data.
I mean, doing a test with an input of 10 char and a len of 2 is good, doing a test with an input of 3K+ char and a len of 100+ is insane. How do you expect to see if your code is good or bad ?
Cut your code in small function.
And do some "unitary" test on these function. That way, if something failed, you will be able to quickly found the bug without a debugger. If the function to retrieve data work but you end up with a wrong result, the maybe the problem lies when you construct your solution. If the result's construction work but you end up with bad value in your file, then maybe it's the writing function that is at fault. etc etc.
Use str* function.
Doing a manually shift to the left with a string of len 2 is ok, but doing it with a string of 115 is insane ! How many time did you spend on that ?
This is the piece of code that will take a string as an input an do what you want.
Since the array can have any length, the last value of the array is NULL.
It's like a string : to know the len, you search for the particuliar value '\0'. Here, it's not '\0' but rather NULL.
The result array and the string's result are good thanks to calloc that implictly set all array pointeur to NULL and all string content to \0.
That's why strncpy doesn't fail.
// TODO Do the description
char** Magic(const char * const input, size_t len)
{
char **array = NULL;
size_t arraySize = 0;
size_t inputLen = strlen(input);
char **returnFunction = NULL;
// Basic check (assert ?)
if (!len) {
// TODO Error message
goto END_FUNCTION;
}
// Calcul of the array length
if (len <= inputLen) {
arraySize = inputLen - len + 1;
}
// Last array value must be NULL in order to know the array length
++arraySize;
if (!(array = calloc(arraySize, sizeof(*array)))) {
// TODO Error message
goto END_FUNCTION;
}
for (size_t i = 0; i < arraySize - 1; ++i) {
if (!(array[i] = calloc(len + 1, sizeof(*array[i])))) {
// TODO Error message
goto END_FUNCTION;
}
strncpy(array[i], input + i, len);
}
returnFunction = array;
array = NULL;
/* GOTO */END_FUNCTION:
if (array) {
for (size_t i = 0; i < arraySize; ++i) {
free(array[i]);
}
free(array);
}
return (returnFunction);
}
int main(void)
{
char *input = "ABCDEFGIHJ";
size_t inputLen = strlen(input);
char **result = NULL;
for (size_t len = 1; len <= inputLen; ++len) {
// Get result
if (!(result = Magic(input, len))) {
// TODO error log ?
continue;
}
// Display result
printf("result for %zu len : ", len);
for (size_t i = 0; result[i]; ++i) {
printf("%s%s", result[i], result[i + 1] ? ", " : "\n");
}
// Free result
for (size_t i = 0; result[i]; ++i) {
free(result[i]);
}
free(result);
result = NULL;
}
return (EXIT_SUCCESS);
}
What's left up to you is : retrieving the data (and then use it in the "input" argument) and display/writing the data.
For both of these, DO A FUNCTION.
And test. lot of test.
Do not forget to check if there is a failure when calling a function.

c appending data at two pointer address

I have 2 functions that measure temperature and humidity. Each function returns a pointer to an address containing the data.
uint8_t * get_temperature(int *temperaturelen)
{
memset(htpbuff, 0, sizeof(htpbuff));
int temp_c = readTemperatureC();
htpbuff[0] = 'T';
htpbuff[1] = 'C';
htpbuff[2] = temp_c&0xff;
htpbuff[3] = (temp_c>>8)&0xff;
htpbuff[4] = (temp_c>>16)&0xff;
htpbuff[5] = (temp_c>>24)&0xff;
*temperaturelen = 6;
return (&htpbuff[0]);
}
uint8_t * get_humidity(int *humiditylen)
{
memset(htpbuff, 0, sizeof(htpbuff));
int humidity = readHumidity();
htpbuff[0] = 'R';
htpbuff[1] = '%';
htpbuff[2] = humidity&0xff;
htpbuff[3] = (humidity>>8)&0xff;
htpbuff[4] = (humidity>>16)&0xff;
htpbuff[5] = (humidity>>24)&0xff;
*humiditylen = 6;
return (&htpbuff[0]);
}
I call the functions like this:
tempptr = get_live_temperature(& temp_len);
humptr = get_live_humidity(& hum_len);
I want to appeand the data at each address into a single pointer so that the data at that address would look like TC1234R%1234.
How could I do this?
The reason why is I need to pass this new pointer to a function to transmit the data.
Thanks

How can you swap two fixed-size opencl-c arrays

I want to swap two arrays within an opencl-c call in that manner:
uint tmp_a[L] = {...};
uint tmp_b[L] = {...};
swap(tmp_a, tmp_b); // (tmp_a, tmp_b) <- (tmp_b, tmp_a)
I would like to do it just by swapping the base addresses of the two pointers, but unfortunatley
void swap(uint num_a[L], uint num_b[L]) {
uint* tmp = num_a;
num_a = num_b;
num_b = tmp;
}
doesn't work.
In reality I do it now this way:
for(int i = 0; i < L; ++i) {
const uint tmp = num_a[i];
num_a[i] = num_b[i];
num_b[i] = tmp;
}
I would like to reduce this unnecessary work.
An array is just a pointer to a chunk of memory. When you pass an array to a function (as in your code snippet), you are passing the value of that pointer to the function. Passing an argument to a function is much like an assignment operation. Consider this simple example:
void foo(uint arg[L])
{
arg = NULL;
}
uint bar[L] = {...};
foo(bar);
This is essentially the same as:
uint bar[L] = {...};
uint arg[L] = bar;
arg = NULL;
The pointer stored in bar hasn't been affected by the assignment that happened inside the foo function. If we did want to modify the pointer stored in bar inside the foo function, we should instead pass the address of bar:
void foo(uint *arg[L])
{
*arg = NULL;
}
uint bar[L] = {...};
foo(&bar);
Which is equivalent to:
uint data[L] = {...};
uint *arg[L] = &bar;
*arg = NULL;
This time, arg points to the memory that stores the data contained in bar, which is the pointer to the array values. This means that if we modify the stuff that arg points to, then we will also be modifying bar.
Applying this to your code snippet would look like this:
void swap(uint *num_a[L], uint *num_b[L])
{
uint *tmp = *num_a;
*num_a = *num_b;
*num_b = tmp;
}
...
uint tmp_a[L] = {...};
uint tmp_b[L] = {...};
swap(&tmp_a, &tmp_b); // (tmp_a, tmp_b) <- (tmp_b, tmp_a)

Converting a CFStringRef to char *

I am getting a CFStringRef out of a CFDictionaryRef using CFDictionaryGetValue.
I've been trying to convert the CFStringRef to a char* using CFStringGetCString or CFStringGetCStringPtr and they either return a NULL or it crashes.
Is there a way to do this? How?
Thank you.
EDIT: sample code:
SecStaticCodeRef staticCode;
CFDictionaryRef information;
SecCSFlags flags = kSecCSInternalInformation
| kSecCSSigningInformation
| kSecCSRequirementInformation
| kSecCSInternalInformation;
CFURLRef pathURL = NULL;
CFStringRef pathStr = NULL;
CFStringRef uniqueid;
char* str = NULL;
CFIndex length;
pathStr = CFStringCreateWithCString(kCFAllocatorDefault,
filename, kCFStringEncodingUTF8);
pathURL = CFURLCreateWithString(kCFAllocatorDefault, pathStr, NULL);
SecStaticCodeCreateWithPath(pathURL, kSecCSDefaultFlags, &staticCode);
SecCodeCopySigningInformation(staticCode, flags, &information);
uniqueid = (CFStringRef) CFDictionaryGetValue(information, kSecCodeInfoUnique);
// how do I convert it here to char *?
length = CFStringGetLength(uniqueid);
str = (char *)malloc( length + 1 );
CFStringGetCString(uniqueid, str, length, kCFStringEncodingUTF8);
printf("hash of signature is %s\n", str);
CFRelease(information);
CFRelease(staticCode);
From the Chapter 17 example code in iOS:PTL.
char * MYCFStringCopyUTF8String(CFStringRef aString) {
if (aString == NULL) {
return NULL;
}
CFIndex length = CFStringGetLength(aString);
CFIndex maxSize =
CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
char *buffer = (char *)malloc(maxSize);
if (CFStringGetCString(aString, buffer, maxSize,
kCFStringEncodingUTF8)) {
return buffer;
}
free(buffer); // If we failed
return NULL;
}
The resulting buffer must always be freed (which is why Copy is in the name). The linked example code also has a slightly faster version that uses a buffer you provide.
Another answer:
const char *cs = CFStringGetCStringPtr( cfString, kCFStringEncodingMacRoman ) ;
puts( cs ) ; // works
I can't find the reason why kCFStringEncodingUTF8 gives NULL, but kCFStringEncodingMacRoman seems to work fine.
There is an another one line solution for the same problem:
char * myCString = [(__bridge NSString *)myCfstring UTF8String];
Happy Coding :)
why not simply: printf("%s\n", CFStringGetCStringPtr(uniqueid, kCFStringEncodingUTF8));?
very simple:
CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
const char *path = CFStringGetCStringPtr(cfStrRef, encodingMethod);
From the String Programming Guide for Core Foundation Documentation. Here is how you get the Contents of a CFStringRef as a C String.
I have made a modification to it as so, this should do what you are looking for.
#include <CoreFoundation/CoreFoundation.h>
CFStringRef str;
//Removed CFRange
const char *bytes; //This is where the conversion result will end up.
str = CFSTR("You Want this String!\n"); //Changed this part
bytes = CFStringGetCStringPtr(str, kCFStringEncodingMacRoman);
if (bytes == NULL)
{
//Adding in getting the size
CFIndex stringLengthIndex = CFStringGetLength(str);
//Converted index (signed long ) to int
char localBuffer[(int) stringLengthIndex];
Boolean success;
success = CFStringGetCString(str, localBuffer, stringLengthIndex, kCFStringEncodingMacRoman);
}
//At this point the "bytes" variable should contain your C string copied from the provided CFStringRef

Resources