473,388 Members | 915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Print 64 bit with a char buffer

Hello

I have a buffer of char and I want to print in a 64 bit word in hexa.
charbuff0 charbuff1 charbuff2 charbuff3 charbuff4 charbuff5 charbuff6
charbuff7
64bit msb 64bit lsb
this is my buffer
char *pCharBuff;

I try that:
unsigned signed i;
for(i=0;i<size_of_charbuff;i+8)
{
printf("%llx",*pCharBuff[i]);
}

But it fails!
To test my program I want to print 8 char with something like that
for(i=0;i<size_of_charbuff;i=i+8)
{
printf("%s%s%s%s%s%s%s%s",*pCharBuff[i]",*pCharBuff[i+1]],*pCharBuff[i
+2],*pCharBuff[i+3]],*pCharBuff[i+4]],*pCharBuff[i+5]],*pCharBuff[i
+6]],*pCharBuff[i+7]);
}
But I print a character instead of the value of the char.
Someone have an idea ?

Thanks!
Dec 12 '07 #1
2 4603
la*************@techway.fr wrote:
Hello

I have a buffer of char and I want to print in a 64 bit word in hexa.
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>

int main(void)
{
const size_t size_of_charbuff = 32;
unsigned long long llvalue;
unsigned long lvalue;
unsigned int value;
size_t i;
unsigned char charbuff[size_of_charbuff];

for (i = 0; i < sizeof charbuff; i++)
charbuff[i] = rand() & 0377;

if (64 % CHAR_BIT) {
fprintf(stderr, "%s\n", "64 is not a multiple of CHAR_BIT\n"
"giving up.\n");
exit(EXIT_FAILURE);
}
printf("printing bytes:\n");
for (i = 0; i < sizeof charbuff; i++)
printf("%#04x%c", charbuff[i], (7 == i % 8) ? '\n' : ' ');
putchar('\n');

if (sizeof charbuff >= sizeof llvalue
&& !(sizeof charbuff % sizeof llvalue)) {
printf("\nprinting as unsigned long longs\n");
for (i = 0; i < sizeof charbuff; i += sizeof llvalue) {
memmove(&llvalue, charbuff + i, sizeof llvalue);
printf("%#llx ", llvalue);
}
}
if (sizeof charbuff >= sizeof lvalue
&& !(sizeof charbuff % sizeof lvalue)) {
printf("\nprinting as unsigned longs\n");
for (i = 0; i < sizeof charbuff; i += sizeof lvalue) {
memmove(&lvalue, charbuff + i, sizeof lvalue);
printf("%#lx ", lvalue);
}
}
if (sizeof charbuff >= sizeof value
&& !(sizeof charbuff % sizeof value)) {
printf("\nprinting as unsigned ints\n");
for (i = 0; i < sizeof charbuff; i += sizeof value) {
memmove(&value, charbuff + i, sizeof value);
printf("%#x ", value);
}
}

putchar('\n');
return 0;
}
[output for one implementation]

printing bytes:
0x2d 0xcf 0x46 0x29 0x04 0xb4 0x78 0xd8
0x68 0xa7 0xff 0x3f 0x2b 0xf1 0xfc 0xd9
0x7a 0x96 0x09 0x2c 0xa5 0x57 0x74 0x64
0xc4 0xaf 0x15 0x28 0xa4 0xe9 0x57 0xdb
printing as unsigned long longs
0xd878b4042946cf2d 0xd9fcf12b3fffa768 0x647457a52c09967a 0xdb57e9a42815afc4
printing as unsigned longs
0x2946cf2d 0xd878b404 0x3fffa768 0xd9fcf12b 0x2c09967a 0x647457a5
0x2815afc4 0xdb57e9a4
printing as unsigned ints
0x2946cf2d 0xd878b404 0x3fffa768 0xd9fcf12b 0x2c09967a 0x647457a5
0x2815afc4 0xdb57e9a4
Dec 12 '07 #2
On 12 dc, 20:26, Martin Ambuhl <mamb...@earthlink.netwrote:
laurent.paul...@techway.fr wrote:
Hello
I have a buffer of char and I want to print in a 64 bit word in hexa.

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>

int main(void)
{
const size_t size_of_charbuff = 32;
unsigned long long llvalue;
unsigned long lvalue;
unsigned int value;
size_t i;
unsigned char charbuff[size_of_charbuff];

for (i = 0; i < sizeof charbuff; i++)
charbuff[i] = rand() & 0377;

if (64 % CHAR_BIT) {
fprintf(stderr, "%s\n", "64 is not a multiple of CHAR_BIT\n"
"giving up.\n");
exit(EXIT_FAILURE);
}
printf("printing bytes:\n");
for (i = 0; i < sizeof charbuff; i++)
printf("%#04x%c", charbuff[i], (7 == i % 8) ? '\n' : ' ');
putchar('\n');

if (sizeof charbuff >= sizeof llvalue
&& !(sizeof charbuff % sizeof llvalue)) {
printf("\nprinting as unsigned long longs\n");
for (i = 0; i < sizeof charbuff; i += sizeof llvalue) {
memmove(&llvalue, charbuff + i, sizeof llvalue);
printf("%#llx ", llvalue);
}
}

if (sizeof charbuff >= sizeof lvalue
&& !(sizeof charbuff % sizeof lvalue)) {
printf("\nprinting as unsigned longs\n");
for (i = 0; i < sizeof charbuff; i += sizeof lvalue) {
memmove(&lvalue, charbuff + i, sizeof lvalue);
printf("%#lx ", lvalue);
}
}

if (sizeof charbuff >= sizeof value
&& !(sizeof charbuff % sizeof value)) {
printf("\nprinting as unsigned ints\n");
for (i = 0; i < sizeof charbuff; i += sizeof value) {
memmove(&value, charbuff + i, sizeof value);
printf("%#x ", value);
}
}

putchar('\n');
return 0;

}

[output for one implementation]

printing bytes:
0x2d 0xcf 0x46 0x29 0x04 0xb4 0x78 0xd8
0x68 0xa7 0xff 0x3f 0x2b 0xf1 0xfc 0xd9
0x7a 0x96 0x09 0x2c 0xa5 0x57 0x74 0x64
0xc4 0xaf 0x15 0x28 0xa4 0xe9 0x57 0xdb

printing as unsigned long longs
0xd878b4042946cf2d 0xd9fcf12b3fffa768 0x647457a52c09967a 0xdb57e9a42815afc4
printing as unsigned longs
0x2946cf2d 0xd878b404 0x3fffa768 0xd9fcf12b 0x2c09967a 0x647457a5
0x2815afc4 0xdb57e9a4
printing as unsigned ints
0x2946cf2d 0xd878b404 0x3fffa768 0xd9fcf12b 0x2c09967a 0x647457a5
0x2815afc4 0xdb57e9a4
Martin, you are the best !
Thank you very very much !
Dec 13 '07 #3

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

Similar topics

17
by: Axel | last post by:
Hiho, here my Newbie question (Win32 GUI): I'm reading a file in binary mode to a char* named buffer. I used malloc(filesize) to make the needed space avaiable. The filedata in the buffer...
8
by: Ekim | last post by:
my question is as follows: I've got a DLL in which I have a method GetBuffer (this one is extern, exported, is called from outside this program) which shall pass a char-buffer to the...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
7
by: Fernando | last post by:
Hello, I want call, from main program, to a function (with void arguments), what it run the function program and return the data of type char. I have make a example of code, by it dont run, I...
12
by: Lars Langer | last post by:
Hi there, I'm new to this C - never the less - I'm trying to search a string for the occurence of a substring - However, I'm not very succesful since my use of the strstr function always returns...
3
by: fernandez.dan | last post by:
My question is about if I should return char* or have a char* as an argument. My basic premise for this function is to return a char* buffer and the size of the buffer to the caller. I know that...
3
by: gordy | last post by:
Newbie question, please by gentle. I'm trying to read in a text file containing the days of the week, one on each line. Each line should be pointed to by an array of pointers to char but I'm...
15
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123水泥"; char buffer; ...
2
by: esanchezfo | last post by:
Please tell me what is wrong, what is the cause of the segmentation? #include <stdio.h> #include <malloc.h> void GetBuffer (unsigned char ** buffer) { * buffer = malloc (sizeof (unsigned...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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...

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.