uint32_t printing zeros

zizi21 used Ask the Experts™

i have a string that is uint32_t.

uint32_t *t;

//allocate memory
t = "hello";

i want to print the integer value represented by each int

so, i do this..

print value using %d

i am getting some weird values with the rest being zeros...

is something wrong...usually, for ascii characters, i get 0 , 1,, but with this, ...
it is just junk..
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

>> i have a string that is uint32_t.

That doesn't make a lot of sense.

>> uint32_t *t;

This is a pointer to one (or more) uint32_t values. It is NOT a string.

>> //allocate memory

Allocating memory here, before would cause a memory leak, because you overwrite the pointer a bit further in the code :

>> t = "hello";

The type of the string literal "hello" is const char*. This is NOT compatible with uint32_t. So, this assignment does not make much sense.

Could you explain what it is that you are trying to do ?


//allocate memory
t2 = (uint32_t *) malloc(len*sizeof(unint32_t ));

//do a fread from file which reads the string hello ..i was trying to simplify it by just saying allcoate memory,etc..

now, i am trying to print the string which i am able using %s...

but i want to know the values it represents...

Top Expert 2009

Did you understand what I said in my previous post ? uint32_t* is NOT a string. You can NOT show it with %s, since that wouldn't make sense. It also does not make sense to read a string from a file into an array of uint32_t.

As I asked in my previous post : Could you explain what it is that you are trying to do ?

this shows how to convert characters to integers: http://www.dreamincode.net/code/snippet573.htm


i am trying to read unicode characters.
it worked with %s.


When i try to print in hexadecimal, this is what i get.


h is 6568
e is 6C6C
l is 6F
l is 0
o is 0

which doesn't make sense at all.

Top Expert 2009

>> it worked with %s.

No it didn't.

>>  6568
>>  6C6C
>>  6F

These are the five ASCII characters 'h' (0x68), 'e' (0x65), 'l' (0x6C), 'l' (0x6C) and 'o' (0x6F).

Each of these characters takes up ONE byte.
A uint32_t is 4 bytes.

>> which doesn't make sense at all.

Exactly. That's what I've been saying all along. And the reason it doesn't make sense, is because you're using uint32_t* instead of char*.
Use char* instead, and things will look a lot nicer.
char* t = (char*) malloc(len * sizeof(char));

fread(t, 1, len, fp);


Open in new window

Top Expert 2009

>> i am trying to read unicode characters.

What makes you think the file contains unicode ?
What is the file format ? Could you post a sample file here ?


i dont have any unicode file..i am making up the unicode file by putting in my text.. i assume that ascii characters are unicode as well


the file is in text format echo -n "hello" > hello
Top Expert 2009
>> i am making up the unicode file by putting in my text.. i assume that ascii characters are unicode as well

That's not how it works :)

>> the file is in text format echo -n "hello" > hello

That creates an ASCII file, so just read it as such. See the code sample I posted earlier. Note that I used an fread because that's what you wanted to use - it's probably better to use fgets instead.
ascii and unicode are not the same, the characters are either stored in one format or another.  What you actually end up getting could depend on compiler specifics and default settings.  I would recommened being explicit in what type of characters I am writing.  The Encoding class provides functions to explicity casts from string to ascii and unicode.  See code below taken from msdn (http://msdn.microsoft.com/en-us/library/system.text.encoding(VS.71).aspx).  In addition to the example, you can also output the the byte arrays returned by GetBytes as integers.

String* unicodeString = S"This string contains the unicode character Pi(\u03a0)";

   // Create two different encodings.
   Encoding * ascii = Encoding::ASCII;
   Encoding * unicode = Encoding::Unicode;

   // Convert the string into a Byte->Item[].
   Byte unicodeBytes[] = unicode -> GetBytes(unicodeString);

   // Perform the conversion from one encoding to the other.
   Byte asciiBytes[] = Encoding::Convert(unicode, ascii, unicodeBytes);

   // Convert the new Byte into[] a char and[] then into a string.
   // This is a slightly different approach to converting to illustrate
   // the use of GetCharCount/GetChars.
   Char asciiChars[] = new Char[ascii -> GetCharCount(asciiBytes, 0, asciiBytes -> Length)];
   ascii -> GetChars(asciiBytes, 0, asciiBytes->Length, asciiChars, 0);
   String* asciiString = new String(asciiChars);

   // Display the strings created before and after the conversion.
   Console::WriteLine(S"Original String*: {0}", unicodeString);
   Console::WriteLine(S"Ascii converted String*: {0}", asciiString);


thanks a million..trying it ...


thanks a million

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial