Question: Unicode <-> HEX conversion in C source file?

?

^_^

conversion from:

a="a";

to

a=0x????;

If there are many unicode strings to convert, how can I do batch-conversion?
 
K

Kevin Goodsell

^_^ said:
conversion from:

a="a";

to

a=0x????;

If there are many unicode strings to convert, how can I do batch-conversion?

If you really want help, then

1) Stop cross-posting wildly.
2) Stop re-posting similar messages over and over.
3) Phrase your question in a way that we can understand it.

Try posting ONE message to ONE relevant group that explains your problem
in sufficient detail, then wait for a reply (which may take several
hours). Otherwise you are likely to be ignored, flamed, and/or killfiled.

-Kevin
 
S

Stephen Howe

If you really want help, then
1) Stop cross-posting wildly.
2) Stop re-posting similar messages over and over.
3) Phrase your question in a way that we can understand it.

Try posting ONE message to ONE relevant group that explains your problem
in sufficient detail, then wait for a reply (which may take several
hours). Otherwise you are likely to be ignored, flamed, and/or killfiled.

I don't think this guy speaks English that well, it is a foreign language to
him, hence the cryptic messages.
Probably Chinese.

Stephen Howe
 
M

Morris Dovey

Stephen said:
I don't think this guy speaks English that well, it is a
foreign language to him, hence the cryptic messages. Probably
Chinese.

[Reading in news:comp.lang.c]

No need to guess. From cleansugar's header:

Organization: Korea Telecom
Message-ID: <[email protected]>

I think the OP wants a tool that can be used to convert string
literals to unicode equivalents in C and/or C++ source files.

Can someone who knows more about this than I either redirect or
provide help?
 
T

Thomas Wintschel

?

^_^

I'm sorry that I was rude to speak unpolite broken English.

It's my fault. I am not an English speaker

Though, I can speak more correct expression, I was neglect.

Sorry.


What I want is to convert Unicode characters in source code to 0x??? format.

Then it is going to be saved as ASCII format a documents.

Written in not-Latin format Unicode characters in source code cause that
English OS users can not read it without fonts.

If source code's format were saved as UTF8, compiler reads it automatically.

But I don't want this method.

I want to know, either, that convert decimal format numbers to hexademical
format.

For example, I'll show an source.

example.cpp:

#define MAX 16777215
void main(){
if (MAX==a) printf("wrong\n";);
}

example_I_wanted.cpp
#define MAX 0xFFFFFF <------*this part*
void main(){
if (MAX==a) printf("wrong\n";);
}

To do so, C or C++ source parsing->converting DEC to HEX->saving CPP file
with converted characters are needed.

I don't know detailed metheds.

If gurus like you give me some good ways, I will follow your wisdom.

Thank you and I'm sorry again.



Morris Dovey said:
Stephen said:
I don't think this guy speaks English that well, it is a
foreign language to him, hence the cryptic messages. Probably
Chinese.

[Reading in news:comp.lang.c]

No need to guess. From cleansugar's header:

Organization: Korea Telecom
Message-ID: <[email protected]>

I think the OP wants a tool that can be used to convert string
literals to unicode equivalents in C and/or C++ source files.

Can someone who knows more about this than I either redirect or
provide help?
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.
 
J

Joona I Palaste

^_^ <[email protected]> scribbled the following
What I want is to convert Unicode characters in source code to 0x??? format.
Then it is going to be saved as ASCII format a documents.
Written in not-Latin format Unicode characters in source code cause that
English OS users can not read it without fonts.
If source code's format were saved as UTF8, compiler reads it automatically.
But I don't want this method.
I want to know, either, that convert decimal format numbers to hexademical
format.
For example, I'll show an source.

#define MAX 16777215
void main(){
if (MAX==a) printf("wrong\n";);
}
example_I_wanted.cpp
#define MAX 0xFFFFFF <------*this part*
void main(){
if (MAX==a) printf("wrong\n";);
}

You don't *HAVE* to do this. As numbers, 16777215 and 0xFFFFFF are
completely interchangable within a C or C++ program. The runtime
program will only see them as a pattern of bits anyway.

And void main() is an illegal form of main(). Use int main().

So, the answer to your question is: your programs should work fine as
they are.
 
M

Martin Ambuhl

^_^ said:
I'm sorry that I was rude to speak unpolite broken English.
It's my fault. I am not an English speaker

That's OK, it wasn't rude, nor was your English unpolite in any way. (By
the way, the normal English word is "impolite." "Unpolite" is perfectly
logical and understandable, but it disappeared from normal English use in
the early 18th century.) The problem is that you didn't give us a question
that we could understand. Many people who *are* native English speakers
fail to do this.

Posting to both C and C++ newsgroups is likely an error. C and C++ are
different languages, and, even when the languages admit the same forms of
code, the normal idioms in the two languages are different. It makes sense
to post to both _only_ when the question has the same answers in both
languages. Since you can't know this, since you would then already know
the answer, it is best to post to a newsgroup for the language you are using.
Though, I can speak more correct expression, I was neglect.

As a side note, you might consider comp.usage.english as another newsgroup
you might post in, if improving your English is important to you. The
above line, for example, might more idiomatically be written, "However, I
can express myself better. I was negligent [or neglectful]."

What I want is to convert Unicode characters in source code to 0x??? format.

If you can read the Unicode characters into a buffer, you can convert those
chars into an integer, as long as the total number of bytes in a character
is less than the sizeof the integer (best unsigned) type that you use.

I want to know, either, that convert decimal format numbers to hexademical
format.

Numbers as stored are simply binary, interpreted for humans as in some base.
Suppse you have an unsigned int
unsigned int a = 263;
We can display this as octal
printf("%#o\n",a); /* displays 0407 */
or hex
printf("%#x\n",a); /* displays 0x107 */
or decimal
printf("%u\n",a); /* displays 263 */
For example, I'll show an source.

example.cpp:

#define MAX 16777215
void main(){

main always returns an int. "void" is wrong. Don't do this.
if (MAX==a) printf("wrong\n";);

Even though this is an example of an input file, it is best not to post
hopeless code.
The variable 'a' is undeclared.
The C++ people may object that "printf" is too un-C++-like and complain
that <cstdio> is not #included.
The C people might complain that <stdio.h> is not #included. People
using compilers without C99 conformance (almost all), may complain
that main should actually return a value; 0 is common for successful
completion and EXIT_SUCCESS and EXIT_FAILURE are available if
}

example_I_wanted.cpp
#define MAX 0xFFFFFF <------*this part*
void main(){

main always returns an int. "void" is wrong. Don't do this.
if (MAX==a) printf("wrong\n";);
}

To do so, C or C++ source parsing->converting DEC to HEX->saving CPP file
with converted characters are needed.

To parse an input file containing a C program is probably beyond you at the
moment. You will need to detect sequence of characters that might be an
integer, determine that it is one (this requires examining its context),
and probably checking the use for signedness.

It is probably better for you to edit these files by hand. It is largely
because of the occurances "void main()" that I presume that your computing
skills are not up to writing such a program. If I am in error, I apologize.
 
?

^_^

Why I want 0x???? is easy reading.


Joona I Palaste said:
^_^ <[email protected]> scribbled the following
format.

automatically.




You don't *HAVE* to do this. As numbers, 16777215 and 0xFFFFFF are
completely interchangable within a C or C++ program. The runtime
program will only see them as a pattern of bits anyway.

And void main() is an illegal form of main(). Use int main().

So, the answer to your question is: your programs should work fine as
they are.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Life without ostriches is like coffee with milk."
- Mika P. Nieminen
 
J

Joona I Palaste

^_^ <[email protected]> scribbled the following
Why I want 0x???? is easy reading.

Oh, now I see. Well, I don't have any ready-made solution for changing
the decimal values to hexadecimal ones. Sorry for wasting your time
answering the wrong question.
 
?

^_^

Thank you very much.

Martin Ambuhl said:
^_^ said:
I'm sorry that I was rude to speak unpolite broken English.
It's my fault. I am not an English speaker

That's OK, it wasn't rude, nor was your English unpolite in any way. (By
the way, the normal English word is "impolite." "Unpolite" is perfectly
logical and understandable, but it disappeared from normal English use in
the early 18th century.) The problem is that you didn't give us a question
that we could understand. Many people who *are* native English speakers
fail to do this.

Posting to both C and C++ newsgroups is likely an error. C and C++ are
different languages, and, even when the languages admit the same forms of
code, the normal idioms in the two languages are different. It makes sense
to post to both _only_ when the question has the same answers in both
languages. Since you can't know this, since you would then already know
the answer, it is best to post to a newsgroup for the language you are using.
Though, I can speak more correct expression, I was neglect.

As a side note, you might consider comp.usage.english as another newsgroup
you might post in, if improving your English is important to you. The
above line, for example, might more idiomatically be written, "However, I
can express myself better. I was negligent [or neglectful]."

What I want is to convert Unicode characters in source code to 0x???
format.

If you can read the Unicode characters into a buffer, you can convert those
chars into an integer, as long as the total number of bytes in a character
is less than the sizeof the integer (best unsigned) type that you use.

I want to know, either, that convert decimal format numbers to hexademical
format.

Numbers as stored are simply binary, interpreted for humans as in some base.
Suppse you have an unsigned int
unsigned int a = 263;
We can display this as octal
printf("%#o\n",a); /* displays 0407 */
or hex
printf("%#x\n",a); /* displays 0x107 */
or decimal
printf("%u\n",a); /* displays 263 */
For example, I'll show an source.

example.cpp:

#define MAX 16777215
void main(){

main always returns an int. "void" is wrong. Don't do this.
if (MAX==a) printf("wrong\n";);

Even though this is an example of an input file, it is best not to post
hopeless code.
The variable 'a' is undeclared.
The C++ people may object that "printf" is too un-C++-like and complain
that <cstdio> is not #included.
The C people might complain that <stdio.h> is not #included. People
using compilers without C99 conformance (almost all), may complain
that main should actually return a value; 0 is common for successful
completion and EXIT_SUCCESS and EXIT_FAILURE are available if
}

example_I_wanted.cpp
#define MAX 0xFFFFFF <------*this part*
void main(){

main always returns an int. "void" is wrong. Don't do this.
if (MAX==a) printf("wrong\n";);
}

To do so, C or C++ source parsing->converting DEC to HEX->saving CPP file
with converted characters are needed.

To parse an input file containing a C program is probably beyond you at the
moment. You will need to detect sequence of characters that might be an
integer, determine that it is one (this requires examining its context),
and probably checking the use for signedness.

It is probably better for you to edit these files by hand. It is largely
because of the occurances "void main()" that I presume that your computing
skills are not up to writing such a program. If I am in error, I apologize.
 
B

Ben Pfaff

^_^ said:
I want to know, either, that convert decimal format numbers to hexademical
format.

Several of us have pointed out that you probably don't want to do
this and that it will be difficult even if you do. However, the
difficulty mainly stems from a desire to get the result
completely correct. If you're not concerned with complete
correctness, but would be willing to look over the results and
fix any mistakes (which would probably be rare), then I'd bet you
could write a fairly simple script in Perl or another scripting
langugae to do your translation; e.g., something like this, which
I have not tested at all and may contain bugs or simply be one
big bug:
#! /usr/bin/perl -p
while (/(?<!0x)([0-9]+)/) {
$_ = $` . sprintf("0x%x", $1) . $';
}
 
L

LaDainian Tomlinson

Ben Pfaff said:
^_^ said:
I want to know, either, that convert decimal format numbers to hexademical
format.

Several of us have pointed out that you probably don't want to do
this and that it will be difficult even if you do. However, the
difficulty mainly stems from a desire to get the result
completely correct. If you're not concerned with complete
correctness, but would be willing to look over the results and
fix any mistakes (which would probably be rare), then I'd bet you
could write a fairly simple script in Perl or another scripting
langugae to do your translation; e.g., something like this, which
I have not tested at all and may contain bugs or simply be one
big bug:
#! /usr/bin/perl -p
while (/(?<!0x)([0-9]+)/) {
$_ = $` . sprintf("0x%x", $1) . $';
}

Unfortunately, it actually hangs on the first line that _does_ contain a
number :). It would also try to change

int data14;

to

int data0xe;

which probably isn't desirable (only slightly less desirable than 14 variables
named 'data'). The following is only slightly more robust, but the OP can run
with it if he feels inclined:


[~/perl: 137]% cat numbers
#define MAX 16777215

int main(void){
int x = 14;
int y17 = 9;
int z = 0x10A9;
return 0;
}
[~/perl: 138]% perl -pe 's/\b([0-9]+)\b/sprintf "0x\U%x", $1/ge' numbers
#define MAX 0xFFFFFF

int main(void){
int x = 0xE;
int y17 = 0x9;
int z = 0x10A9;
return 0x0;
}
[~/perl: 139]%


Good luck,

Brandan L.
 
B

Ben Pfaff

LaDainian Tomlinson said:
Ben Pfaff said:
^_^ said:
I want to know, either, that convert decimal format numbers to hexademical
format.

while (/(?<!0x)([0-9]+)/) {
$_ = $` . sprintf("0x%x", $1) . $';
}

[~/perl: 138]% perl -pe 's/\b([0-9]+)\b/sprintf "0x\U%x", $1/ge' numbers

Ah, I'd forgotten about the `e' flag, thanks.
 
J

Jeremy Yallop

LaDainian said:
[~/perl: 138]% perl -pe 's/\b([0-9]+)\b/sprintf "0x\U%x", $1/ge' numbers

$ perl -pe 's/\b([0-9]+)\b/sprintf "0x\U%x", $1/ge' <<< 010
0xA
 
C

Christof Krueger

Jeremy said:
LaDainian said:
[~/perl: 138]% perl -pe 's/\b([0-9]+)\b/sprintf "0x\U%x", $1/ge' numbers


$ perl -pe 's/\b([0-9]+)\b/sprintf "0x\U%x", $1/ge' <<< 010
0xA
Right. Numbers in octal base should be excluded because the conversion
would change the value.

changint to
$ perl -pe 's/\b([1-9][0-9]+)\b/sprintf "0x\U%x", $1/ge'
should help
 
Ç

Ç㼺¿í

test..



LaDainian Tomlinson said:
Ben Pfaff said:
^_^ said:
I want to know, either, that convert decimal format numbers to hexademical
format.

Several of us have pointed out that you probably don't want to do
this and that it will be difficult even if you do. However, the
difficulty mainly stems from a desire to get the result
completely correct. If you're not concerned with complete
correctness, but would be willing to look over the results and
fix any mistakes (which would probably be rare), then I'd bet you
could write a fairly simple script in Perl or another scripting
langugae to do your translation; e.g., something like this, which
I have not tested at all and may contain bugs or simply be one
big bug:
#! /usr/bin/perl -p
while (/(?<!0x)([0-9]+)/) {
$_ = $` . sprintf("0x%x", $1) . $';
}

Unfortunately, it actually hangs on the first line that _does_ contain a
number :). It would also try to change

int data14;

to

int data0xe;

which probably isn't desirable (only slightly less desirable than 14 variables
named 'data'). The following is only slightly more robust, but the OP can run
with it if he feels inclined:


[~/perl: 137]% cat numbers
#define MAX 16777215

int main(void){
int x = 14;
int y17 = 9;
int z = 0x10A9;
return 0;
}
[~/perl: 138]% perl -pe 's/\b([0-9]+)\b/sprintf "0x\U%x", $1/ge' numbers
#define MAX 0xFFFFFF

int main(void){
int x = 0xE;
int y17 = 0x9;
int z = 0x10A9;
return 0x0;
}
[~/perl: 139]%


Good luck,

Brandan L.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top