[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

convert int to uint32_t

Posted on 2009-02-20
16
Medium Priority
?
1,148 Views
Last Modified: 2012-05-06
as the title says it all, how can I do this?
0
Comment
Question by:kuntilanak
  • 5
  • 5
  • 2
  • +2
16 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 23697269
By casting - you can either use a C-style cast like

int i = 42;
uint32_t u = (uint32_t) i;

or a C++-style cast

int i =42;
uint32_t u = static_cast<uint32_t>(i);

See also this tutorial on typecasting in general: http://www.cplusplus.com/doc/tutorial/typecasting.html and also http://www.cprogramming.com/tutorial/lesson11.html
0
 

Author Comment

by:kuntilanak
ID: 23697396
I tried htol and it works
0
 
LVL 86

Expert Comment

by:jkr
ID: 23697414
'htol()'? You mean 'htonl()'? But that wan would convert the value to network byte order...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:cup
ID: 23697540
Note that an int has negative numbers but a uint32_t doesn't.  If you try converting a negative number, you will get the binary equivalent which may not be quite what you might expect.  eg -1 will become 0xFFFFFFFF.

As jkr says, htonl would convert int to network byte order i.e. on some machines, it does absolutely nothing, on others it byte swaps.  You might find that it works on SunOS but when you take it to a Windows machine, everything goes wrong.  Stick to the casting: it is a lot safer than playing with hton and ntoh functions (host to network or network to host).
0
 

Author Comment

by:kuntilanak
ID: 23697731
as I tried to use casting and it doesn't work...
0
 
LVL 86

Expert Comment

by:jkr
ID: 23698268
OK, how exactly were you trying to use these casts? (just to be sure)

And what is the scenario/setup/background?
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 23698850
did you try Convert.ToUInt32 (intVariable);
0
 

Author Comment

by:kuntilanak
ID: 23699047
I tried something like this:

int i = 42;
uint32_t u = (uint32_t) i;
0
 
LVL 11

Expert Comment

by:cup
ID: 23699493
Does it come up with a compilation error or does it display something completely different when you print it?

printf ("%u\n", u);

should have given you 42.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 23699943
did my suggestion work?
Convert.ToInt32()
0
 

Author Comment

by:kuntilanak
ID: 23700354
it just gives me a largee number
0
 
LVL 86

Expert Comment

by:jkr
ID: 23700538
Acutally,

uint32_t u = (uint32_t) i;

should work fine.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23700552
>> it just gives me a largee number

What is it that you are trying to do ? What does the int value represent, and where does it come from ? What will you do with the uint32_t value ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23700555
>> it just gives me a largee number

And are you sure that you didn't try to cast a negative int value ?
0
 

Author Comment

by:kuntilanak
ID: 23701388
well.. I think I got it working with  htonl now.. thanks for the suggestions
0
 
LVL 86

Expert Comment

by:jkr
ID: 23701518
>>I think I got it working with  htonl now

Hm, but that will give you incorrect results on Intel CPUs...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question