473,812 Members | 2,984 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

endianness and sscanf/sprintf

Two different platforms communicate over protocols which consist of
functions and arguments in ascii form. System might be little
endian/big endian.

It is possible to format string using sprintf and retreive it using
sscanf.
Each parameter has a delimiter, data type size is ported to the
platform, and expected argument order is known.

Is this approach portable w.r.t. endianess ?
regards,
Pramod
Nov 14 '05 #1
22 5044
"pramod" <sp********@yah oo.com> wrote in message
news:c6******** *************** ***@posting.goo gle.com
Two different platforms communicate over protocols which consist of
functions and arguments in ascii form. System might be little
endian/big endian.

It is possible to format string using sprintf and retreive it using
sscanf.
Each parameter has a delimiter, data type size is ported to the
platform, and expected argument order is known.

Is this approach portable w.r.t. endianess ?
regards,
Pramod

endianness only affects the way that integers are stored (and perhaps
floating point numbers --- I am not sure). It does not affect the storage of
characters so it is not an issue if you are only sending text.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Nov 14 '05 #2
You will be fine as everything is being converted to characters.
As long as characters are represented as 8 bytes, the numbers
will be interpreted correctly. Java bytecodes use the same approach.

The following article discusses the endianness in detail:

http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams
Nov 14 '05 #3
EventHelix.com wrote:
You will be fine as everything is being converted to characters.
As long as characters are represented as 8 bytes, the numbers
will be interpreted correctly.


In C (and, as far as I am aware, C++ too), characters are always represented
in a single byte. Character /constants/ are represented (in C, but not C++)
by the int type, which might conceivably be eight bytes. Is that what you
meant?

(Followups set to comp.lang.c)

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #4
On Wed, 31 Dec 2003 01:20:44 -0800, pramod wrote:
Two different platforms communicate over protocols which consist of
functions and arguments in ascii form. System might be little
endian/big endian.

It is possible to format string using sprintf and retreive it using
sscanf.
Each parameter has a delimiter, data type size is ported to the
platform, and expected argument order is known.

Is this approach portable w.r.t. endianess ?


Yes, and a very good way to do it. But only if really using ascii,
otherwise you may end up mixing codesets. Consider using UTF8 if you use
characters >=128 (i.e. not ascii).

HTH,
M4

Nov 14 '05 #5
EventHelix.com wrote:
You will be fine as everything is being converted to characters.
As long as characters are represented as 8 bytes,
bits?
the numbers
will be interpreted correctly. Java bytecodes use the same approach.

The following article discusses the endianness in detail:

http://www.eventhelix.com/RealtimeMa...ndOrdering.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams


Nov 14 '05 #6
"Jeff Schwab" <je******@comca st.net> wrote...
EventHelix.com wrote:
You will be fine as everything is being converted to characters.
As long as characters are represented as 8 bytes,


bits?


Not that it matters. The second sentence almost invalidates the otherwise
perfectly correct first ;-)

Peter
Nov 14 '05 #7
Richard Heathfield <in*****@addres s.co.uk.invalid > wrote in message news:<3f******@ news2.power.net .uk>...
EventHelix.com wrote:
You will be fine as everything is being converted to characters.
As long as characters are represented as 8 bytes, the numbers
will be interpreted correctly.


In C (and, as far as I am aware, C++ too), characters are always represented
in a single byte. Character /constants/ are represented (in C, but not C++)
by the int type, which might conceivably be eight bytes. Is that what you
meant?

(Followups set to comp.lang.c)


Typo: it should have been "8 bits" (i.e. byte).

Sandeep
Nov 14 '05 #8
EventHelix.com wrote:
Richard Heathfield <in*****@addres s.co.uk.invalid > wrote in message
news:<3f******@ news2.power.net .uk>...
EventHelix.com wrote:
> You will be fine as everything is being converted to characters.
> As long as characters are represented as 8 bytes, the numbers
> will be interpreted correctly.


In C (and, as far as I am aware, C++ too), characters are always
represented in a single byte. Character /constants/ are represented (in
C, but not C++) by the int type, which might conceivably be eight bytes.
Is that what you meant?

Typo: it should have been "8 bits" (i.e. byte).


But there is no requirement in either C or C++ for a byte to be exactly 8
bits; only that it must be /at least/ 8 bits.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #9
On Fri, 02 Jan 2004 05:53:31 +0000, Richard Heathfield wrote:
EventHelix.com wrote:
Richard Heathfield <in*****@addres s.co.uk.invalid > wrote in message
news:<3f******@ news2.power.net .uk>...
EventHelix.com wrote:

> You will be fine as everything is being converted to characters.
> As long as characters are represented as 8 bytes, the numbers
> will be interpreted correctly.
Even assuming you ment 8 bits, this is not true. If one system uses ascii
and the other uses ebcdic, you're screwed. Even the subtle distinctions
between iso-latin-1 and iso-latin-15, two almost compatible and often used
character sets, might bite you. All of these use 8 bits (well OK, ascii
uses 7).

In C (and, as far as I am aware, C++ too), characters are always
represented in a single byte. Character /constants/ are represented (in
C, but not C++) by the int type, which might conceivably be eight bytes.
Is that what you meant?

Typo: it should have been "8 bits" (i.e. byte).


But there is no requirement in either C or C++ for a byte to be exactly 8
bits; only that it must be /at least/ 8 bits.


But note the unfortunate discrepancy between the meaning of the word byte
in C/C++ and that of measoring storage. However, C/C++ is not alone here,
Internet standards talk about octets when they mean 8 bits.

Same with the unit words. That means different things to different people.
The way I learned it at uni, very long time ago, was that a word was the
basic unit of storage. Same as the definition of byte in C/C++. Along came
MicroSoft and institutionalis ed the word-size of the 8086 as a WORD, so to
others a word now is 16 bits. I've seen even different uses of the word
'word', anyone got an example?

Why am I saying this? Because in the context of C/C++ a byte has a defined
meaning. However, in the context of disks and memory, a byte has a
different meaning. When the context is not clear it is very easy to get
confusion. Ah I here you say, but this is a C/C++ group, so the meaning is
clear. That may be true, but:
- The problem described a certain context, one where many people
(incorrectly) use the word byte to mean 8 bits.
- It is very confusing to people anyhow. Youngsters are raised with the
notion that a byte is 8 bits.

In the end, we can only conclude that this difference in meaning is very
unfortunate. Technically, an octet is the correct term for 8 bits. But
we're never going to change the common use of byte anymore. In the
meantime we'll have to live with it.

I just wished the C/C++ standards had used a different term than byte.
Even word would have been better.

M4

Nov 14 '05 #10

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

Similar topics

21
674
by: pramod | last post by:
Two different platforms communicate over protocols which consist of functions and arguments in ascii form. System might be little endian/big endian. It is possible to format string using sprintf and retreive it using sscanf. Each parameter has a delimiter, data type size is ported to the platform, and expected argument order is known. Is this approach portable w.r.t. endianess ?
82
12655
by: zardoz | last post by:
I've got this problem: unsigned long long lTemp; char cLargeNum="1324567890"; sscanf(clargeNum,"%llu",&lTemp); which under Win32 isn't working*. My program needs to compile under posix so no Win32 specials allowed....
10
5475
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
5
6405
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less precise than I had expected:
2
4032
by: SSM | last post by:
Hi, Does C standard comment about "Endianness" to be used to store a structure/union variables? Thanks & Regards, Mehta
2
2864
by: Mark Oliver | last post by:
Hi. What is the best replacement for sscanf? Thanks, Mark
6
3112
by: ATS | last post by:
INF: Has anyone made a CString, sprintf, and sscanf for .NET? Please help, I want to code with PURE .NET (i.e. pure CLR). No MFC, No ATL, no C-Run Time Library. But I want CString, sprintf, and sscanf. The "String" class in .NET is completely worthless to me, especially its so called "Format" method. In fact to just, harp on how "lacking" it is to me, in C++.NET, one can NOT do this:
7
27019
by: nick | last post by:
is it similar to scanf? when i use scanf it can read the words in the screen automatically one after another.i use a char array to store the string,then use sscanf to read the words,but it just only reat out the first word in the string array every time. so if i want to read the words in the string one by one, just like scanf, what should i do? thanks!
22
2967
by: Superfox il Volpone | last post by:
Hello I have some problem with sscanf, I tryed this code but it doesn't works : char* stringa = "18/2005" char mese; char anno; int i_letture; i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
7
2502
by: RSoIsCaIrLiIoA | last post by:
until a poor newbie can build a better function than sscanf and fgets scanf("%s", string) is like gets(string)
0
9734
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
9607
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
10664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
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
9219
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...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
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...
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3029
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.