Converting 4bytes to signed and unsigned ints












0















I want to convert a four-byte string to either int32 or uint32 in c++.
This answer helped me to write the following code:



#include <string>
#include <iostream>
#include <stdint.h>

int main()
{
std::string a("xaax00x00xaa", 4);

int u = *(int *) a.c_str();
int v = *(unsigned int *) a.c_str();
int x = *(int32_t *) a.c_str();
int y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;

return 0;
}


However the outputs are all the same, where the int (or int32_t) values are correct, but the unsigned ones are wrong. Why does not the unsigned conversions work?



// output
int: -1442840406
uint: -1442840406
int32_t: -1442840406
uint32_t: -1442840406


Pythons struct.unpack, gives the right conversion



In [1]: import struct
In [2]: struct.unpack("<i", b"xaax00x00xaa")
Out[2]: (-1442840406,)
In [3]: struct.unpack("<I", b"xaax00x00xaa")
Out[3]: (2852126890,)


I would also like a similar solution to work for int16 and uint16, but first things first, since I guess an extension would be trivial if I manage to solve this problem.










share|improve this question


















  • 2





    Because you're placing them all in (signed) int variables.

    – Rotem
    Nov 24 '18 at 19:45













  • Ahhh! How stupid of me... Been pulling my hair for hours. Thanks.

    – user10668188
    Nov 24 '18 at 19:49
















0















I want to convert a four-byte string to either int32 or uint32 in c++.
This answer helped me to write the following code:



#include <string>
#include <iostream>
#include <stdint.h>

int main()
{
std::string a("xaax00x00xaa", 4);

int u = *(int *) a.c_str();
int v = *(unsigned int *) a.c_str();
int x = *(int32_t *) a.c_str();
int y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;

return 0;
}


However the outputs are all the same, where the int (or int32_t) values are correct, but the unsigned ones are wrong. Why does not the unsigned conversions work?



// output
int: -1442840406
uint: -1442840406
int32_t: -1442840406
uint32_t: -1442840406


Pythons struct.unpack, gives the right conversion



In [1]: import struct
In [2]: struct.unpack("<i", b"xaax00x00xaa")
Out[2]: (-1442840406,)
In [3]: struct.unpack("<I", b"xaax00x00xaa")
Out[3]: (2852126890,)


I would also like a similar solution to work for int16 and uint16, but first things first, since I guess an extension would be trivial if I manage to solve this problem.










share|improve this question


















  • 2





    Because you're placing them all in (signed) int variables.

    – Rotem
    Nov 24 '18 at 19:45













  • Ahhh! How stupid of me... Been pulling my hair for hours. Thanks.

    – user10668188
    Nov 24 '18 at 19:49














0












0








0








I want to convert a four-byte string to either int32 or uint32 in c++.
This answer helped me to write the following code:



#include <string>
#include <iostream>
#include <stdint.h>

int main()
{
std::string a("xaax00x00xaa", 4);

int u = *(int *) a.c_str();
int v = *(unsigned int *) a.c_str();
int x = *(int32_t *) a.c_str();
int y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;

return 0;
}


However the outputs are all the same, where the int (or int32_t) values are correct, but the unsigned ones are wrong. Why does not the unsigned conversions work?



// output
int: -1442840406
uint: -1442840406
int32_t: -1442840406
uint32_t: -1442840406


Pythons struct.unpack, gives the right conversion



In [1]: import struct
In [2]: struct.unpack("<i", b"xaax00x00xaa")
Out[2]: (-1442840406,)
In [3]: struct.unpack("<I", b"xaax00x00xaa")
Out[3]: (2852126890,)


I would also like a similar solution to work for int16 and uint16, but first things first, since I guess an extension would be trivial if I manage to solve this problem.










share|improve this question














I want to convert a four-byte string to either int32 or uint32 in c++.
This answer helped me to write the following code:



#include <string>
#include <iostream>
#include <stdint.h>

int main()
{
std::string a("xaax00x00xaa", 4);

int u = *(int *) a.c_str();
int v = *(unsigned int *) a.c_str();
int x = *(int32_t *) a.c_str();
int y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;

return 0;
}


However the outputs are all the same, where the int (or int32_t) values are correct, but the unsigned ones are wrong. Why does not the unsigned conversions work?



// output
int: -1442840406
uint: -1442840406
int32_t: -1442840406
uint32_t: -1442840406


Pythons struct.unpack, gives the right conversion



In [1]: import struct
In [2]: struct.unpack("<i", b"xaax00x00xaa")
Out[2]: (-1442840406,)
In [3]: struct.unpack("<I", b"xaax00x00xaa")
Out[3]: (2852126890,)


I would also like a similar solution to work for int16 and uint16, but first things first, since I guess an extension would be trivial if I manage to solve this problem.







c++ int






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 19:38









user10668188user10668188

193




193








  • 2





    Because you're placing them all in (signed) int variables.

    – Rotem
    Nov 24 '18 at 19:45













  • Ahhh! How stupid of me... Been pulling my hair for hours. Thanks.

    – user10668188
    Nov 24 '18 at 19:49














  • 2





    Because you're placing them all in (signed) int variables.

    – Rotem
    Nov 24 '18 at 19:45













  • Ahhh! How stupid of me... Been pulling my hair for hours. Thanks.

    – user10668188
    Nov 24 '18 at 19:49








2




2





Because you're placing them all in (signed) int variables.

– Rotem
Nov 24 '18 at 19:45







Because you're placing them all in (signed) int variables.

– Rotem
Nov 24 '18 at 19:45















Ahhh! How stupid of me... Been pulling my hair for hours. Thanks.

– user10668188
Nov 24 '18 at 19:49





Ahhh! How stupid of me... Been pulling my hair for hours. Thanks.

– user10668188
Nov 24 '18 at 19:49












1 Answer
1






active

oldest

votes


















0














You need to store the unsigned values in an unsigned variables and it will work:



#include <string>
#include <iostream>
#include <stdint.h>

int main()
{
std::string a("xaax00x00xaa", 4);

int u = *(int *) a.c_str();
unsigned int v = *(unsigned int *) a.c_str();
int32_t x = *(int32_t *) a.c_str();
uint32_t y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;

return 0;
}




When you cast the value to unsigned and then store it in a signed variable, the compiler plays along. Later, when you print the signed variable, the compiler generates code to print a signed variable output.






share|improve this answer
























  • Great! The biggest risk of being lazy and just copy-paste rows... Thanks!

    – user10668188
    Nov 24 '18 at 20:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461727%2fconverting-4bytes-to-signed-and-unsigned-ints%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to store the unsigned values in an unsigned variables and it will work:



#include <string>
#include <iostream>
#include <stdint.h>

int main()
{
std::string a("xaax00x00xaa", 4);

int u = *(int *) a.c_str();
unsigned int v = *(unsigned int *) a.c_str();
int32_t x = *(int32_t *) a.c_str();
uint32_t y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;

return 0;
}




When you cast the value to unsigned and then store it in a signed variable, the compiler plays along. Later, when you print the signed variable, the compiler generates code to print a signed variable output.






share|improve this answer
























  • Great! The biggest risk of being lazy and just copy-paste rows... Thanks!

    – user10668188
    Nov 24 '18 at 20:45
















0














You need to store the unsigned values in an unsigned variables and it will work:



#include <string>
#include <iostream>
#include <stdint.h>

int main()
{
std::string a("xaax00x00xaa", 4);

int u = *(int *) a.c_str();
unsigned int v = *(unsigned int *) a.c_str();
int32_t x = *(int32_t *) a.c_str();
uint32_t y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;

return 0;
}




When you cast the value to unsigned and then store it in a signed variable, the compiler plays along. Later, when you print the signed variable, the compiler generates code to print a signed variable output.






share|improve this answer
























  • Great! The biggest risk of being lazy and just copy-paste rows... Thanks!

    – user10668188
    Nov 24 '18 at 20:45














0












0








0







You need to store the unsigned values in an unsigned variables and it will work:



#include <string>
#include <iostream>
#include <stdint.h>

int main()
{
std::string a("xaax00x00xaa", 4);

int u = *(int *) a.c_str();
unsigned int v = *(unsigned int *) a.c_str();
int32_t x = *(int32_t *) a.c_str();
uint32_t y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;

return 0;
}




When you cast the value to unsigned and then store it in a signed variable, the compiler plays along. Later, when you print the signed variable, the compiler generates code to print a signed variable output.






share|improve this answer













You need to store the unsigned values in an unsigned variables and it will work:



#include <string>
#include <iostream>
#include <stdint.h>

int main()
{
std::string a("xaax00x00xaa", 4);

int u = *(int *) a.c_str();
unsigned int v = *(unsigned int *) a.c_str();
int32_t x = *(int32_t *) a.c_str();
uint32_t y = *(uint32_t *) a.c_str();
std::cout << a << std::endl;
std::cout << "int: " << u << std::endl;
std::cout << "uint: " << v << std::endl;
std::cout << "int32_t: " << x << std::endl;
std::cout << "uint32_t: " << y << std::endl;

return 0;
}




When you cast the value to unsigned and then store it in a signed variable, the compiler plays along. Later, when you print the signed variable, the compiler generates code to print a signed variable output.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 19:46









John MurrayJohn Murray

869514




869514













  • Great! The biggest risk of being lazy and just copy-paste rows... Thanks!

    – user10668188
    Nov 24 '18 at 20:45



















  • Great! The biggest risk of being lazy and just copy-paste rows... Thanks!

    – user10668188
    Nov 24 '18 at 20:45

















Great! The biggest risk of being lazy and just copy-paste rows... Thanks!

– user10668188
Nov 24 '18 at 20:45





Great! The biggest risk of being lazy and just copy-paste rows... Thanks!

– user10668188
Nov 24 '18 at 20:45




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461727%2fconverting-4bytes-to-signed-and-unsigned-ints%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin