开发者

store a string in an int

i try to store a string into an integer as follows:

i read the characters of the string and every 4 characters i do this:

val = (int) ch << 24 | (int) ch << 16 | (int) ch << 8 | (int) ch;

Then i put the integer value in an array of integer that is called memory (=> int memory[16]).

I would like to do it in an automatic way for every length of a string, plus i have difficulties to inverse the procedure again for an arbitrary size string. Any help?

EDIT:

(from below)

Basically, i do an exercise in JAVA. It's a MIPS simulator system. I have Register, Datum, Instruction, Label, Control, APSImulator classes and others. When i try to load the program from an array to simulator's memory, i actually read every contents of the array which is called 'program' and put it in memory. Memory is 2048 long and 32 bits wide. Registers are declared also 32bit inte开发者_JAVA技巧gers. So when there is an content in the array like Datum.datum( "string" ) - Datum class has IntDatum and StringDatum subclasses - i have somehow to store the "string" in the simulator's data segment of memory. Memory is 0-1023 text and 1024-2047 data region. I also have to delimit the string with a null char - plus any checkings for full memory etc. I figure out that one way to store a String to MemContents ( reference type - empty interface - implemented by class that memory field belongs to ) is to store the string every ( 2 or maybe 4 symbols ) to a register and then take the contents of the register and store it in memory. So, i found very difficult to implement that and the reverse procedure also.


If you are working in C, you have your string in a char array that is of a size multiple of a int, you can just take the pointer to the char array, cast it to a pointer to a int array and do whatever you want with your int array. If you don't have this last guarantee, you may simply write a function that creates your int array on the fly:

size_t IntArrayFromString(const char * Source, int ** Dest)
{
    size_t stringLength=strlen(Source);
    size_t intArrElements;
    intArrElements=stringLength/sizeof(int);
    if(stringLength%sizeof(int)!=0)
        intArrElements++;
    *Dest=(int *)malloc(intArrElements*sizeof(int));
    (*Dest)[intArrElements-1]=0;
    memcpy(Dest, Source, stringLength);
    return intArrElements;
}

The caller is responsible for freeing the Dest buffer. (I'm not sure if it really works, I didn't test it)


Have you considered simply using String.getBytes() ? You can then use the byte array to create the ints (for example, using the BigInteger(byte[]) constructor.

This may not be the most efficient solution, but is probably less prone to errors and more readable.


Assuming Java: You could look at the ByteBuffer class, and it's getInt method. It has a byte order parameter which you need to configure first.


Basically, i do an exercise in JAVA. It's a MIPS simulator system. I have Register, Datum, Instruction, Label, Control, APSImulator classes and others. When i try to load the program from an array to simulator's memory, i actually read every contents of the array which is called 'program' and put it in memory. Memory is 2048 long and 32 bits wide. Registers are declared also 32bit integers. So when there is an content in the array like Datum.datum( "string" ) - Datum class has IntDatum and StringDatum subclasses - i have somehow to store the "string" in the simulator's data segment of memory. Memory is 0-1023 text and 1024-2047 data region. I also have to delimit the string with a null char - plus any checkings for full memory etc. I figure out that one way to store a String to MemContents ( reference type - empty interface - implemented by class that memory field belongs to ) is to store the string every ( 2 or maybe 4 symbols ) to a register and then take the contents of the register and store it in memory. So, i found very difficult to implement that and the reverse procedure also.


One common way to do this in C is to use a union. It could look like

union u_intstr {
  char fourChars[4];
  int  singleInt;
};

Set the chars into the union as

union u_intstr myIntStr;
myIntStr.fourChars[0] = ch1;
myIntStr.fourChars[1] = ch2;
myIntStr.fourChars[2] = ch3;
myIntStr.fourChars[3] = ch4;

and then access the int as

printf("%d\n", myIntStr.singleInt);

Edit

In your case for 16 ints the union could be extended to look like

union u_my16ints {
  char str[16*sizeof(int)];
  int  ints[16];
};


This is what I come up with

int len = strlen(str);
int count = (len + sizeof(int))/sizeof(int);
int *ptr = (int *)calloc(count, sizeof(int));
memcpy((void *)ptr, (void *)str, count*sizeof(int));

Due to the use of calloc(), the resulting buffer has at least one NULL, maybe more to pad the last integer. This is not portable because the integers are in native byte order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜