473,660 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Block Text File

I have a file that has blocks of data that can vary in length. The
first 2 bytes of the block are a Hex number telling me how many bytes
long the block is (including those 2 bytes). I need to be able to
read those first 2 bytes, then read then entire block and write it out
to a new file with '\n' at the end of each block. Can someone help me
with that? I am having significant trouble determining the block
length as I have done little work in C++.

Thank you,

Scott
Oct 28 '08 #1
8 2618
scad wrote:
I have a file that has blocks of data that can vary in length. The
first 2 bytes of the block are a Hex number telling me how many bytes
long the block is (including those 2 bytes).
Are you sure the two bytes form a hexadecimal number (in ascii?), that
is, the maximum size of the block is 255 bytes (ie. FF in hex), rather
than the two bytes forming a 16-bit value telling the size of the block
(ie. the maximum size would then be 65535 bytes)?

The solution is obviously different depending on that. Also in the
latter case it depends on whether the two bytes form a low-endian or a
high-endian value.
Oct 28 '08 #2
On Oct 28, 4:07*pm, Juha Nieminen <nos...@thanks. invalidwrote:
scad wrote:
I have a file that has blocks of data that can vary in length. *The
first 2 bytes of the block are a Hex number telling me how many bytes
long the block is (including those 2 bytes).

* Are you sure the two bytes form a hexadecimal number (in ascii?), that
is, the maximum size of the block is 255 bytes (ie. FF in hex), rather
than the two bytes forming a 16-bit value telling the size of the block
(ie. the maximum size would then be 65535 bytes)?

* The solution is obviously different depending on that. Also in the
latter case it depends on whether the two bytes form a low-endian or a
high-endian value.
It is a 16-bit value. 7F 88 = 32648

Thank you,
Oct 29 '08 #3
On Oct 29, 1:28 am, scad <scadr...@gmail .comwrote:
On Oct 28, 4:07 pm, Juha Nieminen <nos...@thanks. invalidwrote:
scad wrote:
I have a file that has blocks of data that can vary in
length. The first 2 bytes of the block are a Hex number
telling me how many bytes long the block is (including
those 2 bytes).
Are you sure the two bytes form a hexadecimal number (in
ascii?), that is, the maximum size of the block is 255 bytes
(ie. FF in hex), rather than the two bytes forming a 16-bit
value telling the size of the block (ie. the maximum size
would then be 65535 bytes)?
The solution is obviously different depending on that. Also
in the latter case it depends on whether the two bytes form
a low-endian or a high-endian value.
It is a 16-bit value. 7F 88 = 32648
And how is this binary value represented? Without knowing that,
we can't read it. If it's the same as an unsigned short in XDR,
something like:

unsigned short result = input.get() ;
result |= result << 8 | input.get() ;

would do the trick (except for error handling). If the format
is something else, you'd need something different.

And of course, this only works if you open the file in binary.
Similarly, reading the data, then outputing it with a trailing
'\n', will likely only work if the data is text, encoded in the
same character set as you normally use.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 29 '08 #4
scad wrote:
It is a 16-bit value. 7F 88 = 32648
Thus it had nothing to do with hexadecimal. You should be more
accurate when posting questions, or else you will only send people into
wild goose chases.
Oct 29 '08 #5
On Oct 29, 11:35 am, Juha Nieminen <nos...@thanks. invalidwrote:
scad wrote:
It is a 16-bit value. 7F 88 = 32648
Thus it had nothing to do with hexadecimal. You should be more
accurate when posting questions, or else you will only send people into
wild goose chases.
It's common for beginners to associate binary values
with hex. No need to bite the newbies.

Sean

Oct 30 '08 #6
In message
<9d************ *************** *******@y71g200 0hsa.googlegrou ps.com>,
se************* @yahoo.com writes
>On Oct 29, 11:35 am, Juha Nieminen <nos...@thanks. invalidwrote:
>scad wrote:
It is a 16-bit value. 7F 88 = 32648
Thus it had nothing to do with hexadecimal. You should be more
accurate when posting questions, or else you will only send people into
wild goose chases.

It's common for beginners to associate binary values
with hex. No need to bite the newbies.
It's common for beginners and others to confuse values with
representations , and this should be discouraged.

A value is just a value, it isn't "binary" any more than it is
"hexadecima l".

--
Richard Herring
Oct 30 '08 #7
Richard Herring wrote:
A value is just a value, it isn't "binary" any more than it is
"hexadecima l".
True, but it's difficult to talk about values and their storage when
the terminology is so confusing.

"Hexadecima l" refers quite unambiguously to the (usually ascii)
representation of a numerical value (in base 16). The term "binary" is
more complicated.

In theory when you say "the number is stored in binary" it might refer
to one of two things:

1) It's stored in base-2 representation. That is, the number is stored
by writing a combination of the two characters '0' and '1'.

2) It's stored in the same way as it's stored in memory, in other
words, as a series of octets. In other words, it's stored in "raw"
format, without any conversion or representation in ascii.

Thus the term "binary" is used with two different meanings: In some
contexts it talks about base-2 (ascii) representation, in other contexts
it talks about raw, unconverted byte values (eg. when saying "open the
file in binary mode). These two things have basically nothing to do with
each other, except that they share the name "binary".

Maybe this is the reason why it seems that some people get even more
confused and think "hexadecima l" refers to what usually is meant with
"binary" (in the second meaning).
Oct 30 '08 #8
On Oct 30, 8:14 pm, Juha Nieminen <nos...@thanks. invalidwrote:
Richard Herring wrote:
A value is just a value, it isn't "binary" any more than it
is "hexadecima l".
True, but it's difficult to talk about values and their
storage when the terminology is so confusing.
"Hexadecima l" refers quite unambiguously to the (usually
ascii) representation of a numerical value (in base 16). The
term "binary" is more complicated.
In theory when you say "the number is stored in binary" it
might refer to one of two things:
1) It's stored in base-2 representation. That is, the number
is stored by writing a combination of the two characters '0'
and '1'.
That is, actually, what is required by the C++ standard.

Of course, since only two characters are involved, a character
encoding using just one bit (rather than the usual 7, 8 or more)
is sufficient, and used by all of the implementations I've ever
encountered.

(Sort of a half :-). Just thought I'd add to the confusion, for
the fun of it.)
2) It's stored in the same way as it's stored in memory, in
other words, as a series of octets. In other words, it's
stored in "raw" format, without any conversion or
representation in ascii.
I like the word "raw". Or "machine" or "hardware" representation.

The C++ standard requires this to be a pure binary
representation (and I don't think the intent is to require
ASCII).

Of course, all of the standard requirements are "as if"; an
implementation can use base 10, as long as it implements &, |, ^
and ~ in a manner that they behave "as if" the representation
were base 2.
Thus the term "binary" is used with two different meanings: In
some contexts it talks about base-2 (ascii) representation, in
other contexts it talks about raw, unconverted byte values
(eg. when saying "open the file in binary mode). These two
things have basically nothing to do with each other, except
that they share the name "binary".
And that they are both demonstrably base 2. (Consider the
behavior of |, &, ^ and ~.)
Maybe this is the reason why it seems that some people get
even more confused and think "hexadecima l" refers to what
usually is meant with "binary" (in the second meaning).
Since most modern machines are byte oriented, maybe we should
call machine format base 256.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 31 '08 #9

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

Similar topics

2
13546
by: Eric | last post by:
Hi, I've a problem with trying to retrieve a session variable in an include file. Basically, the main asp creates a session variable: <% Session("var1") = "Hello" %> And then when I click on a button it refers to the include file, which I believe is all client-side code as there are no server <% %> tags.
1
3437
by: Arthur Dent | last post by:
Hello all, sorry for the cross-post, but im not sure which group is best for this question. I am an ASP.NET developer, but am learning PHP/perl for the first time now to make some to changes to a client's site which was done by someone else. I wrote a debug TPL which ive got working great now and was Such a sense of accomplishment!! :) . People always say how cryptic C can be, but i think perl definitely beats C out! Anyway.... In the...
1
24167
by: amit | last post by:
Hello Group, Does anybody know how I can have a global variable in an HTML file? for instance, I have a fuction (called aFunction() here) and during a mousedown or up event the function is going to be called. The third passing arugment or parameter is myGlobarVar. How can I make this work? since myGlobalVar is defined in a different block of javascript within the HTML file?
1
25665
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
2
10908
by: neeebs | last post by:
Hi, I'm not sure if this is a javascript problem per se, but here goes. I have an xsl document with a python function defined within a <script> block. Elsewhere in the xsl file, within a python script block, I call the python function and store the return value in a variable. I need to be able to display this returned value on the webpage that the xsl generates. I tried to do a document.write() but found that document.write() causes...
2
3009
by: Kevin | last post by:
I am having difficulty updating a variable page-time-stamp in the following snippit. The variable time-stamp is initialized from the attribute time-stamp from the log element. Some of the page elements (children of log) in my XML source have a time-stamp attribute, so I want to use the time-stamp from the page for the page-time-stamp value. I use an xsl:choose/xsl:when/xsl:otherwise combination to setup an if/else statement to select the...
112
5425
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
3
2039
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in the same file (.c + all relevant .h's)? e.g.
2
1655
by: RgeeK | last post by:
I'm seeing something which make me think I'm missing something about how global var's behave. I've defined a global string, right at the start of my .py file. outXMLfile = "abc" I define a class and do a bunch of stuff below that. Then I have another class, and in it, there is a method 'def' that has: def OnOutfileButton(self,evt):
0
8428
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
8851
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
8754
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
8542
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
8630
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
7362
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
6181
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
4177
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...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.