473,326 Members | 2,175 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,326 software developers and data experts.

sprintf syntax for string to hex

Hi All,

I am trying to figure out a right syntax to convert an integer array
into hex array. sprintf worked for me on doing single integer:

int i, Iarray[8], n=15;
char buf[9];
sprintf(buf, "0x%02x", n);

The above code worked. Howeve, what I am trying to do is to convert an
array. I tried the following syntax, no good:

for (i=0; i<9; i++) sprintf(buff[i], "0x%02x", Iarray[i]);
for (i=0; i<9; i++) sprintf(buff + i, "0x%02x", Iarray[i]);

Anybody can help? Thanks a lot!

Huey

Nov 14 '05 #1
3 41929
hu********@yahoo.com wrote:
Hi All,

I am trying to figure out a right syntax to convert an integer array
into hex array. sprintf worked for me on doing single integer:

int i, Iarray[8], n=15;
char buf[9];
sprintf(buf, "0x%02x", n);

The above code worked. Howeve, what I am trying to do is to convert an
array. I tried the following syntax, no good:

for (i=0; i<9; i++) sprintf(buff[i], "0x%02x", Iarray[i]);
for (i=0; i<9; i++) sprintf(buff + i, "0x%02x", Iarray[i]);

Anybody can help? Thanks a lot!

Huey


You are incrementing the destination-buffer ( buff ) via i.
But you are writing on each run in the for-loop 4 Characters to it:
Assume the first three bytes in your buffer being ab, cd and ef.
You want to write : 0xab0xcd0xef (according to your format string without a
blank)
But in fact you write:
0xab
0xcd
0xef
=
000xef

Regards
Jogi

P.S.:
1.) you should look to the size of your destination-buffer, if your program
with the current fromat-string should work, it must be at least four times
bigger than the source !
2.) you should try to use the return-value of sprintf, which tells you how
much was written, so you could end up in something like:
p = buff;
for(...)
p += sprintf( p, "0x%02x", Iarray[i]);
--
The particular mistake will not be repeated. There are plenty of
mistakes left that have not yet been used.**********A.*Tanenbaum**
Jo*****@kuenstner.de
Nov 14 '05 #2
hu********@yahoo.com wrote:
Hi All,

I am trying to figure out a right syntax to convert an integer array
into hex array. sprintf worked for me on doing single integer:
Nope, you are converting an array of int into a string containing
a hex representation. You probably want to convert the array of
int into an array of strings which each contain the hex number.
int i, Iarray[8], n=15;
char buf[9];
sprintf(buf, "0x%02x", n);

The above code worked. Howeve, what I am trying to do is to convert an
array. I tried the following syntax, no good:

for (i=0; i<9; i++) sprintf(buff[i], "0x%02x", Iarray[i]); &buff[i] if any for (i=0; i<9; i++) sprintf(buff + i, "0x%02x", Iarray[i]);


This effectively gives you a buffer overrun as you write
at least four characters plus string terminator to buff[0], buff[1],
...., buff[8], i.e. you occupy buff[0]..buff[12] even though you
only are allowed to access buff[0]..buff[8].
In addition, you access Iarray[8] which also is not available
by you. Even if buf is large enough and Iarray[8] exists, you
effectively could as well write
sprintf(buff, "000000000x%02x", Iarray[8]);
instead of your loops.

Apart from that, %x denotes an argument of type unsigned int,
so we need (unsigned int)Iarray[i].

The right size for buf to contain only n for arbitrary
unsigned int values is
(sizeof(unsigned int)*CHAR_BIT+3)/4
for the hex digits plus 2 for the leading '0' and 'x' plus the
string terminator '\0'. On typical implementations with 32-Bit
unsigned int, this is 11. If you use C99, you should
use snprintf() instead of sprintf() as sprintf() is inherently
unsafe. Then the correct minimum buffer size does not play
as much of a role.

Do not use "magic numbers" explicitly in your code; instead
#define them in one place and only use them.

#define LARGE_ENOUGH 11
#define ARRAY_SIZE 9

size_t i;
int Iarray[ARRAY_SIZE], ret;
char buffers[ARRAY_SIZE][LARGE_ENOUGH];

.....

for (i=0; i<ARRAY_SIZE; i++) {
ret = sprintf(buf[i], "0x%02x", (unsigned) Iarray[i]);
if (ret >= LARGE_ENOUGH) {
/* We already have overwritten the array bounds and are
** deep in undefined behaviour; with a bit of luck, we
** will get this out: */
fprintf(stderr, "LARGE_ENOUGH is not large enough by %d\n",
ret+1-LARGE_ENOUGH);
/* Probably, data was corrupted; die. */
exit(EXIT_FAILURE);
} else if (ret < 0) {
fprintf(stderr, "sprintf failed for i=%lu\n",
(unsigned long) i);
}
}

In C99, we can do the loop somewhat cleaner:

for (i=0; i<ARRAY_SIZE; i++) {
ret = snprintf(buf[i], LARGE_ENOUGH, "0x%02x", (unsigned) Iarray[i]);
if (ret >= LARGE_ENOUGH) {
/* We have not overwritten anything but: */
fprintf(stderr, "LARGE_ENOUGH is not large enough by %d\n",
ret+1-LARGE_ENOUGH);
} else if (ret < 0) {
fprintf(stderr, "sprintf failed for i=%zu\n", i);
}
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3
hu********@yahoo.com wrote:

I am trying to figure out a right syntax to convert an integer array
into hex array. sprintf worked for me on doing single integer:

int i, Iarray[8], n=15;
char buf[9];
sprintf(buf, "0x%02x", n);

The above code worked. Howeve, what I am trying to do is to convert an
array. I tried the following syntax, no good:

for (i=0; i<9; i++) sprintf(buff[i], "0x%02x", Iarray[i]);
for (i=0; i<9; i++) sprintf(buff + i, "0x%02x", Iarray[i]);

Anybody can help? Thanks a lot!


#define NINE 9
#define FIFTEEN 15 /* or suitable names for the purpost */
int i, Iarray[NINE], n = FIFTEEN;
char buf[NINE * FIFTEEN]; /* holds it all */
char *bptr;

bptr = buf;
for (i = 0; i < NINE; i++) {
bptr += sprintf(bptr, ......);
}

(UNTESTED) but the point is advancing bptr by the amount written,
and having a large enouth buf for the whole schmeer.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #4

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

Similar topics

13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
2
by: OneLG13 | last post by:
I have a program that uses a scripting language that does not support formating strings with a printf syntax. Thus, I am writing a plugin to add the functionality. The entry point and argument...
2
by: steve | last post by:
hey i wrote a progam a wihle back in c++ and i used the sprintf function as follows: OVERLAPPED osWait = {0}; DWORD dwWritten; char B; sprintf (B, "\x02" "CB 0 1" "\x0D\x0A\x03");...
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
19
by: Carson | last post by:
Is it possible to remove the dashes in a social security number using sprintf? If so what would the syntax look like? If sprintf won't do this is there a C function that will?
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
5
by: lawpoop | last post by:
Hello all - I have sprintf formatting a number that goes on a report. I'm using %05.2f to change 5.5 to 05.50, for example. However, I'm having a problem with negative values -- I get -3.43, for...
2
by: Sudhakar | last post by:
until i started using the techniques for avoiding sql injection, i have been using a normal insert and select sql query which worked fine. i have a registration page where a user enters their...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.