thanks one addition i want on that is if it was possible to print out number of lines as well on left side before hex (in hex format) starting from 0? Also what does memset do? I have raised 10 more points...
Main Topics
Browse All TopicsI have run into a situation where i cannot figure out a way to display Hex with text on right site of that particular hex(by reading from file char by char and converting it to hex and displaying it). The following code only allows me to display hex with 16 chars on line, as for hex requirement that is good, but at the end of line i want to display text of that 16 chars of hex as well. How can i do it? Some code will help and if possible plz try to adjust it in the following code.
.....main........
while (f)
{
int n = 16;
for (int i =0; i<n; i++)
{
f.get(c); //get text from file
hexed(c); //convert to hex
}
cout << endl;
}
return (0);
}//main ends here
void hexed(char c)
{
char LoNibble, HiNibble, LoChar, HiChar;
char HexTab[]= "0123456789ABCDEF";
LoNibble = c & 0x0F;
HiNibble = (c >>4) & 0x0F;
LoChar = HexTab[ LoNibble ];
HiChar = HexTab[ HiNibble ];
cout << HiChar << LoChar << " " ;
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>>if it was possible to print out number of lines as well on left side before hex
Sure:
while (f)
{
unsigned int nLines = 1;
int n = 16;
// array to store the line
char acText [ 16 + 1]; // plus trailing '0'
memset ( acText, 0, sizeof ( acText));
cout << setbase ( ios_base::hex) << nLines << " ";
for (int i =0; i<n; i++)
{
f.get(c); //get text from file
acText [ i] = c;
hexed(c); //convert to hex
}
cout << acText << endl;
nLines++;
}
>>Also what does memset do?
'memset()' initializes the char array with bin. zeros.
if i try to output line numbers with the function you gave to output lines in hex (I made my original hex function to work with it to ouput lines in hex format) with numbers incrementing till eOf. It puts just 1 before each line and doesnt increment with lines..
while (f)
{
unsigned int nLines = 1;
int n = 16;
// array to store the line
char acText [ 16 + 1]; // plus trailing '0'
memset ( acText, 0, sizeof ( acText));
inthexed( nLines);
for (int i =0; i<n; i++)
{
f.get(c); //get text from file
acText [ i] = c;
hexed(c); //convert to hex
}
cout << acText << endl;
nLines++;
}
//my original char to hex converter that i used to convert number of lines from decimal to hex
void inthexed(unsigned int c) //char c to un..int c
{
int LoNibble, HiNibble, LoChar, HiChar; //changed from char to int
char HexTab[]= "0123456789ABCDEF";
LoNibble = c & 0x0F;
HiNibble = (c >>4) & 0x0F;
LoChar = HexTab[ LoNibble ];
HiChar = HexTab[ HiNibble ];
cout << HiChar << LoChar << " " ;
}
what i want number output to be like is in 00000000 (for 0) and .........(000000A0) for 10 and so on format...
Well, then use
while (f) {
unsigned int nLines = 1;
int n = 16;
// array to store the line
char acText [ 16 + 1]; // plus trailing '0'
memset ( acText, 0, sizeof ( acText));
char acLine [ 10];
sprintf ( acLine, "%8.8x ", nLines++);
cout << acLines;
for (int i =0; i<n; i++)
{
f.get(c); //get text from file
acText [ i] = c;
hexed(c); //convert to hex
}
cout << acText << endl;
}
I am not gonna change your 'handrolled' function to do the formatting :o)
Just a little point.
When you want to display the text of the 16 bytes you should probably make sure that 'undisplayable' characters and characters that do things to cursor aren't simply printed out, make sure you replace those with a dot or space or something before you print the text.
This function does a hex display of 16 bytes at a time:
void hexline(unsigned int addr,
const unsigned char * buf, int n,
ostream & os)
{
// addr is the address to display at start.
// buf is a pointer to data, n is number of bytes in
// data (usually 16 but can be less on the last
// record.
// os is the output stream.
char abuf[16 + 1]; // buffer for the text.
char tmp[4 + 1]; // address buffer 4 digits + null.
sprintf( tmp, "%04x", addr);
os << tmp << ':';
static const char digits[17] = "0123456789abcdef";
for (int i = 0; i < n; i++) {
unsigned char uc = buf[i];
if (i == 8)
os << " -"; // output a dash in the middle.
os << ' ' << digits[uc >> 4] << digits[uc & 0x0f];
if (is_special(uc))
uc = '.'; // replace uc with . if special.
abuf[i] = static_cast<char>(uc);
}
abuf[n] = '\0';
// if n < 16 we must add blanks to make the text buf
// align with previous lines.
while (n < 16) {
if (n == 8)
os << " "; // use " -" if you want to.
os << " ";
++n;
}
os << ' ' << abuf << endl;
}
memset(dst,val,count); does the same as:
for (int i = 0; i < count; i++) {
static_cast<char *>(dst)[i] = val;
}
only more efficient.
You can use memset to clear a buffer or set a portion of the buffer equal to blanks. Since I had my function do output to ostream I didn't need to use memset but if you want to output to a string instead you can use memset to blank out all the blanks as I did in that while loop.
The function written above can be called like this:
unsigned int addr = 0;
int n;
unsigned char buf[16];
while ((n = fread(buf,16,1,fp)) > 0) {
hexline(addr,buf,n,cout);
addr += n;
}
and the file will output in the 'usual' hexdump fashion.
<line> 0000 00 11 22 33 44 55 66 77 - 88 99 aa bb cc dd ee ff .."3DUfw........<line end>
The text may vary depending on your coding system for upper bytes in the range 0x80-0xff.
Alf
Dear MellowD0c
I've refunded 45 points to enable you to accept the comment for one expert and to post a
"Points for <expertname>" Q for the other expert in the same topic area.
Please:
1) Post the link to the original Q in the "Points for <expertname>" and
2) Add in the original Q a comment with the link to the "Points for <expertname>", thus the email notif will warn the expert.
modulo
Community Support Moderator
Experts Exchange
http://www.experts-exchang
There are your points Salte
Please can you help me convert to following code base_10_value to base_16_value. I had been trying very hard bu I have been unsuccessful. I would appreciated your help very much. Thank you. The program must add two very large hexadecima (unsigned) integers. The two summads should be stored arrays. Starting at the least significant digits, the program should compute the sum by adding the two integers digit by digit.
My luck,
Please can you help me convert to following code base_10_value to base_16_value. I had been trying very hard bu I have been unsuccessful. I would appreciated your help very much. Thank you. The program must add two very large hexadecima (unsigned) integers. The two summads should be stored arrays. Starting at the least significant digits, the program should compute the sum by adding the two integers digit by digit.
My luck, #include<iostream.h>
const char ch='A', 'B', 'C', 'D', 'E', 'F';
void main(){
char first_summand[256];
char second_summand[256];
char sum[258];
char ch=isum;
char ch=icarry;
int isum, icarry;
char ch * first, * second, * csum;
//Input the first summand
cout << "Input the first summand:(1-9,A-F(UPPERCASE
first = first_summand;
*first++ = '\0';
while ( (*first++ = cin.get() )!= '\n' )
;
*--first = '\0';
first--;
//Input the second summand
cout << "Input the second summand:(1-9,A-F(UPPERCASE
second = second_summand;
*second++ = '\0';
while ( (*second++ = cin.get()) != '\n')
;
*--second = '\0';
second--;
//Calculate the sum
csum = sum + 256;
*csum-- = '\0';
icarry = 0;
while ( *first || *second) {
if ( *first && *second) {
isum = *first-- + *second-- - 2 * int('0') + icarry;
icarry = isum / 16;
isum = isum % 16;
*csum-- = isum + '0';
}
if ( *first && (! *second) ) {
isum = *first-- - int ('0') + icarry;
icarry = isum / 16;
isum = isum % 16;
*csum-- = isum + '0';
}
if ( (! *first) && *second) {
isum = *second-- - int('0') + icarry;
icarry = isum / 16;
isum = isum % 16;
*csum-- = isum + '0';
}
}
char ch = *csum;
if ( icarry ) {
*csum-- = icarry + '0';
}
csum++; // adjust csum to point to the start of the output string
cout << (csum) << '\n';
}
One,
If you have a question of code like this, you should post a new question.
Even if it is related to the old already answered question - since you have a new question,
you really should point a question.
If you don't plan to award anyone anything for helping you, why should we help ya?
Oh well, I am a nice guy so I might help you if I get the time :-)
For one thing, it might be homework so I cannot give you solution. However, you did post a program and I can comment on that one :-)
Secondly, your problem description appear to indicate multiple precision and you might try to read some info on the net on how multiple precision numbers works. Adding in particular is very easy to do.
Anyway, here are some comments on the program code you gave.
onst char ch='A', 'B', 'C', 'D', 'E', 'F';
What is this? In C the comma operator is an operator that says "discard whatever result you computed before comma and only use the expression after the comma".
Thus ch gets the value 'F' and all the other A, B etc are ignored.
I don't think that is what you intended so you should probably rethink this statement.
Secondly, the way you read the numbers are "hairy" for lack of a better word and most
likely wrong. Again I think you should rethink what it is you intend to do and ask yourself if this statement really does what you want it to do.
You say you want the numbers with the least significant digit at the smaller index. Thus
you should read the digit you read first - the most significant digit - at the END of the buffer.
So something like:
char buf[256];
char * end = buf + sizeof(buf); // end point to end of buf.
char * ptr = end; // ptr points to end of buf.
add in the digits as you read them by *--ptr = .....,
You should probably also convert the characters to hex digits - nibbles. A quick and easy
way to do that is like this:
static const char digits[] = "0123456789abcdefABCDEF";
if c is a char that you just read the next digit, you do:
char c = cin.get();
char * p = strchr)digits, c);
if (p != NULL) { // valid digit.
int x = p - digits;
// The following statement makes the digit value become 11 if you read 'B' which
// in the array above gets index 17.
if (x >= 16)
x -= 6;
*--ptr = static_cast;char>(x);
}
You can then translate this to a loop so you read all the digits.
The number is then stored in the array pointed to by ptr and of length end - ptr and the least
significant digit is at ptr[0], the next least significant is at ptr[1] and the most significant digit
is at ptr[end - ptr - 1} etc.
Also, I STRONGLY suggest you put the "read a number into the array" in a separate function and call that function twice - once for each number - instead of having two almost identical loops. What you do above there is very bad style.
If you keep the numbers as text strings, then your addition is almost correct. The only problem is that you forget to store away isum anywhere, you compute it correctly and then promptly throws it away and ignore it.
If you do change the input routine to produce nibbles as I suggested above, remember to remove the -2 * int('0') above. If you do keep it as text you MUST include it. Also remember to add '0' back to isum before storing it in result. You do not need to cast the '0' to int. C++ has 'x' as a char type but it is automatically converted to int when you try to add it to something that doesn't fit in a char. A char can always be stored in an int without any explicit casting, so '0' is just as fine as int('0') there.
I believe the two if statements inside the while loop is to handle the case that one of the numbers are shorter than the other.
For one thing this is very inefficient since you do the test for both in the while loop each time. Once any of those tests are true you don't have to do the big loop any more and just add in the carry etc. Another way to handle this is simply to have each digit be '0' whenever you reach the end of the string before the other string is empty. Something like this:
isum = 0;
isum += *first ? *first : '0';
isum += *second ? *second : '0';
isum += icarry - 2 * '0';
icarry = isum >> 4; // same as isum / 16.
isum &= 15; // same as isum %= 16
should get you a valid isum as a value in the range 0-15, if you want it to be a digit
you can then use the digits[] array and index into it.
Hope this is of help.
Alf
Business Accounts
Answer for Membership
by: jkrPosted on 2003-01-21 at 06:56:13ID: 7770744
Try
while (f)
{
int n = 16;
// array to store the line
char acText [ 16 + 1]; // plus trailing '0'
memset ( acText, 0, sizeof ( acText));
for (int i =0; i<n; i++)
{
f.get(c); //get text from file
acText [ i] = c;
hexed(c); //convert to hex
}
cout << acText << endl;
}