Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Converting long to a fixed length string

Posted on 1998-05-30
Last Modified: 2010-04-15
How do you convert a long to a string with a length of 4 and how do you convert it back to a long?

Question by:baileys
  • 6
  • 2
  • 2
  • +2
LVL 11

Expert Comment

ID: 1251061
Several options: sprintf(), ltoa(), atol().
Details comming...
LVL 11

Expert Comment

ID: 1251062
Using sprintf():

    char buffer[20];
    long num = 1234L;
    sprintf(buffer, "%4ld", num); /* "Percent Four El Di" in case the font is not clear */

sprintf() will not cause the result to be truncated (so make sure the original long is less than 10000 but larger than -1000 if you want it to fit in 4 spaces) but will pad the value to the right with spaces.  If you want left padding (right justification), use "%-4ld".  If you want leading zeros instead, use "%04ld".

Using sscanf():

    char* string = "4567";
    long num;
    sscanf(string, "%4ld", &num);

This will convert the first four numerical characters in the string into one long.

LVL 11

Expert Comment

ID: 1251063
Disregarding the length, you can use more efficient functions:

    char* string = "9876";
    long num = atol(string);


    char buffer[20];
    long num = 9876L;
    ltoa(num, buffer, 10); /* Calculation is decimal - base 10 */

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


Author Comment

ID: 1251064
I am converting Ada code to C.  I am not allowed to modify the file structures.  There are two Ada procedures that uses the "unchecked_conversion" procedure that converts one type of data to another type of data and back(without type checking or any checks) without losting the integrity of the data.  One of the Ada functions takes a string date in the format of "YYYYMMDDHHMMSS" and converts it to a long, which represent the number of seconds since Jan 1, 1970.  This long is then converted to a 4 byte string(using the "unchecked_conversion" procedure) which is stored in the database.

The second Ada procedure reverses the process.

Now, the big question is, how to do this in C?  The long HAS to fit into a 4 byte string.
LVL 84

Expert Comment

ID: 1251065
/* I don't know if this stores the same string as the Ada procedure, but it's certainly unchecked */

#include <string.h>
char string[4];
int num=896549100;

LVL 11

Accepted Solution

alexo earned 200 total points
ID: 1251066
You don't want to convert it to a string, you just want to use it as 4 bytes.
I suggest a cast.  Given a pointer to a long, convert it to a pointer to characters.  Then use the 4 characters from that pointer.  Remember, C strings are zero terminated so treating the "raw" 4 bytes as a string will fail if one of them is a binary zero.

    long num = 1234567890L;
    char* p = (char*)&num;

p[0] .. p[3] are the four bytes.

LVL 10

Expert Comment

ID: 1251067
I doubt that it is a four byte string (in the sense of a text string).  Rather it is just four bytes.  In that case, there is no conversion necessary ... a long is already four characters long.

The problem is that, for string manipulation function in C, a zero byte indicates the end of the string.

However, C also includes library functinos for working with arrays of bytes.  For example, you can use strcpy to copy a null terminated string, but memcpy to copy a fixed number of bytes.

If you REALLY need to convert the long to a 'string' of for bytes. then you can say someting like this...

union LongAs3Bytes {
  long lval;
  struct {
    char byte[4];
  } array;

then you can say ...

  LongAs4Bytes x;
  int i;
  x.lval = 1234;
  for (i = 0; i < 4; i++) {
    char b = x.array.byte[i];
    // etc

alternatively, you can simply cast
  long l = 1234;
  int i;
  char* array = (char*)l;
  for (i = 0; i < 4; i++) {
    char b = array[i];
    // etc

However, I think (from your description) that you can simply read and write the long directly .. and it will be read/written as four bytes.

As for the conversion from a date format to a long, there are C library functions that will help with this .. alternatively, look at www.snippets.org for appropriate routines.

Please let me if I have understood your problem correctly and have been of any help (or can be of any more)

If so, and you wish to grade me, remember that you must reject previously proposed answers first .. or get in touch with EE customer service and request a split of points.



Expert Comment

ID: 1251068
One thing you might want to consider is that just because the Ada program stores the data as four bytes doesn't mean that by overlapping them onto a C long variable will give you the correct information, especially if the data comes from a different operating system or compiler. Here is a prime example:

Suppose that the data was stored high-byte first. In other words, the number 00 00 00 FF might actually need to be mapped to FF 00 00 00.

This sort of stuff does come up, so if you are getting wrong answers, this might be why.

As a "worse" example, it might store bit information in reverse order. So,

Binary 10000000 00000000 00000000 01010101
Hex    8   0    0   0    0   0    5   5
Might need to be stored as:
Binary 10101010 00000000 00000000 00000001
Hex    A   A    0   0    0   0    0   1

(See how the BITS themselves are reversed?)

Just keep all this in mind. Also, are you sure your local compiler has long stored as four bytes?

Jes keeping you on your toes.


Author Comment

ID: 1251069
How do I get contact EE customer service?
LVL 11

Expert Comment

ID: 1251070
>> How do I get contact EE customer service?
Post a 0-point question in the "experts exchange" area.

>> Suppose that the data was stored high-byte first
The "endianness" rikonigsberg refers to is dependent only on the processor, not programming language, compiler or OS.

>> As a "worse" example, it might store bit information in reverse order.


Expert Comment

ID: 1251071
>> >> Suppose that the data was stored high-byte first
>> The "endianness" rikonigsberg refers to is dependent only on >> the processor, not programming language, compiler or OS.
>> >> As a "worse" example, it might store bit information in
>> reverse order.
>> Never!  

Perhaps. Like I said, I'm just throwing out a warning just in case the data did come from another source, and a straight mapping doesn't seem to work. Though it might be the case that pure reversal of the bits, reversal of the bytes could occur because the data could come from being processed on another processor. When I was referring to separate OSes, I was not referring to Win95 vs WinNT, but rather varying Unix operating systems. Details are sketchy off the top of my head, but there you go.

And yes, I couldn't remember the name "endian" :)
LVL 11

Expert Comment

ID: 1251072
And the autograder hits again!
LVL 10

Expert Comment

ID: 1251073
Gees .. that autograder is a busy chap !! :-)

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
sameEnds challenge 3 179
Constant string is of type char *   ? 7 42
What's wrong with the delete function in this program for queue using array? 8 26
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question