473,395 Members | 1,623 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,395 software developers and data experts.

sprintf: the format problem

Hi All,

I saved hashed values of hex into buf[32], then buf becomes unreadable
by the means of calling puts(buf), printf("%s") and so on. Then, I
need to convert the hex in buf[] into a char buffer cbuf[32], by
calling sprintf(). I did:

sprintf(cbuf, "%x", buf);

No error! Just no working. Well, I used sprintf() a lot in format "%s"
"%d", and liked sprintf. It is my first time of using it in format
"%x". I wonder does format affect sprintf? Anybody can help me to get
this trick? Thanks!

Huey
Nov 14 '05 #1
2 2512
Huey wrote:
Hi All,

I saved hashed values of hex into buf[32], then buf becomes unreadable
by the means of calling puts(buf), printf("%s") and so on. Then, I
need to convert the hex in buf[] into a char buffer cbuf[32], by
calling sprintf(). I did:

sprintf(cbuf, "%x", buf);


I understand very little of what you've said, but if buf is an array
then you certainly cannot use it this way in a sprintf call. The %x
format specifier applies *ONLY* to unsigned int. If you pass anything
else, the behavior is undefined.

I would try to suggest an alternative, but I have no idea what you are
trying to do.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2

"Huey" <hu********@yahoo.com> schrieb im Newsbeitrag
news:ae**************************@posting.google.c om...
Hi All,

I saved hashed values of hex into buf[32], then buf becomes unreadable
by the means of calling puts(buf), printf("%s") and so on. Then, I
need to convert the hex in buf[] into a char buffer cbuf[32], by
calling sprintf(). I did:

sprintf(cbuf, "%x", buf);
Try
(CHAR_BIT == 8 assumed)

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

int main(void)
{
int i;
unsigned char buf[32] =
{0, 1, 2, 3, 4, 5, 55, 66, 77};
char cbuf[97];

for(i = 0; i < 32; i++)
{
sprintf(cbuf + 3 * i, "%02x ", buf[i]);
}
puts(cbuf);
return EXIT_SUCCESS;
}


No error! Just no working. Well, I used sprintf() a lot in format "%s"
"%d", and liked sprintf. It is my first time of using it in format
"%x". I wonder does format affect sprintf? Anybody can help me to get
this trick? Thanks!


from N869:
o,u,x,X The unsigned int argument is converted to unsigned octal (o),
unsigned
decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd;
the
letters abcdef are used for x conversion and the letters ABCDEF for X
conversion. The precision specifies the minimum number of digits to appear;
if the value being converted can be represented in fewer digits, it is
expanded
with leading zeros. The default precision is 1. The result of converting a
zero value with a precision of zero is no characters.
Nov 14 '05 #3

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

Similar topics

3
by: Pilatus | last post by:
Hi everybody, I am beginner and I have a problem in this simple code : I want that the char contents 01234567, but it don't work Could you help me ? int bin ; char *buf = new char ; for (int...
1
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
2
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
4
by: ypjofficial | last post by:
Hello All, To use sprintf function we have to first create a char * and assign some memory to it or we have to fixed memory sized array. eg. char str;//or it can be //char * str =(char...
10
by: Saurabh | last post by:
Hi all, I am working on RedHat Linux GCC 3.0. I am trying to convert a long to string through sprintf. but i m getting segmantation fault. I tried snprintf also but no avail.here is the piece...
12
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???...
6
by: Henryk | last post by:
I did a lot of delphi GUI programming recently. In my experience most of the time you just want to throw a standard exception with a descriptive message. Then all calling functions can handle...
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...
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...

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.