converting unsigned char * to jbytearray - c

I am trying to convert an unsigned char * to jbyteArray using the following code
int callWR(const char *name,unsigned char *ubuf)
{
jbyteArray bArray=env->NewByteArray(strlen((const char *)ubuf));
env->SetByteArrayRegion(bArray, 0, strlen((const char *)ubuf), (jbyte *)ubuf);
jstring tableName= (env)->NewStringUTF(name);
int status;
status=(int)(env)->CallStaticIntMethod(clsMC,midWRow,tableName,bArray);
env->DeleteLocalRef(bArray);
return status;
}
Is this the right method to pass a byte array argument through JNI to a java function? is type casting (jbyte *)ubuf an efficient way of converting a unsigned char * to jbyte *? The values that I am getting at the destination seem to be messed up. Help please.

Related

Segmentation fault error: How to access my sha256 function?

I want to get the hash value by putting the message value in the sha256 function. But I get a segmentation fault error.
This is my main function.
int main() {
unsigned char *digest;
unsigned char *message = "fsd";
unsigned int len=3;
sha256(message, len, digest);
printf("%s",digest); }
Below is sha256 function.
void sha256(const unsigned char *message, unsigned int len,
unsigned char *digest);
How should I access the function so that it doesn't throw an error?
digest needs to point somewhere. In your code digest is just an uninitialized poibter that points nowhere.
int main() {
unsigned char digest[some_length]; // the digest will be put here
unsigned char *message = "fsd";
unsigned int len=3;
sha256(message, len, digest);
printf("%s",digest);
}
where some_length is the length needed for the SHA256 digest, refer to the documentation of the sha256 function.
Also depending on what exactly sha256 does (you didn0't provide the code nor any documentation), printf("%s",digest); won't work because digest might contain unprintable characters.

Warning makes integer from pointer without a cast

expected ‘int’ but argument is of type ‘char *’
dont know how to correct, any suggestion
#include <stdio.h>
int my_strncmp(char const *s1, char const *s2, int n);
int main (int ac, char **ag) {
char result;
if (ac == 4) {
result = my_strncmp(ag[1], ag[2], ag[3]);
printf("%d\n", result);
}
return(0);
}
You need to convert ag[3] (of type char * / string) to an integer.
Have a look at strtol() and its brethren. With atoi() exists a simpler function, which however is not as robust and versatile. That is why I would recommend getting into the habit of using strtol() et al., always.
Sidenote, "n" parameters are usually made size_t (unsigned) instead of int. (Compare strncmp()). You'd use strtoul() then.
The last parameter of my_strncmp is defined as an int n, yet when it is called in main the third parameter is char * type
in here,
int my_strncmp(char const *s1, char const *s2, int n);
the last part is int n
result = my_strncmp(ag[1], ag[2], ag[3]);
but here what you are passing ag[3] is of type char
hope this helps..

assign pointer from argv in function

I am trying to assign a pointer correctly from the programs **argv. When I assign data in the main function it works fine, but when I attempt to place that logic into a separate function it does not.
What am I doing wrong here?
void parse_args(char *argv[ ], unsigned char *data, *data_len, *nprocs){
data = (unsigned char *)argv[1];
*data_len = strlen(argv[1]);
*nprocs = atoi(argv[2]);
}
int main(int argc, char **argv) {
unsigned char *data;
int data_len;
int nprocs;
// this doesnt work (for data)
parse_args(argv, data, &data_len, &nprocs)
// this works (for data)
data = (unsigned char *)argv[1];
}
This line
data = (unsigned char *)argv[1];
modifies a local copy of main's local data, because all parameters, including pointers, are passed by value. If you would like to modify data inside main, pass it by pointer (i.e. you need a pointer to pointer now):
void parse_args(char *argv[ ], unsigned char **data_ptr, int *nprocs) {
...
*(data_ptr) = (unsigned char *)argv[1];
...
}
your function needs to be passed a char * [] (which is equivalent to a char** in an argument specification). You shouldn't specify the type when calling a function, that should have given you a compiler error (char * is not to be used here!)
// this doesnt work (for data)
parse_args(char *argv, data, &data_len)
must be replaced by
parse_args(argv, data, &data_len)
So, next, you pass a pointer data , but you pass that pointer by value, i.e. your parse_args gets a nice copy of that pointer (which, technically, is just an address stored in a variable), and then you modify that copy. You might want to pass it like data_len:
void parse_args(char *argv[ ], unsigned char **data, *data_len, *nprocs){
..
parse_args(argv, &data, &data_len, &nprocs)
All in all, this doesn't seem to be a great attempt at argument parsing. There's lots of libraries out there to do that for you, and if you want to stay old-school, I'd recommend using gengetopt, which generates all the parsing code you need and has nice documentation.

How to handle data types?

I'm really facing a problem with data types conversions.
I'm making a GUI version of a program with GTK. To get entry_text string I need a const char* which obliges me to use this data type in the function below.
I want to convert it to string(char[]) and compiler keep giving me errors below :
Source code where errors come:
//....
char ret (const char *bd){
char c[100];
strcpy(c,bd);
return *c;
}
char encode(const char ebuf[],const char epass[]) {
char *buf=ret(ebuf);
char *pass=ret(epass);
//...
When I compile the code I get following errors (with g++):
codgui.cpp: In function ‘char encode(const char*, const char*)’:
codgui.cpp:36: error: invalid conversion from ‘char’ to ‘char*’
codgui.cpp:37: error: invalid conversion from ‘char’ to ‘char*’
Anyone have any clue on how to fix this?
Your ret function is only returning the first character in the local variable c. You want to return a char*, and you should never return the address of a local variable, so you will need to create it on the heap.
char* ret (const char *bd){
char *c = new char[100];
strcpy(c,bd);
return c;
}
char encode(const char ebuf[],const char epass[]) {
char *buf=ret(ebuf);
char *pass=ret(epass);

Reading audio rlp

I am tring to get sound samples from microphone through Fez Panda 2. I am using rlp to accomplish that. Here is my code:
int GHAL_AnalogIn_Read(unsigned char channel)
{
return ((*((int*)(ADC_DATA_BASE_ADDRESS) + channel)) >>8) & 0x3FF;
}
int ReadAudio(unsigned int *generalArray, void **args, unsigned int argsCount ,unsigned int *argSize)
{
unsigned char *buffer = (unsigned char*)args[0];
int buffer_lengh = argSize[0];
unsigned char channel = *(unsigned char*)args[1];
int i=0;
while(i<buffer_lengh)
{
buffer[i] = GHAL_AnalogIn_Read(channel);
i++;
RLPext->Delay(100);
}
return 0;
}
The problem is that I need float values not unsigned char because I'm performing fft on these sound samples. So I need modification that will provide me float values. Any ideas?
Have you got experience with C? Especially with the meaning of * and &? * means: get the value pointed by address. So void ** args says someting like 'get the value pointed by the value obtained from address'. void is used to freely input anything you like. As you can not put whole structures or objects in an argument, you provide the pointer (an address) to a structure or object. By using the * you obtain the value on the address of the argument.
In C you do not pass whole arrays in an argument, you pass on the address of the first index.
Now you could simply re-factor your function to be something like:
int ReadAudio(unsigned int *generalArray, float arg, unsigned int argsCount ,unsigned int *argSize)
But as void **args is pointing to a buffer now, I think you should know what operation you want to perform on the data collected. An analog read will always provide you with an integer, most ADC (analog - digital - converter) are 10-bit or so.
If a float is 4 bytes on a 32-bit system, you want to mangle your data (unsigned char *buffer) in a 4-byte boundary.
EDIT: I have overlooked this in the documentatio: Note: Parameter of all function in RLP code file must have format follow this:Note: Parameter of all function in RLP code file must have format follow this:. Just cast the buffer bytes to a float by 4 byte boundary and I think you will do fine.

Resources