473,809 Members | 2,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

representation of integers(again) very annoying..

I have a member of a struct which is:
int32_t ut_addr_v6[4];
And int32_t is typedef int int32_t; mentioning that for clarity.

Now when I attempt the following:
printf("%u.%u.% u.%u\n", ut_addr_v6[0],
ut_addr_v6[1], ut_addr_v6[2],
ut_addr_v6[3]);

I get: 2912244697.0.0. 0

Why is it that I get this result from the above?

However, if I use the following:

void *ptr;
unsigned char *ptr1;

ptr = ut_addr_info;
ptr1 = ptr;

printf("%u.%u.% u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);

It displays how I wish it to display. So why is it that it works for a
pointer to unsigned char but doesn't work when I attempt to display
each element of the array of ints? And why is it that the array of its
is 4 ints and not just one, when all it seems to occupy is one int? Is
this for compensation of support for ipv6? If that is the case, then I
must be accessing the individual bytes of the first element of the
array with unsigned char *.

Also, what is a good method/algorithm for converting network byte
order to host byte order where host byte order is little endian? I can
extract the individual bytes in reverse order and stoe them in an
array but I was aiming more for storing them in an object of the same
type I am manipulating.

And lastly, when I was using the "ptr" of type void * above, I did the
following:

printf("%u.%u.% u.%u\n", (unsigned char)ptr[0],
(unsigned char)ptr[1], (unsigned char)ptr[2],
(unsigned char)ptr[3]);

dereferencing of void *.

Is this because the cast has no affect when ptr[0] is firstly
converted to *(ptr+0) ? What is a way around this while still using a
pointer to void?
- nethlek
Nov 13 '05 #1
4 2081
On 12 Oct 2003 17:04:51 -0700, ne*****@tokyo.c om (Mantorok Redgormor)
wrote:
I have a member of a struct which is:
int32_t ut_addr_v6[4];
And int32_t is typedef int int32_t; mentioning that for clarity.

Now when I attempt the following:
printf("%u.%u. %u.%u\n", ut_addr_v6[0],
ut_addr_v6[1], ut_addr_v6[2],
ut_addr_v6[3]);
Is it really part of a struct? You have no struct name in the code.

Based on what you provided it seems as if an int is 32 bits. Is this
correct?

%u is the format for an unsigned int. You are passing a signed int.
Normally a mismatch between format and argument causes undefined
behavior.

I get: 2912244697.0.0. 0

Why is it that I get this result from the above?

However, if I use the following:

void *ptr;
unsigned char *ptr1;

ptr = ut_addr_info;
ptr1 = ptr;
You could have simply said
ptr1 = (void*)ut_addr_ info;

Which is it, ut_addr_info or ut_addr_v6?

printf("%u.%u. %u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);
Here the %u is correct because an unsigned char will be automatically
promoted to unsigned int when used as an argument in a variadic
function.

But each ptr[i] is only one char. (Pls confirm that on your system a
char is 8 bits.) That means that ptr1[0] through ptr1[3] are the four
bytes of ut_addr_v6[0].

It displays how I wish it to display. So why is it that it works for a
pointer to unsigned char but doesn't work when I attempt to display
each element of the array of ints? And why is it that the array of its
You are not printing the same data. In one case, you are printing the
4 bytes of one int, each as a separate value. In the other, you are
printing this one int in its entirety and three other int as well.
is 4 ints and not just one, when all it seems to occupy is one int? Is
this for compensation of support for ipv6? If that is the case, then I
It is your code; how would we know.
must be accessing the individual bytes of the first element of the
array with unsigned char *.
That is common for dealing with ip addresses.
Also, what is a good method/algorithm for converting network byte
order to host byte order where host byte order is little endian? I can
extract the individual bytes in reverse order and stoe them in an
array but I was aiming more for storing them in an object of the same
type I am manipulating.
If you are talking about unsigned int, you could use
unsigned_int = ((unsigned)firs t_byte << 24) |
((unsigned)seco nd_byte << 16) |
((unsigned)thir d_byte << 8) |
fourth_byte;

And lastly, when I was using the "ptr" of type void * above, I did the
following:

printf("%u.%u. %u.%u\n", (unsigned char)ptr[0],
(unsigned char)ptr[1], (unsigned char)ptr[2],
(unsigned char)ptr[3]);

dereferencin g of void *.

Is this because the cast has no affect when ptr[0] is firstly
converted to *(ptr+0) ? What is a way around this while still using a
pointer to void?


Cast and [] have the same precedence and associate left to right so it
should have been processed as ((unsigned char)ptr)[0] which never
dereferences a void *. Post some compilable code so we can see for
ourselves.
<<Remove the del for email>>
Nov 13 '05 #2
In article <41************ **************@ posting.google. com>,
ne*****@tokyo.c om (Mantorok Redgormor) wrote:
<snipped>


I think you should take a course about programming in C.
Nov 13 '05 #3
Barry Schwarz wrote:

On 12 Oct 2003 17:04:51 -0700, ne*****@tokyo.c om (Mantorok Redgormor)
wrote:

printf("%u.%u. %u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);


Here the %u is correct because an unsigned char will be automatically
promoted to unsigned int when used as an argument in a variadic
function.


unsigned char will be converted to int,
if int can represent all values of the original type.

--
pete
Nov 13 '05 #4
On 13 Oct 2003 05:36:46 GMT, Barry Schwarz <sc******@deloz .net> wrote:
On 12 Oct 2003 17:04:51 -0700, ne*****@tokyo.c om (Mantorok Redgormor)
wrote:
I have a member of a struct which is:
int32_t ut_addr_v6[4];
And int32_t is typedef int int32_t; mentioning that for clarity.

Now when I attempt the following:
printf("%u.%u .%u.%u\n", ut_addr_v6[0],
ut_addr_v6[1], ut_addr_v6[2],
ut_addr_v6[3]);


Is it really part of a struct? You have no struct name in the code.

Based on what you provided it seems as if an int is 32 bits. Is this
correct?

%u is the format for an unsigned int. You are passing a signed int.
Normally a mismatch between format and argument causes undefined
behavior.

I get: 2912244697.0.0. 0

Why is it that I get this result from the above?

However, if I use the following:

void *ptr;
unsigned char *ptr1;

ptr = ut_addr_info;
ptr1 = ptr;


You could have simply said
ptr1 = (void*)ut_addr_ info;

Which is it, ut_addr_info or ut_addr_v6?

printf("%u.%u .%u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);


Here the %u is correct because an unsigned char will be automatically
promoted to unsigned int when used as an argument in a variadic
function.

But each ptr[i] is only one char. (Pls confirm that on your system a
char is 8 bits.) That means that ptr1[0] through ptr1[3] are the four
bytes of ut_addr_v6[0].

It displays how I wish it to display. So why is it that it works for a
pointer to unsigned char but doesn't work when I attempt to display
each element of the array of ints? And why is it that the array of its


You are not printing the same data. In one case, you are printing the
4 bytes of one int, each as a separate value. In the other, you are
printing this one int in its entirety and three other int as well.
is 4 ints and not just one, when all it seems to occupy is one int? Is
this for compensation of support for ipv6? If that is the case, then I


It is your code; how would we know.
must be accessing the individual bytes of the first element of the
array with unsigned char *.


That is common for dealing with ip addresses.

Also, what is a good method/algorithm for converting network byte
order to host byte order where host byte order is little endian? I can
extract the individual bytes in reverse order and stoe them in an
array but I was aiming more for storing them in an object of the same
type I am manipulating.


If you are talking about unsigned int, you could use
unsigned_int = ((unsigned)firs t_byte << 24) |
((unsigned)seco nd_byte << 16) |
((unsigned)thir d_byte << 8) |
fourth_byte;

And lastly, when I was using the "ptr" of type void * above, I did the
following:

printf("%u.%u .%u.%u\n", (unsigned char)ptr[0],
(unsigned char)ptr[1], (unsigned char)ptr[2],
(unsigned char)ptr[3]);

dereferenci ng of void *.

Is this because the cast has no affect when ptr[0] is firstly
converted to *(ptr+0) ? What is a way around this while still using a
pointer to void?


Cast and [] have the same precedence and associate left to right so it
should have been processed as ((unsigned char)ptr)[0] which never
dereferences a void *. Post some compilable code so we can see for
ourselves.


Disregard the last paragraph. Reading too quickly. It is a matter of
precedence. [] has higher precedence than cast so this is interpreted
as (unsigned char)(ptr[0]) which does in fact dereference the void
pointer. You could have used ((unsigned char*)ptr)[0].
<<Remove the del for email>>
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
7491
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the function will return me an array holding a random subset of integers in that range of a size that I specify So the Function would Probabaly look something like this:
9
9005
by: Arun Goel | last post by:
Hi, I came across this question from a website.. According to the C++ standard, what is an object's internal representation in memory guaranteed to be? a) Initialized b) On a word boundary c) Contiguous d) On stack
10
3253
by: Mantorok Redgormor | last post by:
I always see posts that involve the representation of integers, where some poster claims that the unerlyding representation of an integer doesn't have to reflect on the actual integer, for example: int foo = 0; 0 can be all zeros 0x00000000 or 00000000 00000000 00000000 00000000 Then someone chimes in and says 0 doesn't have to contain all zeros..
16
2317
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
33
2518
by: Ruffin Bailey | last post by:
I coulda sworn I was given an explanation during an AppDev class years ago for VB6, but don't recall the answer. Why is it that -1 is True in Visual Basic (and now VB.NET)? Bit flags seem like they should always be 0 or 1 to me... (not that I haven't used VB long enough by now to know better). Sorry to pester, but "why is -1 = true?" is a difficult thing to Google! Ruffin Bailey
64
3962
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? - AFAIK C allows "null pointers" to be represented differently then "all bits 0". Is this correct? - AFAIK I can't `#define NULL 0x10000' since `void* p=0;' should work just like `void* p=NULL'. Is this correct?
4
1813
by: Steve Carter | last post by:
The following code fragment prompts the user to enter two integers separated by a space. It works the way I want it to, but a friend said it's not correct and said it can cause a buffer overflow. I don't really see anything wrong with it. Can someone enlighten me? int main(void) { int i,j; char buff;
13
1707
by: Gary Wessle | last post by:
Hi there I have a method which returns time_t and another two methods return double data types and I cann't change that since the library is provided by Big Bucks Inc. I think time_t is long but I could not verify that from time.h using sizeof(type) and numeric_limits<type>::max() tells me that int and long give the same output.
26
50422
by: Carramba | last post by:
Hi! How can I output value of char or int in binary form with printf(); ? thanx in advance
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.