C: Converting unsigned char array to signed int (vice versa)

Doe Joe

I'm trying to convert an unsigned char array buffer into a signed int (vice versa).

Below is a demo code:

int main(int argv, char* argc[])
{
    int original = 1054;
    unsigned int i = 1054;
    unsigned char c[4];
    int num;

    memcpy(c, (char*)&i, sizeof(int));

    //num  = *(int*) c;                          // method 1 get
    memcpy((char *)&num, c, sizeof(int));        // method 2 get
    printf("%d\n", num);

    return 0;
}

1) Which method should I use to get from unsigned char[] to int?

method 1 get or method 2 get? (or any suggestion)

2) How do I convert the int original into an unsigned char[]?

I need to send this integer via a buffer that only accepts unsigned char[]

Currently what i'm doing is converting the int to unsigned int then to char[], example :

int g = 1054;
unsigned char buf[4];
unsigned int n;
n = g;
memcpy(buf, (char*)&n, sizeof(int));

Although it works fine but i'm not sure if its the correct way or is it safe?

PS. I'm trying to send data between 2 devices via USB serial communication (between Raspberry Pi & Arduino)

Grzegorz Szpetkowski

You could store both int (or unsigned int) and unsigned char array as union. This method is called type punning and it is fully sanitized by standard since C99 (it was common practice earlier, though). Assuming that sizeof(int) == 4:

#include <stdio.h>

union device_buffer {
    int i;
    unsigned char c[4];
};

int main(int argv, char* argc[])
{
    int original = 1054;

    union device_buffer db;
    db.i = original;

    for (int i = 0; i < 4; i++) {
        printf("c[i] = 0x%x\n", db.c[i]);
    }
}

Note that values in array are stored due to byte order, i.e. endianess.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting unsigned char to int and short

Casting a bitset to unsigned char, and vice versa

Swift converting signed array of Int [int8] to unsigned array of Int [UInt8]

C Converting Char array (strings) into int array

Convert from char array to 16bit signed int and 32bit unsigned int

C# Signed & Unsigned Integral to Big Endian Byte Array, and vice versa using Bitwise way with "best" performance

Difference between in signed and unsigned char in c

How to convert from stringstream to unsigned char vector and vice versa?

Converting String to Byte Array and vice versa

Type punning a positive signed integer into an unsigned (and vice versa)

Converting unsigned char array to signed short in C

Converting Characters to ASCII Code & Vice Versa In C++/CLI

Typecasted signed int converting like unsigned int

Cast signed char to unsigned int in C

converting member variables in a Java class to an object array and vice versa

Casting Parameter Variables from int to char and vice-versa

signed or unsigned int in c++

Converting unsigned char array to int

Is a type cast necessary while converting between signed int and unsigned int?

Is resolving char between signed and unsigned int unspecified?

How to derive signed type from unsigned type generic parameter, and vice-versa?

ByteBuffer - convert long to char array and vice versa

converting char array to int using c++

Converting a signed int32 to an unsigned int64

Java - Converting String from Scanner to int and vice versa

Why char is unsigned and int is signed by default?

Converting char to binary strings and vice-versa

Converting an unsigned char array to jstring

Is converting from unsigned char to signed char and vice versa in C89 well defined?