# How to convert UINT16 into two signed chars?

I’m doing this conversion is because from a network where the data being received is an array of unsigned char[8]. Every two bytes represent a UINT16 number. I want to send these bytes through a socket however, sockets require the chars to be signed (I’m working in little-endian).

Since it is impossible to convert a unsigned char to signed char I thought I could form a UINT16 number from the two unsigned chars and then decompose the number into two signed chars.

The code I have works in situations when the UINT16 number is between [0,127] and [256,65535], but when the value is between [128,255] the first char blows up to ~4294967295 and the UINT16 is now represented as ~65535. Any explanations/solutions?

uint16_t in = 129;
cout << "input ist" << in << endl;

char lo = in & 0xFF;
char hi = in >> 8;

printf("%u", lo);
cout << "n" << endl;
printf("%u", hi);
cout << "n" << endl;

uint16_t out = lo | uint16_t(hi) << 8;

cout << "out ist" << out << endl;


The error I get when I send unsigned char[] through the socket; if I use the following initialization I can use it as an appropriate argument for the send() function through the socket (winsock). Works just fine.

char buffer[8]
for (int i= 0; i <8; i++){
buffer[i] = 0x016; //random data
}
send(clientSocket, buffer, 8, 0);


But if initialize the buffer as following I get an error:

unsigned char buffer[8]
for (int i=0; i < 8;i++){
buffer[i] = 0x016; //again random data
}
send(clientSocket, buffer,8,0);


With the error stating:

int send (SOCKET, const char *, int, int)’: cannot convert argument 2 from unsigned char[8] to const char *

Stack Overflow Asked by the big pescado on February 22, 2021

1 Answers

## One Answer

[This is answered for C. OP has yet to clarify their needs for C versus C++ answers.]

To pass an array of unsigned char named buffer using the send function, which has an parameter of type of const char *, simply cast the array to the required type, as with:

send(socket, (const char *) buffer, length, flags);


In C, character types are used to deal with “raw bytes” of data (as well as being used to encode characters, an unfortunate conflation of meanings and uses), so you can send any type of data this way. The socket routines will not interpret the data according to any particular type; they will just transmit the bits without regard to any signedness. You can use the same cast without const for receiving data, as long as the data is written to the same type of object that it originally came from.

(There can be issues where the sending and receiving systems use different representations for various things, such as big-endian storage on one and little-endian storage on another or four bytes for long type on one and eight bytes on another or different padding and alignment in structures. These are separate issues unrelated to the transmission of raw data via sockets.)

(As noted by Kaz, modern APIs generally declare such parameters to be void * or const void *, and no cast is necessary when passing other object pointers to them. If you encountered some compiler warning message about this, you may be using an outdated API.)

Correct answer by Eric Postpischil on February 22, 2021

## Related Questions

### Android assistant – MediaBrowserService – Voice command – “Play x on appName”

0  Asked on November 4, 2021 by salah-hammouda

### Google fit API add multiple data type based on scope

1  Asked on November 4, 2021 by draw134

### Using serverless-tscpaths plugin with serverless-plugin-optimize gives import error with relative paths

1  Asked on November 4, 2021 by user3536652

### Compiled autohotkey file does not run, uncompiled works

0  Asked on November 4, 2021 by user1592380

### Android unique splash screen : how to make it fill screen?

5  Asked on November 4, 2021 by arnaudambro

### ASP.NET parameter/variable in both app Settings and app Build Events

1  Asked on November 4, 2021 by mathew-alden

### .Net Core 3.1 and Angular compile verbosity in Visual Studio

3  Asked on November 4, 2021 by adriaan-davel

### Getting UserDefaults is not returning anything

2  Asked on November 4, 2021

### Downloading SetupTools in Maximo/Jython fails with HTTP error 403 SSL is required

1  Asked on November 4, 2021 by maximo-wiki

### Databricks connect & PyCharm & remote SSH connection

3  Asked on November 4, 2021 by george-sotiropoulos

### Add Brightness and Contrast to Cropper JS without Caman JS?

1  Asked on November 4, 2021

### CallScreeningService – Disconnect call after few seconds – Android 10

1  Asked on November 4, 2021 by rohan-kandwal

### Can developers of gsuite addons view the data in your sheets / docs?

3  Asked on November 4, 2021

### How to filter rows in both parent and nested tables, using reactable?

0  Asked on November 4, 2021 by george-dontas

### how to update select options based on previous selected values in jQuery DataTables

1  Asked on November 4, 2021

### Why is my App crashing when I drag an element?

0  Asked on November 4, 2021 by lonelegend

### How to configure Android Studio to include configuration folder containing pom.xml in project?

0  Asked on November 4, 2021 by jisasava

### HTTP DELETE Works From Browser But Not From Postman or IOS App

1  Asked on November 4, 2021 by tj-dalessandro

### flask_sqlalchemy: error with __setattr__ to DefaultMeta

1  Asked on November 4, 2021 by david-wolever

### Why does C# System.Decimal (decimal) “waste” bits?

2  Asked on November 4, 2021

### Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.