326 questions
5
votes
1
answer
127
views
Buffer of chars, unsigned chars or a void* - do we have contradictions in the C++ standard?
This is a follow-up on:
Why is it UB to pass a char array as the argument of placement new?
The spec says the following for creating an object using an existing storage - [intro.object#3]:
If a ...
2
votes
0
answers
60
views
Node.js FFI-NAPI: TypeError - writePointer: Buffer instance expected
I’m using ffi-napi and ref-napi in Node.js to call a C function, but I encountered the following error:
TypeError: error setting argument 3 - writePointer: Buffer instance expected as third argument
...
2
votes
1
answer
101
views
Why does my attempt to convert QString to unsigned char add random characters? [closed]
I have a QString which stores a hashed input via the line
hashedPass = QCryptographicHash::hash(input.toLocal8Bit(), QCryptographicHash::Sha256).toHex()
I am trying to convert this code to an ...
2
votes
1
answer
111
views
char * vs unsigned char *
So I was playing around with char* and unsigned char* pointers. And I came across this issue:
Code:
#include <stdio.h>
void func(unsigned int max) {
unsigned int* intptr = &max;
...
0
votes
1
answer
50
views
Select specific pixels of an image
I'm using C and stb_image, and image data is stored in an unsigned char* where a group of 4 elements represents a pixel, with r, g, b, and a channels respectively. I'm trying to get pixels of part of ...
0
votes
2
answers
116
views
Differences in Casting: char vs. unsigned char
In this function, why do we need to cast with unsigned char? Can't we cast with char and get the same result since both have a range of "255"? Why choose unsigned char?
Suppose there is no ...
0
votes
2
answers
170
views
Why do I need to Bitwise AND with 1 the shift righted char in binary to get the correct binary value?
This is the exercise that I'm doing:
Assignment name : print_bits
Expected files : print_bits.c
Allowed functions: write
Write a function that takes a byte, and prints it in binary WITHOUT A ...
0
votes
1
answer
67
views
why is the output of this question is 266 -1 10 in turbo c compiler?
int main(){
int a, b;
char *cp;
a = 511;
cp = &a;
b = *cp;
*cp = 10;
printf("%d %d %d", a,b,*cp);
return 0;
}
The output for the above code is 266 -1 10
...
3
votes
3
answers
1k
views
Printf performing implicit casting
In the below code snippet, why do we get an output of 600? Two questions will help me understand the behavior.
I specified the type for variable b to be uint8_t, why doesn't the math operation ...
5
votes
1
answer
145
views
Unexpected behaviour on underflow of unsigned char
The following program:
#include <iostream>
#include <string>
int main ()
{
unsigned char result1 {0};
unsigned char result2 {0};
result1 = (result1 - 1) % 8;
result2 = ...
0
votes
1
answer
776
views
nlohmann Json not working with std::array<unsinged char, SIZE>
I have the below program to convert my class into JSON object using Nlohmann JSON, When I used the std::array<unsigned char, SIZE> it gave an error, but when I used the std::array<char, SIZE&...
0
votes
1
answer
659
views
C++ make unsigned char * buffer from string [duplicate]
I have this string.
char * data = "A1B2C3D4E5F6A7B8";
Here each A1 B2 C3 D4 E5 F6 A7 B8 will be bytes for unsigned char* buffer.
My concern is how to convert the data into unsigned char* ...
0
votes
1
answer
172
views
Usage of int instead of char [duplicate]
I've got a copy of the book "The C Programming Language", and in one of the first chapters it shows a short example of a code that copies characters from stdin to stdout. Here is the code:
...
0
votes
1
answer
451
views
Bitwise operator with taking input from user
#include<stdio.h>
int main()
{
unsigned char a,b;
printf("Enter first number : ");
scanf("%d",&a);
printf("Enter 2nd number : ");
...
-1
votes
1
answer
203
views
convert struct to unsigned char through overloaded operator << and >> (see update)
I have this struct with 2 attributes (one char and one int, for a memory usage of 3 bytes:
struct Node {
char data;
int frequency;
}
I try overload the operators << and >> for ...
-5
votes
1
answer
701
views
How come the output of this C program is 1?
#include <stdio.h>
int main() {
unsigned char c = (int) 0.54;
for(; c++; printf("%d", c));
printf("%d", c);
return 0;
}
And when I run the program the ...
0
votes
1
answer
62
views
Caveats reinterpret_cast'ing char* to unsigned char*?
I'm trying to fit my C++ app to a C-API. The API in question is mbed_tls which contains a base64 decoder:
int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *...
0
votes
0
answers
295
views
How to store unsigned int value to unsigned char variable?
Suppose I have
unsigned int x = 0x12a
unsigned char y = 0x000
How can I make x = y? When I try to assign x to y, i get y = 0x02a
1
vote
1
answer
783
views
How to read an Unsigned Char image file to Python?
I have an text file called with the extension '.image', at the top of the file is the following:
Image Type: unsigned char
Dimension: 2
Image Size: 512 512
Image Spacing: 1 1
Image Increment: 1 1
...
-1
votes
3
answers
92
views
Char addition does not have expected result in C
Can someone explain me how is this at the end
a=?,b=?,c=-124
and not
a=?,b=?,c=132
This is code:
#include <stdio.h>
int main() {
char a = 'D', b='C', c='A';
a = c + 'D';
b = a + b + 'A'...
2
votes
5
answers
8k
views
What does *= do?
Hey I am kinda new to C and I wanted to ask why this prints out 4 instead of 260?
#include <stdio.h>
int main()
{
unsigned char x = 130;
x *= 2;
printf("%d\n", x);
}
1
vote
1
answer
124
views
Type conversion issues unsigned char, string, hex
The following code has several issues - for which I'm kindly asking for your input / corrections / feedback - of various types: first, I can't get it to do what I'd like to, namely pass it a "...
1
vote
0
answers
937
views
error: narrowing conversion of ‘-58’ from ‘int’ to ‘char’ [-Wnarrowing]
I'm trying to compile https://github.com/jbeder/yaml-cpp on a Raspberry Pi, and I'm getting this error:
/home/pi/yaml-cpp/test/binary_test.cpp:11:38: error: narrowing conversion of ‘-58’ from ‘int’ to ...
0
votes
0
answers
84
views
How can I get unsigned char array? [duplicate]
I writing a function that will print out hexadecimal inside unsigned char array. But I can't get the size of the array. I tried using sizeof() but it seems to print out the size of the type which is 4....
1
vote
1
answer
946
views
How to convert unsigned char to unsigned int in c++?
I have the following piece of code:
const unsigned char *a = (const unsigned char *) input_items;
input_items is basically the contents of a binary file.
Now a[0] = 7, and i want to convert this ...
0
votes
0
answers
28
views
Signed range equilance [duplicate]
Can anyone please tell me that why in signed char range -128 is equal to +128, -127 is equal to +129 and so on. Although in signed range 0 represents positive values most significant bit 1 reserved ...
0
votes
2
answers
140
views
Reading information stored in header array when reading BMP file in c
I am trying to learn image processing using c.
#include <stdio.h>
int main(){
FILE *streamIn;
streamIn=fopen("lena512.bmp","r");
//read imageHeader and ...
0
votes
1
answer
277
views
C++ How do I modify code to send Bytes out of a serial port in the range 0x00 to 0xFF in code that only compiles for the range 0x00 to 0x7F
I have inherited some C++ code that sets up a serial com port in Windows. The code contains the following declaration and method, which I have simplified for the purposes of this question.
std::queue&...
3
votes
2
answers
1k
views
C++ Creating GUID from unsigned char array
I have a function that takes as input an array of unsigned char (16 values exactly) and creates a GUID (from GUID struct) by parsing all values to hexadecimal and passing a guid-formatted string to ...
1
vote
1
answer
715
views
unsigned char in C not working as expected
Since unsigned char represents 0 - 255 and the extended ascii code for 'à' is 133, I expected the following C code to print 133
unsigned char uc;
uc='à';
printf("%hhu \n",uc);
Instead, ...
1
vote
0
answers
36
views
enum size and initialization of variable [duplicate]
Even setting the size of an enum is not enough to be able to assign it to a variable of the same size (http://coliru.stacked-crooked.com/a/edd287393f172abd):
#include <iostream>
#include <...
1
vote
1
answer
314
views
Unsigned char out of bounds for value 247?
I’m trying to cast the value 247 to a char in C++ (Visual Studio).
I’m getting a Debug Assertion Failed error, saying unsigned(c+1) should be less than or equal to 256. I don’t understand where this ...
0
votes
0
answers
319
views
How to scanf input to 256 bit unsigned char array for AES key
I am attempting to prompt user input for a string of 32 characters (256 bits) to be read into an unsigned char array. I plan to use this unsigned char array for a 256 bit AES encryption key using ...
1
vote
2
answers
2k
views
In Python, how can I convert an integer between 0 and 255 to a single unsigned byte? [duplicate]
My laptop runs a Python program that reads a joystick to send a speed command to an Arduino that in turn controls the motor on a toy car. To do that the program converts an integer with values between ...
-2
votes
1
answer
997
views
How to convert unsigned long long int into unsigned char*?
Say for instance I have this unsigned long long int:
unsigned long long int test_int = 0xcfffedefcfffeded; // 14987959699154922989
How could I convert test_int into the following unsigned char* array:...
0
votes
4
answers
3k
views
How to convert unsigned char* to unsigned long long int?
Does anyone know of a library that provides a function that performs this logic or perhaps a method to perform this logic?
I'm trying to convert:
unsigned char test[] = "\x00\x00\x56\x4b\x7c\x8a\...
1
vote
1
answer
151
views
Should I use signed char for characters with negative values if I target portability?
A char data type is not guaranteed to be signed, which means on some implementations it is signed, and on others it is unsigned. So why do people always ignore the signed qualifier?
char c = 'C'; // ...
-1
votes
1
answer
393
views
Appending an unsigned char to a vector changes its value
I have the following 2D vector:
vector<vector<unsigned char>> dates;
I also have the following unsigned char array:
unsigned char date[3] = {1, 18, 108};
When I push_back this array to ...
0
votes
1
answer
647
views
Pass and receive raw byte data via an unsigned char* function parameters in C++
I have a 3rd party x.dll and an export function which is named as "GenerateKeyEx-in C". I do not have any additional info such as .lib, header etc. I have found the function parameters from ...
0
votes
2
answers
351
views
Unsigned char out of range
I am trying to figure out, how to use an unsigned char type of a variable inside a for loop, while not "breaking" out of range for unsigned char, which can vary form 0 to 255.
main(void) ...
3
votes
1
answer
315
views
How to convert elements in const unsigned char array to char
I am trying to iterate over a single const unsigned char array and assign/convert each element to a new char array using typecasting, every thread I've read suggests using typecasting however it's not ...
1
vote
1
answer
553
views
storing 0 in a unsigned char array
I have a unsigned char array like this:
unsigned char myArr[] = {100, 128, 0, 32, 2, 9};
I am using reinterpret_cast to covert it to const char* as I have to pass a const char* to a method. This ...
0
votes
0
answers
53
views
Comparison between signed and unsigned [duplicate]
int main(){
char *mem = malloc(sizeof(char)*10);
for(size_t i = 0; i < 10; i++){
mem[i] = 0xFF;
}
for (size_t i = 0; i < 10; i++){
if(mem[i] == 0xFF){
...
0
votes
5
answers
797
views
A problem with the behaviour of scanf when data type is unsigned char
So I have no idea why this behavior is observed:
#include <stdio.h>
int main()
{
unsigned char c;
char d;
scanf("%hhu", &d);
printf("%c\n", d);
...
0
votes
1
answer
837
views
Storing hexadecimal representation of int at std::vector of unsigned char
In this code:
vector<unsigned char> result;
int n;
unsigned char c;
c = (n >> 24) & 0xFF;
result.push_back(c);
c = (n >> 16) & 0xFF;
result.push_back(c);
c = ...
2
votes
2
answers
1k
views
shifting an unsigned char by more than 8 bits
I'm a bit troubled by this code:
typedef struct _slink{
struct _slink* next;
char type;
void* data;
}
assuming what this describes is a link in a file, where data is 4bytes long ...
1
vote
0
answers
141
views
CPP: Convert unsigned char* to std::vector<unsigned char> [duplicate]
I have an array of unsigned chars ( unsigned char *), how can I convert it to a vector of unsigned chars? ( std::vector<unsigned char> )?
1
vote
1
answer
703
views
Opening a .pak file using fopen and making that file apply to a const unsigned char* (C++)
I'm trying to upload a file (.pak file) using fopen, read it and make it equal blockStorage.
What I'm doing right now doesn't work, I think because pfile.data() doesn't even exist (it's left over from ...
0
votes
1
answer
332
views
C++ unsigned char *{varName}; -> C#
I have DLL written in C++. I need to use it in my C# code
I am trying to make such object of C++ in C#:
typedef struct {
int width;
int height;
int stride;
unsigned char *pixels;} FIS_Image;
Does ...
3
votes
2
answers
679
views
What type of cast is suitable to convert from unsigned char* to char*?
When I write data to and from a buffer to save to a file I tend to use std::vector<unsigned char>, and I treat those unsigned chars just as bytes to write anything into, so:
int sizeoffile = 16;...