KMatle Expert Licensed User Longtime User Jan 13, 2023 #1 I'm still a noob in C I have C: Copy size_t olen = 0; and later it is used as a pointer like &olen As far as I know this is a pointer to an unsigned int variable. Via b4r_main::_encrypted_len = olen; all works but I want to drop that extra step and use/cast the variable directly to a B4R variable. I tried (uint8_t*) and other things but it didn't work. Could someone help me out here? Thanks.
I'm still a noob in C I have C: Copy size_t olen = 0; and later it is used as a pointer like &olen As far as I know this is a pointer to an unsigned int variable. Via b4r_main::_encrypted_len = olen; all works but I want to drop that extra step and use/cast the variable directly to a B4R variable. I tried (uint8_t*) and other things but it didn't work. Could someone help me out here? Thanks.
C candide Active Member Licensed User Longtime User Jan 13, 2023 #2 to store a byte in B4R in a variable accessible in c++ by pointer for read/write: i think it can be done with ArrayByte in B4R: Dim datx(1) as byte datx(0) = 111 in wrapper c++ uint8_t * varx = datx->data; xxx = &varx[0]; ===> xxx will be a pointer to your variable in B4R and a modification under c++ will change B4R variable value i am not sure it is what you want ! Upvote 0
to store a byte in B4R in a variable accessible in c++ by pointer for read/write: i think it can be done with ArrayByte in B4R: Dim datx(1) as byte datx(0) = 111 in wrapper c++ uint8_t * varx = datx->data; xxx = &varx[0]; ===> xxx will be a pointer to your variable in B4R and a modification under c++ will change B4R variable value i am not sure it is what you want !
C candide Active Member Licensed User Longtime User Jan 15, 2023 #4 if you are using inline C, you can find several examples in this forum to modify a global variable B4R from inline C Inline C / C++ As in the other B4X tools you can embed C or C++ code in the project. Example: Sub Process_Globals Public Serial1 As Serial Private Result, N1, N2 As Int 'ignore End Sub Private Sub AppStart Serial1.Initialize(115200) Log("AppStart") N1 = 10 N2 = 20 RunNative("Add", Null)... www.b4x.com Upvote 0
if you are using inline C, you can find several examples in this forum to modify a global variable B4R from inline C Inline C / C++ As in the other B4X tools you can embed C or C++ code in the project. Example: Sub Process_Globals Public Serial1 As Serial Private Result, N1, N2 As Int 'ignore End Sub Private Sub AppStart Serial1.Initialize(115200) Log("AppStart") N1 = 10 N2 = 20 RunNative("Add", Null)... www.b4x.com
KMatle Expert Licensed User Longtime User Jan 18, 2023 #5 I wasn't precise enough. In inline C I use this function which returns &olen (which is size_t) C: Copy mbedtls_pk_encrypt( &pk, (uint8_t*)b4r_main::_decrypted->data, b4r_main::_decrypted_len, (uint8_t*)b4r_main::_encrypted->data, &olen,b4r_main::_encrypted->length, mbedtls_ctr_drbg_random, &ctr_drbg ) ) As you can see I can use global variables. Later I use C: Copy b4r_main::_encrypted_len = olen; to modify encrypted_len in globals (Public encrypted_len As Int) which works fine. Problem: I want to use b4r_main::_encrypted_len directly in the function (like the other values): C: Copy mbedtls_pk_encrypt( &pk, (uint8_t*)b4r_main::_decrypted->data, b4r_main::_decrypted_len, (uint8_t*)b4r_main::_encrypted->data, (uint8_t*)b4r_main::_encrypted_len,b4r_main::_encrypted->length, mbedtls_ctr_drbg_random, &ctr_drbg ) ) This throws an error: b4r_main.cpp:488:68: error: cannot convert 'uint8_t* {aka unsigned char*}' to 'size_t* {aka unsigned int*}' for argument '5' to 'int mbedtls_pk_encrypt(mbedtls_pk_context*, const unsigned char*, size_t, unsigned char*, size_t*, size_t, int (*)(void*, unsigned char*, size_t), void*)' Click to expand... So how do I cast it correctly? C: Copy b4r_main::_encrypted_len = olen; works, but I want to use it directly in the function. Upvote 0
I wasn't precise enough. In inline C I use this function which returns &olen (which is size_t) C: Copy mbedtls_pk_encrypt( &pk, (uint8_t*)b4r_main::_decrypted->data, b4r_main::_decrypted_len, (uint8_t*)b4r_main::_encrypted->data, &olen,b4r_main::_encrypted->length, mbedtls_ctr_drbg_random, &ctr_drbg ) ) As you can see I can use global variables. Later I use C: Copy b4r_main::_encrypted_len = olen; to modify encrypted_len in globals (Public encrypted_len As Int) which works fine. Problem: I want to use b4r_main::_encrypted_len directly in the function (like the other values): C: Copy mbedtls_pk_encrypt( &pk, (uint8_t*)b4r_main::_decrypted->data, b4r_main::_decrypted_len, (uint8_t*)b4r_main::_encrypted->data, (uint8_t*)b4r_main::_encrypted_len,b4r_main::_encrypted->length, mbedtls_ctr_drbg_random, &ctr_drbg ) ) This throws an error: b4r_main.cpp:488:68: error: cannot convert 'uint8_t* {aka unsigned char*}' to 'size_t* {aka unsigned int*}' for argument '5' to 'int mbedtls_pk_encrypt(mbedtls_pk_context*, const unsigned char*, size_t, unsigned char*, size_t*, size_t, int (*)(void*, unsigned char*, size_t), void*)' Click to expand... So how do I cast it correctly? C: Copy b4r_main::_encrypted_len = olen; works, but I want to use it directly in the function.
C candide Active Member Licensed User Longtime User Jan 22, 2023 #6 did you solve your problem ? from error, size_t is uint* , it should be better to have variable uint in B4R and cast can be (uint16_t*) Upvote 0
did you solve your problem ? from error, size_t is uint* , it should be better to have variable uint in B4R and cast can be (uint16_t*)
KMatle Expert Licensed User Longtime User Jan 22, 2023 #7 Thanks for your reply. I tried several things (uint/Int), cast to uint_16t* and even unsigned_int* or size_t*. Either it didn't compile (x cannot be casted to y) or it compiled and then I go a kernal panic/crash (memory violation). So I still use C: Copy b4r_main::_encrypted_len = olen; which works Upvote 0
Thanks for your reply. I tried several things (uint/Int), cast to uint_16t* and even unsigned_int* or size_t*. Either it didn't compile (x cannot be casted to y) or it compiled and then I go a kernal panic/crash (memory violation). So I still use C: Copy b4r_main::_encrypted_len = olen; which works
KMatle Expert Licensed User Longtime User Jan 22, 2023 #8 When I use C: Copy Public decrypted_len As UInt or Public decrypted_len As Int with Inline C: C: Copy ...b4r_main::_encrypted_len, (uint8_t*)b4r_main::_decrypted->data, [B](size_t*)b4r_main::_decrypted_len[/B],... It compiles but throws Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled. Click to expand... Upvote 0
When I use C: Copy Public decrypted_len As UInt or Public decrypted_len As Int with Inline C: C: Copy ...b4r_main::_encrypted_len, (uint8_t*)b4r_main::_decrypted->data, [B](size_t*)b4r_main::_decrypted_len[/B],... It compiles but throws Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled. Click to expand...