Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Print 64 bit with a char buffer

Reply
Thread Tools

Print 64 bit with a char buffer

 
 
laurent.pauloin@techway.fr
Guest
Posts: n/a
 
      12-12-2007
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+
{
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+
{
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!
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-12-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 % ? '\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
 
Reply With Quote
 
 
 
 
laurent.pauloin@techway.fr
Guest
Posts: n/a
 
      12-13-2007
On 12 déc, 20:26, Martin Ambuhl <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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 % ? '\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 !
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert M bit buffer to N bit buffer runcyclexcski@yahoo.com C++ 2 03-26-2007 09:43 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Using a Windows XP 64 bit print driver on a 32 bit Windows 2003 Print Server Alex Griffin Windows 64bit 14 07-27-2005 04:20 PM
char* gets(char* buffer) Sathyaish C Programming 9 10-05-2004 10:38 PM



Advertisments